Class: RETS4R::Client::Requester
- Inherits:
-
Object
- Object
- RETS4R::Client::Requester
- Defined in:
- lib/rets4r/client/requester.rb
Constant Summary collapse
- DEFAULT_USER_AGENT =
"rets4r/#{::RETS4R::VERSION}"
- DEFAULT_RETS_VERSION =
'1.7'
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#method ⇒ Object
Returns the value of attribute method.
-
#password ⇒ Object
Returns the value of attribute password.
-
#post_request_block ⇒ Object
Returns the value of attribute post_request_block.
-
#pre_request_block ⇒ Object
Returns the value of attribute pre_request_block.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #collect_cookies(response) ⇒ Object
-
#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_cookies(cookies) ⇒ Object
- #set_header(name, value) ⇒ Object
- #user_agent ⇒ Object
- #user_agent=(name) ⇒ Object
Constructor Details
#initialize ⇒ Requester
Returns a new instance of Requester.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rets4r/client/requester.rb', line 13 def initialize @headers = { 'User-Agent' => DEFAULT_USER_AGENT, 'Accept' => '*/*', 'RETS-Version' => "RETS/#{DEFAULT_RETS_VERSION}", } @pre_request_block = nil @post_request_block = nil @method = METHOD_GET @auth = RETS4R::Auth.new end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def auth @auth end |
#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.
11 12 13 |
# File 'lib/rets4r/client/requester.rb', line 11 def method @method end |
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/rets4r/client/requester.rb', line 11 def password @password end |
#post_request_block ⇒ Object
Returns the value of attribute post_request_block.
10 11 12 |
# File 'lib/rets4r/client/requester.rb', line 10 def post_request_block @post_request_block 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.
11 12 13 |
# File 'lib/rets4r/client/requester.rb', line 11 def username @username end |
Instance Method Details
#collect_cookies(response) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rets4r/client/requester.rb', line 150 def (response) = {} = response.get_fields('set-cookie') || [] .each do || # Cookies are of the form # JSESSIONID=7939CD3932648EA8F3C0D27723154039; Path=/ # So we need to strip the path component key, value = .split(";").first.split('=') .store key, value end end |
#create_query_string(hash) ⇒ Object
Given a hash, it returns a URL encoded query string.
72 73 74 75 |
# File 'lib/rets4r/client/requester.rb', line 72 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. ++
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 |
# File 'lib/rets4r/client/requester.rb', line 83 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 begin http.start do |sess| 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, sess, headers) if @pre_request_block logger.debug("Sending headers #{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 auth.update(url.path, method, @headers['RETS-Request-ID']) set_header('Authorization', auth.to_s) response = method == METHOD_POST ? sess.post(uri, post_data, headers) : sess.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 = response logger.debug("Recieved cookies '#{cookies.inspect}'") if logger end rescue AuthRequired if retry_auth > 0 retry_auth -= 1 auth.update_with_response(response) retry else raise LoginError.new(response.) end end logger.debug("Recieved headers #{response.to_hash.inspect}") if logger logger.debug("Recieved response #{response.body}") if logger end rescue raise ClientException.new("Error connecting to #{url.host}:#{url.port}\n#{$!}") end @post_request_block.call(self, http, headers) if @post_request_block return response end |
#rets_version ⇒ Object
57 58 59 |
# File 'lib/rets4r/client/requester.rb', line 57 def rets_version (@headers['RETS-Version'] || "").gsub("RETS/", "") end |
#rets_version=(version) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rets4r/client/requester.rb', line 49 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_cookies(cookies) ⇒ Object
163 164 165 166 |
# File 'lib/rets4r/client/requester.rb', line 163 def () = .map{|k,v| "#{k}=#{v}"}.join('; ') set_header('Cookie', ) unless .empty? end |
#set_header(name, value) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/rets4r/client/requester.rb', line 61 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
25 26 27 |
# File 'lib/rets4r/client/requester.rb', line 25 def user_agent @headers['User-Agent'] end |
#user_agent=(name) ⇒ Object
29 30 31 32 |
# File 'lib/rets4r/client/requester.rb', line 29 def user_agent=(name) auth.user_agent = name set_header('User-Agent', name) end |