Sha256: 94ed1c7e19dd0f44b5b0c070c25e4a6af79e77ebffcc1e42f0912fe3c56c66ba

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "typhoeus"

module ShippingScale
  class Client
    def request(request)
      server = server(request)

      response = Typhoeus::Request.get(server, {
        timeout: ShippingScale.config.timeout,
        params: {
          "API" => request.api,
          "XML" => request.build
        }
      })

      xml = Nokogiri::XML.parse(response.body)

      if ((error = xml.search('Error')).any?)
        why = error.search("Description").children.first
        code = error.search("Number").children.first.to_s
        source = error.search("Source").children.first
        raise ShippingScale::Error.for_code(code).new(why, code, source)
      end

      ShippingScale::Response.parse(xml)
    end

    def testing?
      ShippingScale.config.testing?
    end

    def server(request)
      case 
      when request.secure? 
        "https://secure.shippingapis.com/ShippingAPI.dll"
      when testing?
        "http://testing.shippingapis.com/ShippingAPI.dll"
      else
        "http://production.shippingapis.com/ShippingAPI.dll"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shipping-scale-0.1.0 lib/shipping_scale/client.rb