Class: Rets::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rets/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http, options, logger, login_url) ⇒ HttpClient

Returns a new instance of HttpClient.



5
6
7
8
9
10
11
# File 'lib/rets/http_client.rb', line 5

def initialize(http, options, logger, )
  @http = http
  @options = options
  @logger = logger
  @login_url = 
  @options.fetch(:ca_certs, []).each {|c| @http.ssl_config.add_trust_ca(c) }
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



3
4
5
# File 'lib/rets/http_client.rb', line 3

def http
  @http
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/rets/http_client.rb', line 3

def logger
  @logger
end

#login_urlObject (readonly)

Returns the value of attribute login_url.



3
4
5
# File 'lib/rets/http_client.rb', line 3

def 
  @login_url
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/rets/http_client.rb', line 3

def options
  @options
end

Instance Method Details



84
85
86
87
88
89
# File 'lib/rets/http_client.rb', line 84

def http_cookie(name)
  http.cookies.each do |c|
    return c.value if c.name.downcase == name.downcase && c.match?(URI.parse())
  end
  nil
end

#http_get(url, params = nil, extra_headers = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rets/http_client.rb', line 13

def http_get(url, params=nil, extra_headers={})
  http.set_auth(url, options[:username], options[:password])
  headers = extra_headers.merge(rets_extra_headers)
  res = nil
  log_http_traffic("POST", url, params, headers) do
    res = http.get(url, params, headers)
  end
  Client::ErrorChecker.check(res)
  res
end

#http_post(url, params, extra_headers = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rets/http_client.rb', line 24

def http_post(url, params, extra_headers = {})
  http.set_auth(url, options[:username], options[: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
  Client::ErrorChecker.check(res)
  res
end

#log_http_traffic(method, url, params, headers, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rets/http_client.rb', line 35

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_headersObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rets/http_client.rb', line 65

def rets_extra_headers
  user_agent = options[:agent] || "Client/1.0"
  rets_version = options[:version] || "RETS/1.7.2"

  headers = {
    "User-Agent"   => user_agent,
    "RETS-Version" => rets_version
  }

  if options[:ua_password]
    up = Digest::MD5.hexdigest "#{user_agent}:#{options[:ua_password]}"
    session_id = http_cookie('RETS-Session-ID') || ''
    digest = Digest::MD5.hexdigest "#{up}::#{session_id}:#{rets_version}"
    headers.merge!("RETS-UA-Authorization" => "Digest #{digest}")
  end

  headers
end


55
56
57
58
59
60
61
62
63
# File 'lib/rets/http_client.rb', line 55

def save_cookie_store(force=nil)
  if options[:cookie_store]
    if force
      @http.cookie_manager.save_all_cookies(true, true, true)
    else
      @http.save_cookie_store
    end
  end
end