Sha256: cfadef9fa5bc98fbd8014450adcb088c74c094d5d57e57181494e29bac1356bf

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module ShippingScale
  class Client
    def request(request)
      server = "http://production.shippingapis.com/ShippingAPI.dll"

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

      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.2 lib/shipping_scale/client.rb