Sha256: 90a3ec80e0c6b087bc3f746c49e53283b5ba6265227b194cbb90d6d6e2beb5ae

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'savon'
require 'nori'

Nori.parser = :nokogiri

module Clearsale
  class Connector
    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'
    }

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

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

    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

    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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clearsale-0.1.6 lib/clearsale/connector.rb
clearsale-0.1.5 lib/clearsale/connector.rb
clearsale-0.1.4 lib/clearsale/connector.rb