Class: RETS4R::Client::Requester
- Inherits:
-
Object
- Object
- RETS4R::Client::Requester
- Defined in:
- lib/rets4r/client/requester.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#method ⇒ Object
Returns the value of attribute method.
-
#nc ⇒ Object
Returns the value of attribute nc.
-
#password ⇒ Object
Returns the value of attribute password.
-
#pre_request_block ⇒ Object
Returns the value of attribute pre_request_block.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#create_query_string(hash) ⇒ Object
Given a hash, it returns a URL encoded query string.
-
#initialize ⇒ Requester
constructor
A new instance of Requester.
-
#request(url, data = {}, header = {}, method = @method, retry_auth = DEFAULT_RETRY) ⇒ Object
This is the primary transaction method, which the other public methods make use of.
- #rets_version ⇒ Object
- #rets_version=(version) ⇒ Object
- #set_header(name, value) ⇒ Object
- #user_agent ⇒ Object
- #user_agent=(name) ⇒ Object
Constructor Details
#initialize ⇒ Requester
Returns a new instance of Requester.
11 12 13 14 15 16 17 18 19 |
# File 'lib/rets4r/client/requester.rb', line 11 def initialize @nc = 0 @headers = { 'User-Agent' => DEFAULT_USER_AGENT, 'Accept' => '*/*', 'RETS-Version' => "RETS/#{DEFAULT_RETS_VERSION}", } @pre_request_block = nil end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def headers @headers end |
#logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def logger @logger end |
#method ⇒ Object
Returns the value of attribute method.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def method @method end |
#nc ⇒ Object
Returns the value of attribute nc.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def nc @nc end |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def password @password end |
#pre_request_block ⇒ Object
Returns the value of attribute pre_request_block.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def pre_request_block @pre_request_block end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def username @username end |
Instance Method Details
#create_query_string(hash) ⇒ Object
Given a hash, it returns a URL encoded query string.
56 57 58 59 |
# File 'lib/rets4r/client/requester.rb', line 56 def create_query_string(hash) parts = hash.map {|key,value| "#{CGI.escape(key)}=#{CGI.escape(value)}" unless key.nil? || value.nil?} return parts.join('&') end |
#request(url, data = {}, header = {}, method = @method, retry_auth = DEFAULT_RETRY) ⇒ Object
This is the primary transaction method, which the other public methods make use of. Given a url for the transaction (endpoint) it makes a request to the RETS server.
– This needs to be better documented, but for now please see the public transaction methods for how to make use of this method. ++
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 |
# File 'lib/rets4r/client/requester.rb', line 67 def request(url, data = {}, header = {}, method = @method, retry_auth = DEFAULT_RETRY) response = '' http = Net::HTTP.new(url.host, url.port) http.read_timeout = 600 if logger && logger.debug? http.set_debug_output HTTPDebugLogger.new(logger) end http.start do |http| begin uri = url.path if ! data.empty? && method == METHOD_GET uri += "?#{create_query_string(data)}" end headers = @headers headers.merge(header) unless header.empty? @pre_request_block.call(self, http, headers) if @pre_request_block logger.debug(headers.inspect) if logger post_data = data.map {|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&') if method == METHOD_POST response = method == METHOD_POST ? http.post(uri, post_data, headers) : http.get(uri, headers) if response.code == '401' # Authentication is required raise AuthRequired elsif response.code.to_i >= 300 # We have a non-successful response that we cannot handle raise HTTPError.new(response) else = [] if = response.get_fields('set-cookie') then .each do || << .split(";").first end end set_header('Cookie', .join("; ")) unless .empty? # totally wrong. session id is only ever under the Cookie header #set_header('RETS-Session-ID', response['RETS-Session-ID']) if response['RETS-Session-ID'] set_header('RETS-Session-ID',nil) end rescue AuthRequired @nc += 1 if retry_auth > 0 retry_auth -= 1 auth = Auth.authenticate(response, @username, @password, url.path, method, @headers['RETS-Request-ID'], @headers['User-Agent'], @nc) set_header('Authorization', auth) retry else raise LoginError.new(response.) end end logger.debug(response.body) if logger end return response end |
#rets_version ⇒ Object
37 38 39 |
# File 'lib/rets4r/client/requester.rb', line 37 def rets_version (@headers['RETS-Version'] || "").gsub("RETS/", "") end |
#rets_version=(version) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/rets4r/client/requester.rb', line 29 def rets_version=(version) if (SUPPORTED_RETS_VERSIONS.include? version) set_header('RETS-Version', "RETS/#{version}") else raise Unsupported.new("The client does not support RETS version '#{version}'.") end end |
#set_header(name, value) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/rets4r/client/requester.rb', line 41 def set_header(name, value) if value.nil? then @headers.delete(name) else @headers[name] = value end logger.debug("Set header '#{name}' to '#{value}'") if logger end |
#user_agent ⇒ Object
21 22 23 |
# File 'lib/rets4r/client/requester.rb', line 21 def user_agent @headers['User-Agent'] end |
#user_agent=(name) ⇒ Object
25 26 27 |
# File 'lib/rets4r/client/requester.rb', line 25 def user_agent=(name) set_header('User-Agent', name) end |