squake 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e22e0f482c248647b17219586fe85804cf1e44a5ba84a559d5635c79c77eb7f5
4
- data.tar.gz: 3fc836ae7a666ac85da41511d97f6017394910561563f1a873dadfc5dfcd945c
3
+ metadata.gz: 9b4d311a5ad4fb22fd6ec57a34bf0c1a2d79434d8a90129b79b5bd0bb2336587
4
+ data.tar.gz: 19dfd1352aa055fe5ee427a5ce4f8b1cb518b8259c770c77c6360c23a7116e06
5
5
  SHA512:
6
- metadata.gz: ceb4f8164253c9891c7be951943d09230cd02f132f4b054aca34eaaea3fa9287802903d927c5877857c73ba1e9021137a673b5bf6c6fe34994a6a953043cc121
7
- data.tar.gz: cae5cb6dfdb84d0784bd350f21569f6de04ad76ca42b21d21ac58c2b69bd9251ecbbd702160db1c44143e5660e41bac0d02412a3c0e69ebaa912687058a57b99
6
+ metadata.gz: 109e5a1cc4e00f3c840252507b17a740a15a4afee802917e56df9d7e3a0c32f0c6d213313193fa2e03b429b821dcddcf4668bf5aac07744c65a4e8425b9fb3b5
7
+ data.tar.gz: 3f956999206d2b89d4989728a07c75d318fd8e95793df0346237ece389a069d03f31c8103eaf2526d81ab983dc4f1304b5401ea271579a284a378bb83d2b0a16
data/README.md CHANGED
@@ -117,6 +117,44 @@ else
117
117
  end
118
118
  ```
119
119
 
120
+ ### Get a price quote alone
121
+
122
+ #### For a given carbon quantity
123
+
124
+ ```ruby
125
+ Squake::Pricing.quote(
126
+ client: squake_client, # optional
127
+ carbon_quantity: 1000, # required
128
+ carbon_unit: 'kilogram', # optional, default: 'gram', other options: 'kilogram', 'tonne'
129
+ product_id: 'some_product_id', # required
130
+ expand: [], # optional, default: [], allowed values: 'product', 'price' to enrich the response
131
+ )
132
+
133
+ if context.success?
134
+ context.result # Squake::Model::Pricing
135
+ else
136
+ context.errors # [Squake::Errors::APIErrorResult]
137
+ end
138
+ ```
139
+
140
+ #### For a given fixed total
141
+
142
+ ```ruby
143
+ context = Squake::Pricing.quote(
144
+ client: squake_client, # optional
145
+ fixed_total: 1000, # required
146
+ currency: 'USD', # optional, default: 'EUR'
147
+ product_id: 'some_product_id', # required
148
+ expand: [], # optional, default: [], allowed values: 'product', 'price' to enrich the response
149
+ )
150
+
151
+ if context.success?
152
+ context.result # Squake::Model::Pricing
153
+ else
154
+ context.errors # [Squake::Errors::APIErrorResult]
155
+ end
156
+ ```
157
+
120
158
  ### Calculate emissions and include a price quote
121
159
 
122
160
  ```ruby
@@ -49,7 +49,7 @@ module Squake
49
49
  Squake::Model::Pricing.new(
50
50
  id: response_body.fetch(:id),
51
51
  items: items,
52
- carbon_quantity: response_body.fetch(:carbon_quantity).to_d,
52
+ carbon_quantity: BigDecimal(response_body.fetch(:carbon_quantity).to_s),
53
53
  carbon_unit: response_body.fetch(:carbon_unit),
54
54
  payment_link: response_body.fetch(:payment_link, nil),
55
55
  price: price,
@@ -0,0 +1,56 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Squake
5
+ # https://docs-v2.squake.earth/group/endpoint-pricing
6
+ class Pricing
7
+ extend T::Sig
8
+
9
+ ENDPOINT = T.let('/v2/pricing', String)
10
+
11
+ sig do
12
+ params(
13
+ product_id: String,
14
+ fixed_total: T.nilable(Numeric),
15
+ currency: String,
16
+ carbon_quantity: T.nilable(Numeric),
17
+ carbon_unit: T.nilable(String),
18
+ expand: T::Array[String],
19
+ client: Squake::Client,
20
+ request_id: T.nilable(String),
21
+ ).returns(Squake::Return[Squake::Model::Pricing])
22
+ end
23
+ def self.quote(
24
+ product_id:, fixed_total: nil, currency: 'EUR', carbon_quantity: nil, carbon_unit: 'gram',
25
+ expand: [], client: Squake::Client.new, request_id: nil
26
+ )
27
+
28
+ result = client.call(
29
+ path: ENDPOINT,
30
+ method: :get,
31
+ headers: { 'X-Request-Id' => request_id }.compact,
32
+ params: {
33
+ product: product_id,
34
+ fixed_total: fixed_total,
35
+ currency: currency,
36
+ carbon_quantity: carbon_quantity,
37
+ carbon_unit: carbon_unit,
38
+ expand: expand,
39
+ },
40
+ )
41
+
42
+ if result.success?
43
+ pricing = Squake::Model::Pricing.from_api_response(
44
+ T.cast(result.body, T::Hash[Symbol, T.untyped]),
45
+ )
46
+ Return.new(result: pricing)
47
+ else
48
+ error = Squake::Errors::APIErrorResult.new(
49
+ code: :"api_error_#{result.code}",
50
+ detail: result.error_message,
51
+ )
52
+ Return.new(errors: [error])
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/squake/util.rb CHANGED
@@ -11,13 +11,15 @@ module Squake
11
11
  # `&`).
12
12
  sig { params(params: T::Hash[Symbol, String]).returns(String) }
13
13
  def self.encode_parameters(params)
14
- params.map { |k, v| "#{url_encode(k.to_s)}=#{url_encode(v)}" }.join('&')
14
+ params.map do |k, v|
15
+ "#{url_encode(k.to_s)}=#{url_encode(v.to_s)}" unless v.nil?
16
+ end.join('&')
15
17
  end
16
18
 
17
19
  # Encodes a string in a way that makes it suitable for use in a set of
18
20
  # query parameters in a URI or in a set of form parameters in a request
19
21
  # body.
20
- sig { params(key: String).returns(String) }
22
+ sig { params(key: T.nilable(String)).returns(String) }
21
23
  def self.url_encode(key)
22
24
  CGI.escape(key.to_s).
23
25
  # Don't use strict form encoding by changing the square bracket control
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Squake
5
- VERSION = '0.4.3'
5
+ VERSION = '0.5.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SQUAKE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-12 00:00:00.000000000 Z
11
+ date: 2023-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http
@@ -190,6 +190,7 @@ files:
190
190
  - lib/squake/model/pricing.rb
191
191
  - lib/squake/model/product.rb
192
192
  - lib/squake/model/purchase.rb
193
+ - lib/squake/pricing.rb
193
194
  - lib/squake/products.rb
194
195
  - lib/squake/purchase.rb
195
196
  - lib/squake/response.rb