Class: OpenSRS::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/opensrs/server.rb

Overview

Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/opensrs/server.rb', line 19

def initialize(options = {})
  @server   = URI.parse(options[:server] || 'https://rr-n1-tor.opensrs.net:55443/')
  @username = options[:username]
  @password = options[:password]
  @key      = options[:key]
  @timeout  = options[:timeout]
  @open_timeout = options[:open_timeout]
  @logger   = options[:logger]
  @proxy    = URI.parse(options[:proxy]) if options[:proxy]
  @sanitize_request = options[:sanitize_request]

  OpenSRS::SanitizableString.enable_sanitization = @sanitize_request
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def key
  @key
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def open_timeout
  @open_timeout
end

#passwordObject

Returns the value of attribute password.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def proxy
  @proxy
end

#serverObject

Returns the value of attribute server.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def server
  @server
end

#timeoutObject

Returns the value of attribute timeout.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def timeout
  @timeout
end

#usernameObject

Returns the value of attribute username.



17
18
19
# File 'lib/opensrs/server.rb', line 17

def username
  @username
end

Class Method Details

.xml_processor=(name) ⇒ Object



57
58
59
60
# File 'lib/opensrs/server.rb', line 57

def self.xml_processor=(name)
  require "opensrs/xml_processor/#{name.to_s.downcase}"
  @@xml_processor = OpenSRS::XmlProcessor.const_get(name.to_s.capitalize.to_s)
end

Instance Method Details

#call(data = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opensrs/server.rb', line 33

def call(data = {})
  xml = xml_processor.build({ protocol: 'XCP' }.merge!(data))
  log('Request', xml.sanitized, data)

  begin
    response = http.post(server_path, xml, headers(xml))
    log('Response', response.body, data)
  rescue Net::HTTPBadResponse
    raise OpenSRS::BadResponse,
          'Received a bad response from OpenSRS. Please check that your IP address is added to the whitelist, and try again.'
  end

  parsed_response = xml_processor.parse(response.body)
  OpenSRS::Response.new(parsed_response, xml.sanitized, response.body)
rescue Timeout::Error => e
  raise OpenSRS::TimeoutError, e
rescue Errno::ECONNRESET, Errno::ECONNREFUSED => e
  raise OpenSRS::ConnectionError, e
end

#xml_processorObject



53
54
55
# File 'lib/opensrs/server.rb', line 53

def xml_processor
  @@xml_processor
end