Sha256: 166dd00737f343381df149ca639aa6987d1b3abcaf4407c4b588c74a657d9a5f

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require "duty_calculator/client"
require "addressable/uri"
module DutyCalculator
  class Calculation
    class << self
      def validate_params(params)
        required = %w{from to shipping_mode shipping
                      insurance currency preferential_rates detailed_result incl_hs_codes cat
                      hs country_of_hs_code desc
                      sku value weight qty origin reference}
        msg = []
        required.each do |r|
          # ensure keys are strings
          msg << r unless params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}.has_key?(r)
        end
        raise ArgumentError, "Missing arguments from hash #{msg.to_s}" if msg.size >= 1
        return params
      end
    end

    def self.get(params={})
      qs = params.collect do |k,v|
            if v.kind_of?(Array)
              memo = ""
              v.each_with_index do |value, index|
                memo << "#{k}[#{index}]=#{value}"
                memo << "&" if v.size > (index+1)
              end
              memo
            else
              "#{k}=#{v}"
            end
          end
      conn = DutyCalculator::Client.new
      resp = conn.get "#{DutyCalculator::Client.api_base}/calculation?#{qs.join('&')}"
      raise Exception, "Duty Calculator Error: #{DutyCalculator::ErrorMessages.for_code(resp.body["error"]["code"])}" if resp.body["error"]
      raise Exception, "HTTP Status Code #{resp.status}" if resp.status.to_i != 200
      return resp.body
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
duty_calculator-0.0.4 lib/duty_calculator/calculation.rb