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://www.clearsale.com.br/integracaov2/service.asmx'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_url) ⇒ Connector

Returns a new instance of Connector.



20
21
22
23
24
25
26
# File 'lib/clearsale/connector.rb', line 20

def initialize(endpoint_url)
  @token = ENV['CLEARSALE_ENTITYCODE']
  @client = Savon::Client.new do |wsdl|
    wsdl.endpoint = endpoint_url
    wsdl.namespace = NAMESPACE
  end
end

Class Method Details

.build(env = ENV['CLEARSALE_ENV']) ⇒ Object



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

def self.build(env = ENV['CLEARSALE_ENV'])
  url = ENV["CLEARSALE_URL"] || URLs[env] || URLs["homolog"]
  new url
end

Instance Method Details

#append_namespace(namespace, hash) ⇒ Object



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

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

#do_request(method, request) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clearsale/connector.rb', line 28

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

  response = @client.request(:int, method) do |soap, _, http, _|
    soap.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",
    }

    soap.env_namespace = :soap

    soap.body = arguments
    http.headers['SOAPAction'] = "#{NAMESPACE}/#{method}"
  end

  extract_response_xml(method, response.to_hash)
end

#extract_response_xml(method, response) ⇒ Object



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

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

  Nori.parse(response_xml.gsub(/^<\?xml.*\?>/, ''))
end