Class: RETS4R::Client::Requester

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeRequester

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

#authObject

Returns the value of attribute auth


10
11
12
# File 'lib/rets4r/client/requester.rb', line 10

def auth
  @auth
end

#headersObject

Returns the value of attribute headers


10
11
12
# File 'lib/rets4r/client/requester.rb', line 10

def headers
  @headers
end

#loggerObject

Returns the value of attribute logger


10
11
12
# File 'lib/rets4r/client/requester.rb', line 10

def logger
  @logger
end

#methodObject

Returns the value of attribute method


11
12
13
# File 'lib/rets4r/client/requester.rb', line 11

def method
  @method
end

#passwordObject

Returns the value of attribute password


11
12
13
# File 'lib/rets4r/client/requester.rb', line 11

def password
  @password
end

#post_request_blockObject

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_blockObject

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

#usernameObject

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 collect_cookies(response)
  cookies = {}
  cookie_fields = response.get_fields('set-cookie') || []
  cookie_fields.each do |cookie|
    # Cookies are of the form
    #   JSESSIONID=7939CD3932648EA8F3C0D27723154039; Path=/
    # So we need to strip the path component
    key, value = cookie.split(";").first.split('=')
    cookies.store key, value
  end
  cookies
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
          cookies = collect_cookies response
          logger.debug("Recieved cookies '#{cookies.inspect}'") if logger
          set_cookies cookies
        end
      rescue AuthRequired
        if retry_auth > 0
          retry_auth -= 1
          auth.update_with_response(response)
          retry
        else
          raise LoginError.new(response.message)
        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_versionObject


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 set_cookies(cookies)
  cookie_string = cookies.map{|k,v| "#{k}=#{v}"}.join('; ')
  set_header('Cookie', cookie_string) unless cookies.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_agentObject


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