Class: Rets::HttpClient
- Inherits:
-
Object
- Object
- Rets::HttpClient
- Defined in:
- lib/rets/http_client.rb
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#login_url ⇒ Object
readonly
Returns the value of attribute login_url.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #http_cookie(name) ⇒ Object
- #http_get(url, params = nil, extra_headers = {}) ⇒ Object
- #http_post(url, params, extra_headers = {}) ⇒ Object
-
#initialize(http, options, logger, login_url) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #log_http_traffic(method, url, params, headers, &block) ⇒ Object
- #rets_extra_headers ⇒ Object
- #save_cookie_store ⇒ Object
Constructor Details
#initialize(http, options, logger, login_url) ⇒ HttpClient
Returns a new instance of HttpClient.
8 9 10 11 12 13 14 |
# File 'lib/rets/http_client.rb', line 8 def initialize(http, , logger, login_url) @http = http @options = @logger = logger @login_url = login_url @options.fetch(:ca_certs, []).each {|c| @http.ssl_config.add_trust_ca(c) } end |
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the value of attribute http.
6 7 8 |
# File 'lib/rets/http_client.rb', line 6 def http @http end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/rets/http_client.rb', line 6 def logger @logger end |
#login_url ⇒ Object (readonly)
Returns the value of attribute login_url.
6 7 8 |
# File 'lib/rets/http_client.rb', line 6 def login_url @login_url end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/rets/http_client.rb', line 6 def @options end |
Class Method Details
.ensure_cookie_store_exists!(cookie_store) ⇒ Object
49 50 51 52 53 |
# File 'lib/rets/http_client.rb', line 49 def self.() unless File.exist? FileUtils.touch() end end |
.from_options(options, logger) ⇒ Object
16 17 18 19 20 21 22 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 |
# File 'lib/rets/http_client.rb', line 16 def self.(, logger) if [:http_proxy] http = HTTPClient.new(.fetch(:http_proxy)) if [:proxy_username] http.set_proxy_auth(.fetch(:proxy_username), .fetch(:proxy_password)) end else http = HTTPClient.new end if [:receive_timeout] http.receive_timeout = [:receive_timeout] end if [:cookie_store] [:cookie_store] http.([:cookie_store]) end http_client = new(http, , logger, [:login_url]) if [:http_timing_stats_collector] http_client = Rets::MeasuringHttpClient.new(http_client, .fetch(:http_timing_stats_collector), .fetch(:http_timing_stats_prefix)) end if [:lock_around_http_requests] http_client = Rets::LockingHttpClient.new(http_client, .fetch(:locker), .fetch(:lock_name), .fetch(:lock_options)) end http_client end |
Instance Method Details
#http_cookie(name) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/rets/http_client.rb', line 123 def (name) @http..(login_url).each do |c| if c.name.downcase == name.downcase return c.value end end nil end |
#http_get(url, params = nil, extra_headers = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rets/http_client.rb', line 55 def http_get(url, params=nil, extra_headers={}) http.set_auth(url, [:username], [:password]) headers = extra_headers.merge(rets_extra_headers) res = nil log_http_traffic("GET", url, params, headers) do res = http.get(url, params, headers) end Parser::ErrorChecker.check(res) res end |
#http_post(url, params, extra_headers = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rets/http_client.rb', line 66 def http_post(url, params, extra_headers = {}) http.set_auth(url, [:username], [:password]) headers = extra_headers.merge(rets_extra_headers) res = nil log_http_traffic("POST", url, params, headers) do res = http.post(url, params, headers) end Parser::ErrorChecker.check(res) res end |
#log_http_traffic(method, url, params, headers, &block) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rets/http_client.rb', line 77 def log_http_traffic(method, url, params, headers, &block) # optimization, we don't want to compute log params # if logging is off if logger.debug? logger.debug "Rets::Client >> #{method} #{url}" logger.debug "Rets::Client >> params = #{params.inspect}" logger.debug "Rets::Client >> headers = #{headers.inspect}" end res = block.call # optimization, we don't want to compute log params # if logging is off, especially when there is a loop just # for logging if logger.debug? logger.debug "Rets::Client << Status #{res.status_code}" res.headers.each { |k, v| logger.debug "Rets::Client << #{k}: #{v}" } end end |
#rets_extra_headers ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rets/http_client.rb', line 104 def rets_extra_headers user_agent = [:agent] || "Client/1.0" rets_version = [:version] || "RETS/1.7.2" headers = { "User-Agent" => user_agent, "RETS-Version" => rets_version } if [:ua_password] up = Digest::MD5.hexdigest "#{user_agent}:#{[:ua_password]}" session_id = ('RETS-Session-ID') || '' digest = Digest::MD5.hexdigest "#{up}::#{session_id}:#{rets_version}" headers.merge!("RETS-UA-Authorization" => "Digest #{digest}") end headers end |
#save_cookie_store ⇒ Object
97 98 99 100 101 102 |
# File 'lib/rets/http_client.rb', line 97 def if [:cookie_store] #save session cookies @http..(true) end end |