Class: Clearsale::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/clearsale/connector.rb

Constant Summary collapse

NAMESPACE =
"http://www.clearsale.com.br/integration"
URLs =
{
  "homolog"    => 'http://homologacao.clearsale.com.br/integracaov2/service.asmx',
  "production" => 'https://integracao.clearsale.com.br/service.asmx'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_url, proxy = nil) ⇒ Connector

Returns a new instance of Connector.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clearsale/connector.rb', line 19

def initialize(endpoint_url, proxy=nil)
  @token = Clearsale::Config.entity_code

  namespaces = {
      'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope",
      'xmlns:xsd'  => "http://www.w3.org/2001/XMLSchema" ,
      'xmlns:xsi'  => "http://www.w3.org/2001/XMLSchema-instance" ,
      'xmlns:int'  => "http://www.clearsale.com.br/integration",
  }

  savon_options = {:endpoint => endpoint_url, :namespace => NAMESPACE,
                   :namespaces => namespaces, :convert_request_keys_to => :snakecase }

  savon_options[:proxy]  = proxy if proxy
  savon_options[:log]    = Clearsale::Config.log
  savon_options[:logger] = Clearsale::Config.logger
  savon_options[:read_timeout] = Clearsale::Config.read_timeout if Clearsale::Config.read_timeout.present?
  savon_options[:open_timeout] = Clearsale::Config.open_timeout if Clearsale::Config.open_timeout.present?

  @client = Savon.client(savon_options)
end

Class Method Details

.build(env = Clearsale::Config.env) ⇒ Object



13
14
15
16
17
# File 'lib/clearsale/connector.rb', line 13

def self.build(env = Clearsale::Config.env)
  url = URLs[env]
  proxy = Clearsale::Config.proxy
  new url, proxy
end

Instance Method Details

#append_namespace(namespace, hash) ⇒ Object



57
58
59
# File 'lib/clearsale/connector.rb', line 57

def append_namespace(namespace, hash)
  Hash[hash.map {|key, value| ["#{namespace}:#{key}", value]}]
end

#do_request(method, request) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/clearsale/connector.rb', line 41

def do_request(method, request)
  namespaced_request = append_namespace('int', request)
  arguments = namespaced_request.merge({'int:entityCode' => @token})

  response = @client.call(method, :message => arguments, :soap_action => "#{NAMESPACE}/#{method}")

  extract_response_xml(method, response.to_hash)
end

#extract_response_xml(method, response) ⇒ Object



50
51
52
53
54
55
# File 'lib/clearsale/connector.rb', line 50

def extract_response_xml(method, response)
  results = response.fetch(:"#{method.snakecase}_response", {})
  response_xml = results.fetch(:"#{method.snakecase}_result", {}).to_s

  Nori.new(:parser => :nokogiri, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }).parse(response_xml.gsub(/^<\?xml.*\?>/, ''))
end