Class: Intelipost::ApiComponents::Quote

Inherits:
Component
  • Object
show all
Defined in:
lib/intelipost/api/components/quote.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Component

#has_error?

Constructor Details

#initialize(api_key) ⇒ Quote

Returns a new instance of Quote.



5
6
7
# File 'lib/intelipost/api/components/quote.rb', line 5

def initialize(api_key)
  self.api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/intelipost/api/components/quote.rb', line 3

def api_key
  @api_key
end

Instance Method Details

#create(origin_zipcode, destination_zipcode, volumes_collection) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/intelipost/api/components/quote.rb', line 26

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

#request_json(origin_zipcode, destination_zipcode, volumes_collection) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/intelipost/api/components/quote.rb', line 9

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