-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrets_test.rb
More file actions
213 lines (187 loc) · 5.88 KB
/
rets_test.rb
File metadata and controls
213 lines (187 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'mechanize'
module RETS
@parsed_hash = {}
NUMBERS_TO_NAME = {
12=>"Twelve",
11 => "Eleven",
10 => "Ten",
9 => "Nin",
8 => "Eigh",
7 => "Seven",
6 => "Six",
5 => "fif",
4 => "Four",
3 => "Thi",
2 => "Seco",
1 => "Fir"
}
OBJECT_CLASS_TAGS = ['Residential', 'CommercialSale', 'Land', 'Member']
OBJECT_TAGS = ['Property', 'Member']
def self.login
@conn = Mechanize.new
@conn.add_auth("http://rets172lax.raprets.com:6103/Midlands/MIDL/login.aspx", ENV["RETS_USER_ID"], ENV["RETS_PASSWORD"])
@conn.user_agent = ENV["RETS_USER_AGENT"]
@conn.request_headers = { "RETS-Version" => "RETS/1.7.2" }
begin
@conn.post("http://rets172lax.raprets.com:6103/Midlands/MIDL/login.aspx")
rescue Mechanize::UnauthorizedError
RETS::Unauthorized.new
rescue => e
@exception = e
end
end
def self.logout
begin
@conn.post("http://rets172lax.raprets.com:6103/Midlands/MIDL/logout.aspx")
rescue => e
@exception = e
end
end
def self.metadata_system
query = {
"type" => "METADATA-RESOURCE",
"format" => "STANDARD-XML",
"ID" => 0
}
begin
response = @conn.post("http://rets172lax.raprets.com:6103/Midlands/MIDL/getmetadata.aspx", query)
rescue => e
@exception = e
end
response.xml.xpath('//Resource').each do |node|
puts "NEW RESOURCE"
node.children.each do |child_node|
puts "#{child_node.name}: #{child_node.child}"
end
end
end
def self.class_metadata(resource_id = 0)
query = {
"type" => "METADATA-CLASS",
"format" => "STANDARD-XML",
"ID" => resource_id
}
begin
response = @conn.post("http://rets172lax.raprets.com:6103/Midlands/MIDL/getmetadata.aspx", query)
rescue => e
@exception = e
end
response.xml.xpath('//Class').each do |node|
puts "NEW CLASS"
node.children.each do |child_node|
puts "#{child_node.name}: #{child_node.child}"
end
end
end
def self.table_metadata(resource_id, class_name = '')
resource_id = "#{resource_id}:class_name" if !class_name.empty?
query = {
"type" => "METADATA-TABLE",
"format" => "STANDARD-XML",
"ID" => (resource_id)
}
begin
response = @conn.post("http://rets172lax.raprets.com:6103/Midlands/MIDL/getmetadata.aspx", query)
rescue => e
@exception = e
end
response.xml.xpath('//Field').each do |node|
puts "NEW TABLE VALUE"
node.children.each do |child_node|
puts "#{child_node.name}: #{child_node.child}"
end
end
end
def self.search(query_string = nil)
query = {
# "SearchType" => "Agent",
"SearchType" => "Property",
#Residential
# "Class" => "MEMB",
"Class" => "LOTL",
#RETS requires queries use the lookup operator (=|) when using searching on codes of a lookup field.
"Query" => "(City=|Lincoln),(Status=|A)",
# "Query" => "(City=Lincoln)",(Status=|A)
#Query Language
"QueryType" => "DMQL2",
"Format" => "STANDARD-XML",
# "Limit" => "1",
"Count" => "1",
"StandardNames" => "0"
}
begin
@conn.post("http://rets172lax.raprets.com:6103/Midlands/MIDL/search.aspx", query)
rescue => e
@exception = e
end
end
module Parser
@current_object = ""
@current_class = ""
@object_hash = {}
def self.fixXml(body)
# Replace tags starting with numbers with text
body.gsub(/.*\<(\d).*\>.*\<\/(\1).*>.*/) do |broken_xml|
broken_xml.gsub($1, RETS::NUMBERS_TO_NAME[$1.to_i])
end
end
def self.parseXML(xml, field)
@parsed_hash = {}
@current_object = ""
@current_class = ""
count = xml.xpath("/RETS/COUNT").first.attributes["Records"].value
@parsed_hash.merge!({ "Count" => count })
xml.xpath("//#{field}").children.each do |node|
if OBJECT_CLASS_TAGS.include? node.name
@current_class = node.name
@parsed_hash[node.name] = {}
parse_children(node)
@parsed_hash[@current_class][@current_object] << @object_hash
end
end
@parsed_hash
end
def self.parse_children(node)
node.children.each do |child_node|
if (child_node.children.any?)
if child_node.children.length == 1 and child_node.child.text?
@object_hash[child_node.name] = child_node.child.content
elsif OBJECT_TAGS.include? child_node.name
@current_object = child_node.name
if @parsed_hash[@current_class][@current_object].nil?
@parsed_hash[@current_class][@current_object] = []
else
@parsed_hash[@current_class][@current_object] << @object_hash
end
@object_hash = {}
parse_children(child_node)
else
parse_children(child_node)
end
end
end
end
end
class Unauthorized
attr_reader :error_message
def initialize
@error_message = "Authorization failed. Please check your username, password, and user agent. If all are correct please wait a second and try again. The servers sometimes reject/lose sessions for no reason."
end
end
end
@response = RETS.login
unless @response == RETS::Unauthorized
@results = RETS.search
if @results == Mechanize::ResponseCodeError
puts "I should make a custome error for this too but I will in the actual library"
puts @results.page.xml.xpath('//RETS')[0].attributes["ReplyText"].value
else
@parsed_string = RETS::Parser.fixXml(@results.body)
@parsed_xml = Nokogiri::XML(@parsed_string)
@parsed_hash = RETS::Parser.parseXML(@parsed_xml, '/REData/REProperties') #/REProperties
puts @parsed_hash
end
else
puts "Unauthorized"
puts @response.inspect
end