Skip to content

Commit

Permalink
pull out capability links
Browse files Browse the repository at this point in the history
  • Loading branch information
josephholsten committed Aug 2, 2010
1 parent 5781ef9 commit 94d4ed4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
29 changes: 11 additions & 18 deletions lib/rets4r/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'client/dataobject'
require 'client/parsers/response_parser'
require 'client/parsers/compact'
require 'rets4r/client/links'
require 'rets4r/client/exceptions'
require 'logger'
require 'webrick/httputils'
Expand Down Expand Up @@ -84,17 +85,15 @@ class Client
}

attr_accessor :mimemap, :logger
attr_reader :format
attr_reader :format, :urls

# Constructor
#
# Requires the URL to the RETS server and takes an optional output format. The output format
# determines the type of data returned by the various RETS transaction methods.
def initialize(url, format = COMPACT_FORMAT)
@format = format
@urls = {
'Login' => URI.parse(url)
}
@urls = RETS4R::Client::Links.from_login_url(url)
@nc = 0
@headers = {
'User-Agent' => DEFAULT_USER_AGENT,
Expand Down Expand Up @@ -147,12 +146,6 @@ def set_pre_request_block(&block)
@pre_request_block = block
end

# We only allow external read access to URLs because they are internally set based on the
# results of various queries.
def urls
@urls
end

def set_header(name, value)
if value.nil? then
@headers.delete(name)
Expand Down Expand Up @@ -228,7 +221,7 @@ def login(username, password) #:yields: login_results
# We are required to set the Accept header to this by the RETS 1.5 specification.
set_header('Accept', '*/*')

response = request(@urls['Login'])
response = request(@urls.login)

# Parse response to get other URLS
results = @response_parser.parse_key_value(response.body)
Expand All @@ -242,7 +235,7 @@ def login(username, password) #:yields: login_results
if uri.absolute?
@urls[capability] = uri
else
base = @urls['Login'].clone
base = @urls.login.clone
base.path = results.response[capability]
@urls[capability] = base
end
Expand Down Expand Up @@ -274,7 +267,7 @@ def logout()
# mention impossible without a URL). We don't throw an exception, though, but we might
# want to if this becomes an issue in the future.

request(@urls['Logout']) if @urls['Logout']
request(@urls.logout) if @urls.logout
end

# Requests Metadata from the server. An optional type and id can be specified to request
Expand Down Expand Up @@ -307,7 +300,7 @@ def download_metadata(type, id)
'Format' => @format
}

request(@urls['GetMetadata'], data, header).body
request(@urls.metadata, data, header).body
end

# Performs a GetObject transaction on the server. For details on the arguments, please see
Expand All @@ -329,7 +322,7 @@ def get_object(resource, type, id, location = false) #:yields: data_object
'Location' => location ? '1' : '0'
}

response = request(@urls['GetObject'], data, header)
response = request(@urls.objects, data, header)
results = block_given? ? 0 : []

if response['content-type'] && response['content-type'].include?('text/xml')
Expand Down Expand Up @@ -417,7 +410,7 @@ def search(search_type, klass, query, options = false)
#++
options.each { |k,v| data[k] = v.to_s } if options

response = request(@urls['Search'], data, header)
response = request(@urls.search, data, header)

# TODO: make parser configurable
results = RETS4R::Client::CompactNokogiriParser.new(response.body)
Expand All @@ -439,7 +432,7 @@ def count(search_type, klass, query)
'Format' => format,
'Count' => '2'
}
response = request(@urls['Search'], data, header)
response = request(@urls.search, data, header)
result = @response_parser.parse_count(response.body)
return result
end
Expand Down Expand Up @@ -571,7 +564,7 @@ def request(url, data = {}, header = {}, method = @request_method, retry_auth =
def perform_action_url
begin
if @urls.has_key?('Action')
return request(@urls['Action'], {}, {}, METHOD_GET)
return request(@urls.action, {}, {}, METHOD_GET)
end
rescue
raise RETSException.new("Unable to follow action URL: '#{$!}'.")
Expand Down
32 changes: 32 additions & 0 deletions lib/rets4r/client/links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'uri'

module RETS4R #:nodoc:
class Client #:nodoc:
class Links < Hash
attr_accessor :logger
def self.from_login_url(login_url)
links = self.new
links['Login'] = URI.parse(login_url)
links
end
def login
self['Login']
end
def logout
self['Logout']
end
def metadata
self['GetMetadata']
end
def objects
self['GetObject']
end
def search
self['Search']
end
def action
self['Action']
end
end
end
end
39 changes: 39 additions & 0 deletions test/test_client_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "test"))
require 'test_helper'
require 'rets4r/client/links'

class TestClientLinks < Test::Unit::TestCase
def setup
@links = RETS4R::Client::Links.from_login_url('http://example.com/login')
@links['Logout'] = URI.parse('http://example.com/logout')
@links['GetMetadata'] = URI.parse('http://example.com/metadata')
@links['GetObject'] = URI.parse('http://example.com/objects')
@links['Search'] = URI.parse('http://example.com/search')
@links['Action'] = URI.parse('http://example.com/action')
end
def test_should_build_from_login_url
assert_equal normalize_url('http://example.com/login'), @links['Login'].to_s
end
def test_should_access_login
assert_equal normalize_url('http://example.com/login'), @links.login.to_s
end
def test_should_access_logout
assert_equal normalize_url('http://example.com/logout'), @links.logout.to_s
end
def test_should_access_metadata
assert_equal normalize_url('http://example.com/metadata'), @links.metadata.to_s
end
def test_should_access_object
assert_equal normalize_url('http://example.com/objects'), @links.objects.to_s
end
def test_should_access_search
assert_equal normalize_url('http://example.com/search'), @links.search.to_s
end
def test_should_access_action
assert_equal normalize_url('http://example.com/action'), @links.action.to_s
end
def normalize_url(url)
URI.parse(url).to_s
end
end

0 comments on commit 94d4ed4

Please sign in to comment.