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