Class: ShipitAPI::Quotation

Inherits:
Object
  • Object
show all
Defined in:
lib/shipit_api/quotation.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Quotation

Returns a new instance of Quotation.



7
8
9
10
11
# File 'lib/shipit_api/quotation.rb', line 7

def initialize(opts = {})
  @opts = opts
  @api_call = ShipitAPI::Session.connection('prices', 3)
  build!
end

Class Method Details

.get_prices(opts = {}) ⇒ Object



3
4
5
# File 'lib/shipit_api/quotation.rb', line 3

def self.get_prices(opts = {})
  new(opts).get_prices
end

Instance Method Details

#build!Object



32
33
34
35
36
37
# File 'lib/shipit_api/quotation.rb', line 32

def build!
  @opts.each do |k,v|
    singleton_class.send(:attr_accessor, k)
    instance_variable_set("@#{k}", v)
  end
end

#get_communeObject



24
25
26
# File 'lib/shipit_api/quotation.rb', line 24

def get_commune
  ShipitAPI::Commune.find_by_name({ name: @opts[:commune_name] })
end

#get_pricesObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/shipit_api/quotation.rb', line 13

def get_prices
  if !@opts[:commune_name].nil?
    commune = get_commune
    @opts.merge!({ to_commune_id: commune[:id]})
  end

  package = { package: @opts }
  response = @api_call.post('', package.to_json)
  JSON.parse(response.body, symbolize_names: true)
end

#quotaiton_attrsObject



28
29
30
# File 'lib/shipit_api/quotation.rb', line 28

def quotaiton_attrs
  { width: 10, length: 10, height: 10, weight: 1, destinty: 'Domicilio' }
end