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
59
60
61
# File 'lib/squake/model/pricing.rb', line 35

def self.from_api_response(response_body)
  price_or_id = T.let(response_body.fetch(:price), T.any(String, T::Hash[Symbol, T.untyped]))
  price = price_or_id.is_a?(Hash) ? Squake::Model::Price.from_api_response(price_or_id) : price_or_id

  product_or_id = T.let(response_body.fetch(:product), T.any(String, T::Hash[Symbol, T.untyped]))
  product = product_or_id.is_a?(Hash) ? Squake::Model::Product.from_api_response(product_or_id) : product_or_id

  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: BigDecimal(response_body.fetch(:carbon_quantity).to_s),
    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