Class: RETS::Client

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

Constant Summary collapse

URL_KEYS =
{:getobject => true, :login => true, :logout => true, :search => true, :getmetadata => true}

Class Method Summary collapse

Class Method Details

.login(args) ⇒ RETS::Base::Core

Attempts to login to a RETS server.

Parameters:

  • args (Hash)

Options Hash (args):

  • :url (String)

    Login URL for the RETS server

  • :username (String)

    Username to authenticate with

  • :password (String)

    Password to authenticate with

  • :auth_mode (Symbol, Optional)

    When set to :basic will automatically use HTTP Basic authentication, skips a discovery request when initially connecting

  • :useragent (Hash, Optional)

    Only necessary for User Agent authentication

    • :name [String, Optional] - Name to set the User-Agent to

    • :password [String, Optional] - Password to use for RETS-UA-Authorization

  • :rets_version (String, Optional)

    Forces RETS-UA-Authorization on the first request if this and useragent name/password are set. Can be auto detected, but usually lets you bypass 1 - 2 additional authentication requests initially.

  • :http (Hash, Optional)

    Additional configuration for the HTTP requests

    • :verify_mode [Integer, Optional] How to verify the SSL certificate when connecting through HTTPS, either OpenSSL::SSL::VERIFY_PEER or OpenSSL::SSL::VERIFY_NONE, defaults to OpenSSL::SSL::VERIFY_NONE

    • :ca_file [String, Optional] Path to the CA certification file in PEM format

    • :ca_path [String, Optional] Path to the directory containing CA certifications in PEM format

Returns:

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rets/client.rb', line 30

def self.(args)
  raise ArgumentError, "No URL passed" unless args[:url]

  urls = {:login => URI.parse(args[:url])}
  raise ArgumentError, "Invalid URL passed" unless urls[:login].is_a?(URI::HTTP)

  base_url = urls[:login].to_s
  base_url.gsub!(urls[:login].path, "") if urls[:login].path

  http = RETS::HTTP.new(args)
  http.request(:url => urls[:login], :check_response => true) do |response|
    rets_attr = Nokogiri::XML(response.body).xpath("//RETS")
    if rets_attr.empty?
      raise RETS::ResponseError, "Does not seem to be a RETS server."
    end

    rets_attr.first.content.split("\n").each do |row|
      key, value = row.split("=", 2)
      next unless key and value

      key, value = key.downcase.strip.to_sym, value.strip

      if URL_KEYS[key]
        # In case it's a relative path and doesn't include the domain
        if value =~ /(http|www)/
          urls[key] = URI.parse(value)
        else
          key_url = URI.parse(urls[:login].to_s)
          key_url.path = value
          urls[key] = key_url
        end
      end
    end
  end

  http. = urls[:login]
   #   puts "URLS: #{urls.to_s}"
  RETS::Base::Core.new(http, urls)
end