Class: TecDoc::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
# File 'lib/tec_doc/client.rb', line 5

def initialize(options = {})
  self.provider = options[:provider]
  self.country  = options[:country]
  self.connection = Savon::Client.new do |wsdl, http|
    proxy = options[:proxy] || ENV['http_proxy']
    http.proxy = proxy if proxy
  end
  self.mode = options[:mode] || :live
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



3
4
5
# File 'lib/tec_doc/client.rb', line 3

def connection
  @connection
end

#countryObject

Returns the value of attribute country.



3
4
5
# File 'lib/tec_doc/client.rb', line 3

def country
  @country
end

#loggerObject

Returns the logger. Defaults to an instance of Logger writing to STDOUT.



52
53
54
# File 'lib/tec_doc/client.rb', line 52

def logger
  @logger ||= ::Logger.new STDOUT
end

#modeObject

Returns the value of attribute mode.



46
47
48
# File 'lib/tec_doc/client.rb', line 46

def mode
  @mode
end

#providerObject

Returns the value of attribute provider.



3
4
5
# File 'lib/tec_doc/client.rb', line 3

def provider
  @provider
end

Instance Method Details

#log(operation, options) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/tec_doc/client.rb', line 56

def log(operation, options)
  t = Time.now
  results = yield
  duration = 1000.0 * (Time.now - t)
  logger.info "TecDoc: #{operation.inspect} #{options.inspect} #{'(%.1fms)' % duration}"
  results
end

#request(operation, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tec_doc/client.rb', line 15

def request(operation, options = {})
  log operation, options do
    response = connection.request(operation) do
      soap.body = { :in => { :provider => provider }.merge(options) }
    end
    # Parse errors
    status_node = response.doc.xpath("//status").first
    if status_node.text != "200"
      status_text_node = response.doc.xpath("//statusText").first
      raise Error.new(status_text_node.text)
    end
    # Parse the document
    response.doc.xpath("//data/array/array").map do |node|
      node_to_hash(node)
    end
    # response
  end
end