Class: Squake::Model::Pricing

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/squake/model/pricing.rb

Defined Under Namespace

Classes: Item

Class Method Summary collapse

Class Method Details

.from_api_response(response_body) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/squake/model/pricing.rb', line 35

def self.from_api_response(response_body)
  price = Squake::Model::Price.from_api_response(response_body)
  product = Squake::Model::Product.from_api_response(response_body)

  items = response_body.fetch(:items, [])
  items.map! do |item|
    item[:carbon_quantity] = item.fetch(:carbon_quantity).to_d
    item[:distance] = item.fetch(:distance).to_d
    Item.new(item)
  end

  Squake::Model::Pricing.new(
    id: response_body.fetch(:id),
    items: items,
    carbon_quantity: response_body.fetch(:carbon_quantity).to_d,
    carbon_unit: response_body.fetch(:carbon_unit),
    payment_link: response_body.fetch(:payment_link, nil),
    price: price,
    product: product,
    valid_until: Date.parse(response_body.fetch(:valid_until, nil)),
    currency: response_body.fetch(:currency),
    total: response_body.fetch(:total),
  )
end