squake 0.4.3 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e22e0f482c248647b17219586fe85804cf1e44a5ba84a559d5635c79c77eb7f5
4
- data.tar.gz: 3fc836ae7a666ac85da41511d97f6017394910561563f1a873dadfc5dfcd945c
3
+ metadata.gz: 8777e0847a6851455fbf62736f46f685c18a393e33a1586f1f879ceee4fdc4ba
4
+ data.tar.gz: b6c5bb698721fdce1b2d51e552ac3d1c82fac4f8330098667b4f6a17f20b0bd2
5
5
  SHA512:
6
- metadata.gz: ceb4f8164253c9891c7be951943d09230cd02f132f4b054aca34eaaea3fa9287802903d927c5877857c73ba1e9021137a673b5bf6c6fe34994a6a953043cc121
7
- data.tar.gz: cae5cb6dfdb84d0784bd350f21569f6de04ad76ca42b21d21ac58c2b69bd9251ecbbd702160db1c44143e5660e41bac0d02412a3c0e69ebaa912687058a57b99
6
+ metadata.gz: 5c701fef3eb69146d564477e923f551328f6e5898161e58fc65ed659ed7e2c5796d02f432359fb19b1a7ea611524de3ff0daa7dbe891f963b6c1c70f62feffd7
7
+ data.tar.gz: 55f77258422c2f3fc525a63d2815fd3e68edf5480145e2629d4738489e42df9eafb43bb4f4de81c0ad8c9593482992eca2afbf039488ef225c2be2ee414a676d
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
@@ -9,15 +9,22 @@ module Squake
9
9
  # parameters in a URI or as form parameters in a request body. This mainly
10
10
  # involves escaping special characters from parameter keys and values (e.g.
11
11
  # `&`).
12
- sig { params(params: T::Hash[Symbol, String]).returns(String) }
12
+ sig { params(params: T::Hash[Symbol, T.any(String, T::Array[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
+ case v
16
+ when Array
17
+ v.map { |e| "#{url_encode(k.to_s)}[]=#{url_encode(e.to_s)}" }.join('&')
18
+ else
19
+ "#{url_encode(k.to_s)}=#{url_encode(v.to_s)}" unless v.nil?
20
+ end
21
+ end.join('&')
15
22
  end
16
23
 
17
24
  # Encodes a string in a way that makes it suitable for use in a set of
18
25
  # query parameters in a URI or in a set of form parameters in a request
19
26
  # body.
20
- sig { params(key: String).returns(String) }
27
+ sig { params(key: T.nilable(String)).returns(String) }
21
28
  def self.url_encode(key)
22
29
  CGI.escape(key.to_s).
23
30
  # 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.1'
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.1
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-21 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
@@ -217,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
218
  - !ruby/object:Gem::Version
218
219
  version: '0'
219
220
  requirements: []
220
- rubygems_version: 3.2.3
221
+ rubygems_version: 3.4.19
221
222
  signing_key:
222
223
  specification_version: 4
223
224
  summary: The industry solution for sustainable travel and logistics.