Class: RETS4R::ResponseDocument::Base

Inherits:
Nokogiri::XML::Document
  • Object
show all
Defined in:
lib/rets4r/response_document/base.rb

Overview

A ResponseDocument is a Nokogiri XML Document that lets you access all the important information in a RETS service response. This is a DOM style API, so if you have large files to handle or experience performance issues, you should consider switching to RETS4R::Client::CompactNokogiriParser::CompactDocument which provides a SAX API

Direct Known Subclasses

Search

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.safe_parse(*args) ⇒ Object

like #parse, but set the noblanks, noerror and recover options



16
17
18
19
20
21
22
# File 'lib/rets4r/response_document/base.rb', line 16

def self.safe_parse(*args)
  parse(*args) do |config|
    config.strict.noblanks
    config.strict.noerror
    config.strict.recover
  end
end

Instance Method Details

#countObject

the value of the Records attribute in the COUNT element as an integer



90
91
92
# File 'lib/rets4r/response_document/base.rb', line 90

def count
  self.at('/RETS/COUNT')['Records'].to_i
end

#error?Boolean

true unless the response was a success (reply_code: 0) or found no records (reply_code: 20201)

Returns:

  • (Boolean)


54
55
56
# File 'lib/rets4r/response_document/base.rb', line 54

def error?
  reply_code > 0 && reply_code != 20201
end

#invalid?Boolean

true if the document is not #valid?

Returns:

  • (Boolean)


65
66
67
# File 'lib/rets4r/response_document/base.rb', line 65

def invalid?
  !valid?
end

#max_rows?Boolean Also known as: maxrows?

true if the document has a MAXROWS element

Returns:

  • (Boolean)


45
46
47
# File 'lib/rets4r/response_document/base.rb', line 45

def max_rows?
  search('/RETS/MAXROWS').length > 0
end

#parse_countObject

a new transaction with the count as its response



100
101
102
# File 'lib/rets4r/response_document/base.rb', line 100

def parse_count
  to_transaction { count }
end

#parse_key_valueObject

a new transaction with the hash of the body in the response – TODO: put this in a LoginResponseDocument



112
113
114
# File 'lib/rets4r/response_document/base.rb', line 112

def parse_key_value
  to_transaction { to_h }
end

#parse_resultsObject

a new transaction with the response parsed using the CompactDataParser



105
106
107
# File 'lib/rets4r/response_document/base.rb', line 105

def parse_results
  RETS4R::ResponseDocument::Search.parse(self.to_s).to_transaction
end

#reply_codeObject

the reply code as an integer



30
31
32
# File 'lib/rets4r/response_document/base.rb', line 30

def reply_code
  root['ReplyCode'].to_i
end

#reply_textObject

the contents of the ReplyText attribute



40
41
42
# File 'lib/rets4r/response_document/base.rb', line 40

def reply_text
  root['ReplyText']
end

#rets?Boolean

true if the root element is named ‘RETS’

Returns:

  • (Boolean)


25
26
27
# File 'lib/rets4r/response_document/base.rb', line 25

def rets?
  root && root.name == 'RETS'
end

#success?Boolean

true if the reply code indicates success (is 0)

Returns:

  • (Boolean)


35
36
37
# File 'lib/rets4r/response_document/base.rb', line 35

def success?
  reply_code == 0
end

#to_hObject

the body of the document parsed into a hash. Intended for use with Login responses – TODO: put this in a LoginResponseDocument



120
121
122
123
124
125
# File 'lib/rets4r/response_document/base.rb', line 120

def to_h
  pairs = first_child.text.each_line.collect do |line|
    parse_key_value_line(line)
  end
  Hash[pairs]
end

#to_rexmlObject

the ResponseDocument converted to a REXML::Document



95
96
97
# File 'lib/rets4r/response_document/base.rb', line 95

def to_rexml
  REXML::Document.new(to_s)
end

#to_transactionObject

a new transaction from this document. An optional block will be evaluated and placed in the transaction response, if provided



129
130
131
132
133
# File 'lib/rets4r/response_document/base.rb', line 129

def to_transaction
  Client::Transaction.new(self).tap do |t|
    t.response = yield if block_given?
  end
end

#valid?Boolean

true if there are no noticeable errors with the document. use #validate! if you need to access the errors

Returns:

  • (Boolean)


60
61
62
# File 'lib/rets4r/response_document/base.rb', line 60

def valid?
  !blank? && rets? && !error?
end

#validate!Object

raises exceptions if things are wrong. checks for

blank?

raises RETSException if the document is blank?

rets?

raises RETSException if the document is not rets?

error?

raises the relevant exception type if the reply_code indicates an error

returns self, so you can chain this – TODO: write a validate that’s consistant with the one in nokogiri



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rets4r/response_document/base.rb', line 77

def validate!
  raise Client::RETSException, 'No transaction body was returned.' if blank?

  raise Client::RETSException, "Response had invalid root node. Document was: #{inspect}" unless rets?

  if error?
    exception_type = Client::EXCEPTION_TYPES[reply_code] || Client::RETSTransactionException
    raise exception_type, "#{reply_code} - #{reply_text}"
  end
  self
end