josephholsten-rets4r 1.1.17 → 1.1.18
Sign up to get free protection for your applications and to get access to all the features.
- metadata +44 -84
- data/CHANGELOG +0 -336
- data/CONTRIBUTORS +0 -7
- data/GPL +0 -340
- data/LICENSE +0 -29
- data/NEWS +0 -183
- data/README.rdoc +0 -45
- data/RUBYS +0 -56
- data/Rakefile +0 -57
- data/TODO +0 -29
- data/VERSION.yml +0 -5
- data/examples/client_get_object.rb +0 -47
- data/examples/client_login.rb +0 -37
- data/examples/client_metadata.rb +0 -40
- data/examples/client_parser.rb +0 -10
- data/examples/client_search.rb +0 -47
- data/examples/settings.yml +0 -12
- data/lib/rets4r.rb +0 -9
- data/lib/rets4r/auth.rb +0 -73
- data/lib/rets4r/client.rb +0 -723
- data/lib/rets4r/client/data.rb +0 -14
- data/lib/rets4r/client/dataobject.rb +0 -20
- data/lib/rets4r/client/metadata.rb +0 -15
- data/lib/rets4r/client/parsers/compact.rb +0 -42
- data/lib/rets4r/client/parsers/compact_nokogiri.rb +0 -81
- data/lib/rets4r/client/parsers/metadata.rb +0 -92
- data/lib/rets4r/client/parsers/response_parser.rb +0 -100
- data/lib/rets4r/client/transaction.rb +0 -31
- data/lib/rets4r/core_ext/array/extract_options.rb +0 -15
- data/lib/rets4r/core_ext/class/attribute_accessors.rb +0 -58
- data/lib/rets4r/core_ext/hash/keys.rb +0 -46
- data/lib/rets4r/core_ext/hash/slice.rb +0 -39
- data/lib/rets4r/listing_mapper.rb +0 -17
- data/lib/rets4r/listing_service.rb +0 -35
- data/lib/rets4r/loader.rb +0 -8
- data/lib/tasks/annotations.rake +0 -20
- data/lib/tasks/coverage.rake +0 -13
- data/test/compact_nokogiri_test.rb +0 -35
- data/test/data/1.5/bad_compact.xml +0 -7
- data/test/data/1.5/count_only_compact.xml +0 -3
- data/test/data/1.5/error.xml +0 -1
- data/test/data/1.5/invalid_compact.xml +0 -4
- data/test/data/1.5/login.xml +0 -16
- data/test/data/1.5/metadata.xml +0 -0
- data/test/data/1.5/search_compact.xml +0 -8
- data/test/data/1.5/search_compact_big.xml +0 -136
- data/test/data/1.5/search_unescaped_compact.xml +0 -8
- data/test/data/listing_service.yml +0 -36
- data/test/listing_mapper_test.rb +0 -112
- data/test/loader_test.rb +0 -24
- data/test/test_auth.rb +0 -68
- data/test/test_client.rb +0 -315
- data/test/test_helper.rb +0 -12
- data/test/test_parser.rb +0 -96
data/lib/rets4r/client.rb
DELETED
@@ -1,723 +0,0 @@
|
|
1
|
-
# RETS4R Client
|
2
|
-
#
|
3
|
-
# Copyright (c) 2006 Scott Patterson <scott.patterson@digitalaun.com>
|
4
|
-
#
|
5
|
-
# This program is copyrighted free software by Scott Patterson. You can
|
6
|
-
# redistribute it and/or modify it under the same terms of Ruby's license;
|
7
|
-
# either the dual license version in 2003 (see the file RUBYS), or any later
|
8
|
-
# version.
|
9
|
-
#
|
10
|
-
# TODO: 1.0 Support (Adding this support should be fairly easy)
|
11
|
-
# TODO: 2.0 Support (Adding this support will be very difficult since it is a completely different methodology)
|
12
|
-
# TODO: Case-insensitive header
|
13
|
-
|
14
|
-
require 'digest/md5'
|
15
|
-
require 'net/http'
|
16
|
-
require 'uri'
|
17
|
-
require 'cgi'
|
18
|
-
require 'auth'
|
19
|
-
require 'client/dataobject'
|
20
|
-
require 'client/parsers/response_parser'
|
21
|
-
require 'client/parsers/compact'
|
22
|
-
require 'thread'
|
23
|
-
require 'logger'
|
24
|
-
|
25
|
-
module RETS4R
|
26
|
-
class Client
|
27
|
-
COMPACT_FORMAT = 'COMPACT'
|
28
|
-
|
29
|
-
METHOD_GET = 'GET'
|
30
|
-
METHOD_POST = 'POST'
|
31
|
-
METHOD_HEAD = 'HEAD'
|
32
|
-
|
33
|
-
DEFAULT_METHOD = METHOD_GET
|
34
|
-
DEFAULT_RETRY = 2
|
35
|
-
#DEFAULT_USER_AGENT = 'RETS4R/0.8.2' # FIXME
|
36
|
-
DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) ' +
|
37
|
-
'Gecko/2008050509 Firefox/3.0b5'
|
38
|
-
DEFAULT_RETS_VERSION = '1.7'
|
39
|
-
SUPPORTED_RETS_VERSIONS = ['1.5', '1.7']
|
40
|
-
CAPABILITY_LIST = [
|
41
|
-
'Action',
|
42
|
-
'ChangePassword',
|
43
|
-
'GetObject',
|
44
|
-
'Login',
|
45
|
-
'LoginComplete',
|
46
|
-
'Logout',
|
47
|
-
'Search',
|
48
|
-
'GetMetadata',
|
49
|
-
'Update'
|
50
|
-
]
|
51
|
-
|
52
|
-
# These are the response messages as defined in the RETS 1.5e2 and 1.7d6 specifications.
|
53
|
-
# Provided for convenience and are used by the HTTPError class to provide more useful
|
54
|
-
# messages.
|
55
|
-
RETS_HTTP_MESSAGES = {
|
56
|
-
'200' => 'Operation successful.',
|
57
|
-
'400' => 'The request could not be understood by the server due to malformed syntax.',
|
58
|
-
'401' => 'Either the header did not contain an acceptable Authorization or the ' +
|
59
|
-
'username/password was invalid. The server response MUST include a ' +
|
60
|
-
'WWW-Authenticate header field.',
|
61
|
-
'402' => 'The requested transaction requires a payment which could not be authorized.',
|
62
|
-
'403' => 'The server understood the request, but is refusing to fulfill it.',
|
63
|
-
'404' => 'The server has not found anything matching the Request-URI.',
|
64
|
-
'405' => 'The method specified in the Request-Line is not allowed for the resource ' +
|
65
|
-
'identified by the Request-URI.',
|
66
|
-
'406' => 'The resource identified by the request is only capable of generating response ' +
|
67
|
-
'entities which have content characteristics not acceptable according to the accept ' +
|
68
|
-
'headers sent in the request.',
|
69
|
-
'408' => 'The client did not produce a request within the time that the server was prepared to wait.',
|
70
|
-
'411' => 'The server refuses to accept the request without a defined Content-Length.',
|
71
|
-
'412' => 'Transaction not permitted at this point in the session.',
|
72
|
-
'413' => 'The server is refusing to process a request because the request entity is larger than ' +
|
73
|
-
'the server is willing or able to process.',
|
74
|
-
'414' => 'The server is refusing to service the request because the Request-URI is longer than ' +
|
75
|
-
'the server is willing to interpret. This error usually only occurs for a GET method.',
|
76
|
-
'500' => 'The server encountered an unexpected condition which prevented it from fulfilling ' +
|
77
|
-
'the request.',
|
78
|
-
'501' => 'The server does not support the functionality required to fulfill the request.',
|
79
|
-
'503' => 'The server is currently unable to handle the request due to a temporary overloading ' +
|
80
|
-
'or maintenance of the server.',
|
81
|
-
'505' => 'The server does not support, or refuses to support, the HTTP protocol version that ' +
|
82
|
-
'was used in the request message.',
|
83
|
-
}
|
84
|
-
|
85
|
-
attr_accessor :mimemap, :logger
|
86
|
-
attr_reader :format
|
87
|
-
|
88
|
-
# Constructor
|
89
|
-
#
|
90
|
-
# Requires the URL to the RETS server and takes an optional output format. The output format
|
91
|
-
# determines the type of data returned by the various RETS transaction methods.
|
92
|
-
def initialize(url, format = COMPACT_FORMAT)
|
93
|
-
@format = format
|
94
|
-
@urls = {
|
95
|
-
'Login' => URI.parse(url)
|
96
|
-
}
|
97
|
-
@nc = 0
|
98
|
-
@headers = {
|
99
|
-
'User-Agent' => DEFAULT_USER_AGENT,
|
100
|
-
'Accept' => '*/*',
|
101
|
-
'RETS-Version' => "RETS/#{DEFAULT_RETS_VERSION}",
|
102
|
-
'RETS-Session-ID' => '0'
|
103
|
-
}
|
104
|
-
@request_method = DEFAULT_METHOD
|
105
|
-
@semaphore = Mutex.new
|
106
|
-
|
107
|
-
@response_parser = RETS4R::Client::ResponseParser.new
|
108
|
-
|
109
|
-
self.mimemap = {
|
110
|
-
'image/jpeg' => 'jpg',
|
111
|
-
'image/gif' => 'gif'
|
112
|
-
}
|
113
|
-
|
114
|
-
if block_given?
|
115
|
-
yield self
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
# Assigns a block that will be called just before the request is sent.
|
120
|
-
# This block must accept three parameters:
|
121
|
-
# * self
|
122
|
-
# * Net::HTTP instance
|
123
|
-
# * Hash of headers
|
124
|
-
#
|
125
|
-
# The block's return value will be ignored. If you want to prevent the request
|
126
|
-
# to go through, raise an exception.
|
127
|
-
#
|
128
|
-
# == Example
|
129
|
-
#
|
130
|
-
# client = RETS4R::Client.new(...)
|
131
|
-
# # Make a new pre_request_block that calculates the RETS-UA-Authorization header.
|
132
|
-
# client.set_pre_request_block do |rets, http, headers|
|
133
|
-
# a1 = Digest::MD5.hexdigest([headers["User-Agent"], @password].join(":"))
|
134
|
-
# if headers.has_key?("Cookie") then
|
135
|
-
# cookie = headers["Cookie"].split(";").map(&:strip).select {|c| c =~ /rets-session-id/i}
|
136
|
-
# cookie = cookie ? cookie.split("=").last : ""
|
137
|
-
# else
|
138
|
-
# cookie = ""
|
139
|
-
# end
|
140
|
-
#
|
141
|
-
# parts = [a1, "", cookie, headers["RETS-Version"]]
|
142
|
-
# headers["RETS-UA-Authorization"] = "Digest " + Digest::MD5.hexdigest(parts.join(":"))
|
143
|
-
# end
|
144
|
-
def set_pre_request_block(&block)
|
145
|
-
@pre_request_block = block
|
146
|
-
end
|
147
|
-
|
148
|
-
# We only allow external read access to URLs because they are internally set based on the
|
149
|
-
# results of various queries.
|
150
|
-
def urls
|
151
|
-
@urls
|
152
|
-
end
|
153
|
-
|
154
|
-
def set_header(name, value)
|
155
|
-
if value.nil? then
|
156
|
-
@headers.delete(name)
|
157
|
-
else
|
158
|
-
@headers[name] = value
|
159
|
-
end
|
160
|
-
|
161
|
-
logger.debug("Set header '#{name}' to '#{value}'") if logger
|
162
|
-
end
|
163
|
-
|
164
|
-
def get_header(name)
|
165
|
-
@headers[name]
|
166
|
-
end
|
167
|
-
|
168
|
-
def user_agent=(name)
|
169
|
-
set_header('User-Agent', name)
|
170
|
-
end
|
171
|
-
|
172
|
-
def user_agent
|
173
|
-
get_header('User-Agent')
|
174
|
-
end
|
175
|
-
|
176
|
-
def rets_version=(version)
|
177
|
-
if (SUPPORTED_RETS_VERSIONS.include? version)
|
178
|
-
set_header('RETS-Version', "RETS/#{version}")
|
179
|
-
else
|
180
|
-
raise Unsupported.new("The client does not support RETS version '#{version}'.")
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
def rets_version
|
185
|
-
(get_header('RETS-Version') || "").gsub("RETS/", "")
|
186
|
-
end
|
187
|
-
|
188
|
-
def request_method=(method)
|
189
|
-
@request_method = method
|
190
|
-
end
|
191
|
-
|
192
|
-
def request_method
|
193
|
-
# Basic Authentication
|
194
|
-
#
|
195
|
-
@request_method
|
196
|
-
end
|
197
|
-
|
198
|
-
# Provide backwards-compatible get/set methods.
|
199
|
-
alias_method :set_user_agent, :user_agent=
|
200
|
-
alias_method :get_user_agent, :user_agent
|
201
|
-
alias_method :set_request_method, :request_method=
|
202
|
-
alias_method :get_request_method, :request_method
|
203
|
-
alias_method :set_rets_version, :rets_version=
|
204
|
-
alias_method :get_rets_version, :rets_version
|
205
|
-
|
206
|
-
#### RETS Transaction Methods ####
|
207
|
-
#
|
208
|
-
# Most of these transaction methods mirror the RETS specification methods, so if you are
|
209
|
-
# unsure what they mean, you should check the RETS specification. The latest version can be
|
210
|
-
# found at http://www.rets.org
|
211
|
-
|
212
|
-
# Attempts to log into the server using the provided username and password.
|
213
|
-
#
|
214
|
-
# If called with a block, the results of the login action are yielded,
|
215
|
-
# and logout is called when the block returns. In that case, #login
|
216
|
-
# returns the block's value. If called without a block, returns the
|
217
|
-
# result.
|
218
|
-
#
|
219
|
-
# As specified in the RETS specification, the Action URL is called and
|
220
|
-
# the results made available in the #secondary_results accessor of the
|
221
|
-
# results object.
|
222
|
-
def login(username, password) #:yields: login_results
|
223
|
-
@username = username
|
224
|
-
@password = password
|
225
|
-
|
226
|
-
# We are required to set the Accept header to this by the RETS 1.5 specification.
|
227
|
-
set_header('Accept', '*/*')
|
228
|
-
|
229
|
-
response = request(@urls['Login'])
|
230
|
-
|
231
|
-
# Parse response to get other URLS
|
232
|
-
results = @response_parser.parse_key_value(response.body)
|
233
|
-
|
234
|
-
if (results.success?)
|
235
|
-
CAPABILITY_LIST.each do |capability|
|
236
|
-
next unless results.response[capability]
|
237
|
-
|
238
|
-
uri = URI.parse(results.response[capability])
|
239
|
-
|
240
|
-
if uri.absolute?
|
241
|
-
@urls[capability] = uri
|
242
|
-
else
|
243
|
-
base = @urls['Login'].clone
|
244
|
-
base.path = results.response[capability]
|
245
|
-
@urls[capability] = base
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
logger.debug("Capability URL List: #{@urls.inspect}") if logger
|
250
|
-
else
|
251
|
-
raise LoginError.new(response.message + "(#{results.reply_code}: #{results.reply_text})")
|
252
|
-
end
|
253
|
-
|
254
|
-
# Perform the mandatory get request on the action URL.
|
255
|
-
results.secondary_response = perform_action_url
|
256
|
-
|
257
|
-
# We only yield
|
258
|
-
if block_given?
|
259
|
-
begin
|
260
|
-
yield results
|
261
|
-
ensure
|
262
|
-
self.logout
|
263
|
-
end
|
264
|
-
else
|
265
|
-
results
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
# Logs out of the RETS server.
|
270
|
-
def logout()
|
271
|
-
# If no logout URL is provided, then we assume that logout is not necessary (not to
|
272
|
-
# mention impossible without a URL). We don't throw an exception, though, but we might
|
273
|
-
# want to if this becomes an issue in the future.
|
274
|
-
|
275
|
-
request(@urls['Logout']) if @urls['Logout']
|
276
|
-
end
|
277
|
-
|
278
|
-
# Requests Metadata from the server. An optional type and id can be specified to request
|
279
|
-
# subsets of the Metadata. Please see the RETS specification for more details on this.
|
280
|
-
# The format variable tells the server which format to return the Metadata in. Unless you
|
281
|
-
# need the raw metadata in a specified format, you really shouldn't specify the format.
|
282
|
-
#
|
283
|
-
# If called with a block, yields the results and returns the value of the block, or
|
284
|
-
# returns the metadata directly.
|
285
|
-
def get_metadata(type = 'METADATA-SYSTEM', id = '*')
|
286
|
-
xml = download_metadata(type, id)
|
287
|
-
|
288
|
-
result = @response_parser.parse_metadata(xml, @format)
|
289
|
-
|
290
|
-
if block_given?
|
291
|
-
yield result
|
292
|
-
else
|
293
|
-
result
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
def download_metadata(type, id)
|
298
|
-
header = {
|
299
|
-
'Accept' => 'text/xml,text/plain;q=0.5'
|
300
|
-
}
|
301
|
-
|
302
|
-
data = {
|
303
|
-
'Type' => type,
|
304
|
-
'ID' => id,
|
305
|
-
'Format' => @format
|
306
|
-
}
|
307
|
-
|
308
|
-
request(@urls['GetMetadata'], data, header).body
|
309
|
-
end
|
310
|
-
|
311
|
-
# Performs a GetObject transaction on the server. For details on the arguments, please see
|
312
|
-
# the RETS specification on GetObject requests.
|
313
|
-
#
|
314
|
-
# This method either returns an Array of DataObject instances, or yields each DataObject
|
315
|
-
# as it is created. If a block is given, the number of objects yielded is returned.
|
316
|
-
def get_object(resource, type, id, location = 0) #:yields: data_object
|
317
|
-
header = {
|
318
|
-
'Accept' => mimemap.keys.join(',')
|
319
|
-
}
|
320
|
-
|
321
|
-
data = {
|
322
|
-
'Resource' => resource,
|
323
|
-
'Type' => type,
|
324
|
-
'ID' => id,
|
325
|
-
'Location' => location.to_s
|
326
|
-
}
|
327
|
-
|
328
|
-
response = request(@urls['GetObject'], data, header)
|
329
|
-
results = block_given? ? 0 : []
|
330
|
-
|
331
|
-
if response['content-type'] && response['content-type'].include?('text/xml')
|
332
|
-
# This probably means that there was an error.
|
333
|
-
# Response parser will likely raise an exception.
|
334
|
-
rr = @response_parser.parse_object_response(response.body)
|
335
|
-
return rr
|
336
|
-
elsif response['content-type'] && response['content-type'].include?('multipart/parallel')
|
337
|
-
content_type = process_content_type(response['content-type'])
|
338
|
-
|
339
|
-
# TODO: log this
|
340
|
-
# puts "SPLIT ON #{content_type['boundary']}"
|
341
|
-
boundary = content_type['boundary']
|
342
|
-
if boundary =~ /\s*'([^']*)\s*/
|
343
|
-
boundary = $1
|
344
|
-
end
|
345
|
-
parts = response.body.split("\r\n--#{boundary}")
|
346
|
-
|
347
|
-
parts.shift # Get rid of the initial boundary
|
348
|
-
|
349
|
-
# TODO: log this
|
350
|
-
# puts "GOT PARTS #{parts.length}"
|
351
|
-
|
352
|
-
parts.each do |part|
|
353
|
-
(raw_header, raw_data) = part.split("\r\n\r\n")
|
354
|
-
|
355
|
-
# TODO: log this
|
356
|
-
# puts raw_data.nil?
|
357
|
-
next unless raw_data
|
358
|
-
|
359
|
-
data_header = process_header(raw_header)
|
360
|
-
data_object = DataObject.new(data_header, raw_data)
|
361
|
-
|
362
|
-
if block_given?
|
363
|
-
yield data_object
|
364
|
-
results += 1
|
365
|
-
else
|
366
|
-
results << data_object
|
367
|
-
end
|
368
|
-
end
|
369
|
-
else
|
370
|
-
info = {
|
371
|
-
'content-type' => response['content-type'], # Compatibility shim. Deprecated.
|
372
|
-
'Content-Type' => response['content-type'],
|
373
|
-
'Object-ID' => response['Object-ID'],
|
374
|
-
'Content-ID' => response['Content-ID']
|
375
|
-
}
|
376
|
-
|
377
|
-
if response['Transfer-Encoding'].to_s.downcase == "chunked" || response['Content-Length'].to_i > 100 then
|
378
|
-
data_object = DataObject.new(info, response.body)
|
379
|
-
if block_given?
|
380
|
-
yield data_object
|
381
|
-
results += 1
|
382
|
-
else
|
383
|
-
results << data_object
|
384
|
-
end
|
385
|
-
end
|
386
|
-
end
|
387
|
-
|
388
|
-
results
|
389
|
-
end
|
390
|
-
|
391
|
-
# Peforms a RETS search transaction. Again, please see the RETS specification for details
|
392
|
-
# on what these parameters mean. The options parameter takes a hash of options that will
|
393
|
-
# added to the search statement.
|
394
|
-
def search(search_type, klass, query, options = false)
|
395
|
-
header = {}
|
396
|
-
|
397
|
-
# Required Data
|
398
|
-
data = {
|
399
|
-
'SearchType' => search_type,
|
400
|
-
'Class' => klass,
|
401
|
-
'Query' => query,
|
402
|
-
'QueryType' => 'DMQL2',
|
403
|
-
'Format' => format,
|
404
|
-
'Count' => '0'
|
405
|
-
}
|
406
|
-
|
407
|
-
# Options
|
408
|
-
#--
|
409
|
-
# We might want to switch this to merge!, but I've kept it like this for now because it
|
410
|
-
# explicitly casts each value as a string prior to performing the search, so we find out now
|
411
|
-
# if can't force a value into the string context. I suppose it doesn't really matter when
|
412
|
-
# that happens, though...
|
413
|
-
#++
|
414
|
-
options.each { |k,v| data[k] = v.to_s } if options
|
415
|
-
|
416
|
-
response = request(@urls['Search'], data, header)
|
417
|
-
|
418
|
-
results = @response_parser.parse_results(response.body, @format)
|
419
|
-
|
420
|
-
if block_given?
|
421
|
-
yield results
|
422
|
-
else
|
423
|
-
return results
|
424
|
-
end
|
425
|
-
end
|
426
|
-
|
427
|
-
def count(search_type, klass, query)
|
428
|
-
header = {}
|
429
|
-
data = {
|
430
|
-
'SearchType' => search_type,
|
431
|
-
'Class' => klass,
|
432
|
-
'Query' => query,
|
433
|
-
'QueryType' => 'DMQL2',
|
434
|
-
'Format' => format,
|
435
|
-
'Count' => '2'
|
436
|
-
}
|
437
|
-
response = request(@urls['Search'], data, header)
|
438
|
-
result = @response_parser.parse_count(response.body)
|
439
|
-
return result
|
440
|
-
end
|
441
|
-
|
442
|
-
private
|
443
|
-
|
444
|
-
# Copied from http.rb
|
445
|
-
def basic_encode(account, password)
|
446
|
-
'Basic ' + ["#{account}:#{password}"].pack('m').delete("\r\n")
|
447
|
-
end
|
448
|
-
|
449
|
-
# XXX: This is crap. It does not properly handle quotes.
|
450
|
-
def process_content_type(text)
|
451
|
-
content = {}
|
452
|
-
|
453
|
-
field_start = text.index(';')
|
454
|
-
|
455
|
-
content['content-type'] = text[0 ... field_start].strip
|
456
|
-
fields = text[field_start..-1]
|
457
|
-
|
458
|
-
parts = text.split(';')
|
459
|
-
|
460
|
-
parts.each do |part|
|
461
|
-
(name, value) = part.gsub(/\"/, '').split('=')
|
462
|
-
|
463
|
-
content[name.strip] = value ? value.strip : value
|
464
|
-
end
|
465
|
-
|
466
|
-
content
|
467
|
-
end
|
468
|
-
|
469
|
-
# Processes the HTTP header
|
470
|
-
#--
|
471
|
-
# Could we switch over to using CGI for this?
|
472
|
-
#++
|
473
|
-
def process_header(raw)
|
474
|
-
header = {}
|
475
|
-
|
476
|
-
raw.each do |line|
|
477
|
-
(name, value) = line.split(':')
|
478
|
-
|
479
|
-
header[name.strip] = value.strip if name && value
|
480
|
-
end
|
481
|
-
|
482
|
-
header
|
483
|
-
end
|
484
|
-
|
485
|
-
# Given a hash, it returns a URL encoded query string.
|
486
|
-
def create_query_string(hash)
|
487
|
-
parts = hash.map {|key,value| "#{CGI.escape(key)}=#{CGI.escape(value)}"}
|
488
|
-
return parts.join('&')
|
489
|
-
end
|
490
|
-
|
491
|
-
# This is the primary transaction method, which the other public methods make use of.
|
492
|
-
# Given a url for the transaction (endpoint) it makes a request to the RETS server.
|
493
|
-
#
|
494
|
-
#--
|
495
|
-
# This needs to be better documented, but for now please see the public transaction methods
|
496
|
-
# for how to make use of this method.
|
497
|
-
#++
|
498
|
-
def request(url, data = {}, header = {}, method = @request_method, retry_auth = DEFAULT_RETRY)
|
499
|
-
response = ''
|
500
|
-
|
501
|
-
@semaphore.lock
|
502
|
-
|
503
|
-
http = Net::HTTP.new(url.host, url.port)
|
504
|
-
http.read_timeout = 600
|
505
|
-
|
506
|
-
if logger && logger.debug?
|
507
|
-
http.set_debug_output HTTPDebugLogger.new(logger)
|
508
|
-
end
|
509
|
-
|
510
|
-
http.start do |http|
|
511
|
-
begin
|
512
|
-
uri = url.path
|
513
|
-
|
514
|
-
if ! data.empty? && method == METHOD_GET
|
515
|
-
uri += "?#{create_query_string(data)}"
|
516
|
-
end
|
517
|
-
|
518
|
-
headers = @headers
|
519
|
-
headers.merge(header) unless header.empty?
|
520
|
-
|
521
|
-
@pre_request_block.call(self, http, headers) if @pre_request_block
|
522
|
-
|
523
|
-
logger.debug(headers.inspect) if logger
|
524
|
-
|
525
|
-
@semaphore.unlock
|
526
|
-
|
527
|
-
post_data = data.map {|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&') if method == METHOD_POST
|
528
|
-
response = method == METHOD_POST ? http.post(uri, post_data, headers) :
|
529
|
-
http.get(uri, headers)
|
530
|
-
|
531
|
-
@semaphore.lock
|
532
|
-
|
533
|
-
if response.code == '401'
|
534
|
-
# Authentication is required
|
535
|
-
raise AuthRequired
|
536
|
-
elsif response.code.to_i >= 300
|
537
|
-
# We have a non-successful response that we cannot handle
|
538
|
-
@semaphore.unlock if @semaphore.locked?
|
539
|
-
raise HTTPError.new(response)
|
540
|
-
else
|
541
|
-
cookies = []
|
542
|
-
if set_cookies = response.get_fields('set-cookie') then
|
543
|
-
set_cookies.each do |cookie|
|
544
|
-
cookies << cookie.split(";").first
|
545
|
-
end
|
546
|
-
end
|
547
|
-
set_header('Cookie', cookies.join("; ")) unless cookies.empty?
|
548
|
-
set_header('RETS-Session-ID', response['RETS-Session-ID']) if response['RETS-Session-ID']
|
549
|
-
end
|
550
|
-
rescue AuthRequired
|
551
|
-
@nc += 1
|
552
|
-
|
553
|
-
if retry_auth > 0
|
554
|
-
retry_auth -= 1
|
555
|
-
auth = Auth.authenticate(response,
|
556
|
-
@username,
|
557
|
-
@password,
|
558
|
-
url.path,
|
559
|
-
method,
|
560
|
-
@headers['RETS-Request-ID'],
|
561
|
-
get_user_agent,
|
562
|
-
@nc)
|
563
|
-
set_header('Authorization', auth)
|
564
|
-
retry
|
565
|
-
else
|
566
|
-
@semaphore.unlock if @semaphore.locked?
|
567
|
-
raise LoginError.new(response.message)
|
568
|
-
end
|
569
|
-
end
|
570
|
-
|
571
|
-
logger.debug(response.body) if logger
|
572
|
-
end
|
573
|
-
|
574
|
-
@semaphore.unlock if @semaphore.locked?
|
575
|
-
|
576
|
-
return response
|
577
|
-
end
|
578
|
-
|
579
|
-
# If an action URL is present in the URL capability list, it calls that action URL and returns the
|
580
|
-
# raw result. Throws a generic RETSException if it is unable to follow the URL.
|
581
|
-
def perform_action_url
|
582
|
-
begin
|
583
|
-
if @urls.has_key?('Action')
|
584
|
-
return request(@urls['Action'], {}, {}, METHOD_GET)
|
585
|
-
end
|
586
|
-
rescue
|
587
|
-
raise RETSException.new("Unable to follow action URL: '#{$!}'.")
|
588
|
-
end
|
589
|
-
end
|
590
|
-
|
591
|
-
# Provides a proxy class to allow for net/http to log its debug to the logger.
|
592
|
-
class HTTPDebugLogger
|
593
|
-
def initialize(logger)
|
594
|
-
@logger = logger
|
595
|
-
end
|
596
|
-
|
597
|
-
def <<(data)
|
598
|
-
@logger.debug(data)
|
599
|
-
end
|
600
|
-
end
|
601
|
-
|
602
|
-
#### Exceptions ####
|
603
|
-
|
604
|
-
# This exception should be thrown when a generic client error is encountered.
|
605
|
-
class ClientException < Exception
|
606
|
-
end
|
607
|
-
|
608
|
-
# This exception should be thrown when there is an error with the parser, which is
|
609
|
-
# considered a subcomponent of the RETS client. It also includes the XML data that
|
610
|
-
# that was being processed at the time of the exception.
|
611
|
-
class ParserException < ClientException
|
612
|
-
attr_accessor :file
|
613
|
-
end
|
614
|
-
|
615
|
-
# The client does not currently support a specified action.
|
616
|
-
class Unsupported < ClientException
|
617
|
-
end
|
618
|
-
|
619
|
-
# The HTTP response returned by the server indicates that there was an error processing
|
620
|
-
# the request and the client cannot continue on its own without intervention.
|
621
|
-
class HTTPError < ClientException
|
622
|
-
attr_accessor :http_response
|
623
|
-
|
624
|
-
# Takes a HTTPResponse object
|
625
|
-
def initialize(http_response)
|
626
|
-
self.http_response = http_response
|
627
|
-
end
|
628
|
-
|
629
|
-
# Shorthand for calling HTTPResponse#code
|
630
|
-
def code
|
631
|
-
http_response.code
|
632
|
-
end
|
633
|
-
|
634
|
-
# Shorthand for calling HTTPResponse#message
|
635
|
-
def message
|
636
|
-
http_response.message
|
637
|
-
end
|
638
|
-
|
639
|
-
# Returns the RETS specification message for the HTTP response code
|
640
|
-
def rets_message
|
641
|
-
Client::RETS_HTTP_MESSAGES[code]
|
642
|
-
end
|
643
|
-
|
644
|
-
def to_s
|
645
|
-
"#{code} #{message}: #{rets_message}"
|
646
|
-
end
|
647
|
-
end
|
648
|
-
|
649
|
-
# A general RETS level exception was encountered. This would include HTTP and RETS
|
650
|
-
# specification level errors as well as informative mishaps such as authentication being
|
651
|
-
# required for access.
|
652
|
-
class RETSException < RuntimeError
|
653
|
-
end
|
654
|
-
|
655
|
-
# There was a problem with logging into the RETS server.
|
656
|
-
class LoginError < RETSException
|
657
|
-
end
|
658
|
-
|
659
|
-
# For internal client use only, it is thrown when the a RETS request is made but a password
|
660
|
-
# is prompted for.
|
661
|
-
class AuthRequired < RETSException
|
662
|
-
end
|
663
|
-
|
664
|
-
# A RETS transaction failed
|
665
|
-
class RETSTransactionException < RETSException; end
|
666
|
-
|
667
|
-
# Search Transaction Exceptions
|
668
|
-
class UnknownQueryFieldException < RETSTransactionException; end
|
669
|
-
class NoRecordsFoundException < RETSTransactionException; end
|
670
|
-
class InvalidSelectException < RETSTransactionException; end
|
671
|
-
class MiscellaneousSearchErrorException < RETSTransactionException; end
|
672
|
-
class InvalidQuerySyntaxException < RETSTransactionException; end
|
673
|
-
class UnauthorizedQueryException < RETSTransactionException; end
|
674
|
-
class MaximumRecordsExceededException < RETSTransactionException; end
|
675
|
-
class TimeoutException < RETSTransactionException; end
|
676
|
-
class TooManyOutstandingQueriesException < RETSTransactionException; end
|
677
|
-
class DTDVersionUnavailableException < RETSTransactionException; end
|
678
|
-
|
679
|
-
# GetObject Exceptions
|
680
|
-
class InvalidResourceException < RETSTransactionException; end
|
681
|
-
class InvalidTypeException < RETSTransactionException; end
|
682
|
-
class InvalidIdentifierException < RETSTransactionException; end
|
683
|
-
class NoObjectFoundException < RETSTransactionException; end
|
684
|
-
class UnsupportedMIMETypeException < RETSTransactionException; end
|
685
|
-
class UnauthorizedRetrievalException < RETSTransactionException; end
|
686
|
-
class ResourceUnavailableException < RETSTransactionException; end
|
687
|
-
class ObjectUnavailableException < RETSTransactionException; end
|
688
|
-
class RequestTooLargeException < RETSTransactionException; end
|
689
|
-
class TimeoutException < RETSTransactionException; end
|
690
|
-
class TooManyOutstandingRequestsException < RETSTransactionException; end
|
691
|
-
class MiscellaneousErrorException < RETSTransactionException; end
|
692
|
-
|
693
|
-
EXCEPTION_TYPES = {
|
694
|
-
# Search Transaction Reply Codes
|
695
|
-
20200 => UnknownQueryFieldException,
|
696
|
-
20201 => NoRecordsFoundException,
|
697
|
-
20202 => InvalidSelectException,
|
698
|
-
20203 => MiscellaneousSearchErrorException,
|
699
|
-
20206 => InvalidQuerySyntaxException,
|
700
|
-
20207 => UnauthorizedQueryException,
|
701
|
-
20208 => MaximumRecordsExceededException,
|
702
|
-
20209 => TimeoutException,
|
703
|
-
20210 => TooManyOutstandingQueriesException,
|
704
|
-
20514 => DTDVersionUnavailableException,
|
705
|
-
|
706
|
-
# GetObject Reply Codes
|
707
|
-
20400 => InvalidResourceException,
|
708
|
-
20401 => InvalidTypeException,
|
709
|
-
20402 => InvalidIdentifierException,
|
710
|
-
20403 => NoObjectFoundException,
|
711
|
-
20406 => UnsupportedMIMETypeException,
|
712
|
-
20407 => UnauthorizedRetrievalException,
|
713
|
-
20408 => ResourceUnavailableException,
|
714
|
-
20409 => ObjectUnavailableException,
|
715
|
-
20410 => RequestTooLargeException,
|
716
|
-
20411 => TimeoutException,
|
717
|
-
20412 => TooManyOutstandingRequestsException,
|
718
|
-
20413 => MiscellaneousErrorException
|
719
|
-
|
720
|
-
}
|
721
|
-
|
722
|
-
end
|
723
|
-
end
|