Class: Squake::Pricing

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

Overview

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.quote(product_id:, fixed_total: nil, currency: 'EUR', carbon_quantity: nil, carbon_unit: 'gram', expand: [], client: Squake::Client.new, request_id: nil) ⇒ Object



23
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
# File 'lib/squake/pricing.rb', line 23

def self.quote(
  product_id:, fixed_total: nil, currency: 'EUR', carbon_quantity: nil, carbon_unit: 'gram',
  expand: [], client: Squake::Client.new, request_id: nil
)

  result = client.call(
    path: ENDPOINT,
    method: :get,
    headers: { 'X-Request-Id' => request_id }.compact,
    params: {
      product: product_id,
      fixed_total: fixed_total,
      currency: currency,
      carbon_quantity: carbon_quantity,
      carbon_unit: carbon_unit,
      expand: expand,
    },
  )

  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