Sha256: 9c5123ac20d06a28e2a267bb0871416d6a167677f8cfb2e452679f2fd3148989

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

module Intelipost::ApiComponents
  class Quote < Component
    attr_accessor :api_key

    def initialize(api_key)
      self.api_key = api_key
    end

    def request_json(origin_zipcode, destination_zipcode, volumes_collection)
      {
         "origin_zip_code" => origin_zipcode,
         "destination_zip_code" => destination_zipcode,
         "volumes" => volumes_collection.inject([]) { |arr, v|
           arr << {
             "weight" => v.weight,
             "volume_type" => v.volume_type,
             "cost_of_goods" => v.cost_of_goods,
             "width" => v.width,
             "height" => v.height,
             "length" => v.length
           }
         }
      }
    end

    def create(origin_zipcode, destination_zipcode, volumes_collection)
      raw_request = self.request_json(origin_zipcode, destination_zipcode, volumes_collection)
      raw_response = Intelipost::Facade.post_with_log("/v1/quote", api_key, :body => raw_request.to_json)

      if raw_response["status"] == "ERROR"
        response = Intelipost::Models::ErrorResponse.new(raw_response)
      else
        response = Intelipost::Models::Quote.new(raw_response)
        response.attributes = raw_response["content"] || {}
        response.volumes = raw_response["content"]["volumes"].inject([]) { |res, volume|
          res << Intelipost::Models::Volume.new(volume)
          res
        }
        response.delivery_options = raw_response["content"]["delivery_options"].inject([]) { |res, delivery_option|
          res << Intelipost::Models::DeliveryOption.new(delivery_option)
          res
        }
      end

      response
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
intelipost-api-1.0.7 lib/intelipost/api/components/quote.rb
intelipost-api-1.0.6 lib/intelipost/api/components/quote.rb
intelipost-api-1.0.5 lib/intelipost/api/components/quote.rb
intelipost-api-1.0.4 lib/intelipost/api/components/quote.rb
intelipost-api-1.0.3 lib/intelipost/api/components/quote.rb
intelipost-api-1.0.2 lib/intelipost/api/components/quote.rb