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.



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

#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.



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

def method
  @method
end

#ncObject

Returns the value of attribute nc.



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

def nc
  @nc
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
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.



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
        cookies = []
        if set_cookies = response.get_fields('set-cookie') then
          set_cookies.each do |cookie|
            cookies << cookie.split(";").first
          end
        end
        set_header('Cookie', cookies.join("; ")) unless cookies.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.message)
      end
    end

    logger.debug(response.body) if logger
  end

  return response
end

#rets_versionObject



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_agentObject



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