Class: Squake::CalculationWithPricing

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

Constant Summary collapse

ENDPOINT =
T.let('/v2/calculations-with-pricing', String)

Class Method Summary collapse

Class Method Details

.quote(items:, product:, currency: 'EUR', carbon_unit: 'gram', expand: [], payment_link_return_url: nil, payment_method: nil, client: Squake::Client.new, request_id: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
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/calculation_with_pricing.rb', line 24

def self.quote(
  items:, product:, currency: 'EUR', carbon_unit: 'gram',
  expand: [], payment_link_return_url: nil, payment_method: nil,
  client: Squake::Client.new, request_id: nil
)
  # @TODO: add typed objects for all possible items. Until then, we allow either a Hash or a T::Struct
  items = items.map do |item|
    item.is_a?(T::Struct) ? item.serialize : item
  end

  result = client.call(
    path: ENDPOINT,
    method: :post,
    headers: { 'X-Request-Id' => request_id }.compact,
    params: {
      items: items,
      product: product,
      currency: currency,
      carbon_unit: carbon_unit,
      expand: expand,
      payment_link_return_url: payment_link_return_url,
      payment_method: payment_method&.serialize,
    },
  )

  if result.success?
    pricing = Squake::Model::Pricing.from_api_response(
      T.cast(result.body, T::Hash[Symbol, T.untyped]),
    )
    Return.new(result: pricing)
  else
    error = Squake::Errors::APIErrorResult.new(
      code: :"api_error_#{result.code}",
      detail: result.error_message,
    )
    Return.new(errors: [error])
  end
end