squake 0.4.2 → 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: beb49aaa40493fc3d858bd94bd4de78e52f1768ffdfe2c1742e899c77fd3761d
4
- data.tar.gz: 20552b4ca63fe916a479cae955dbf81b9711a59cb0d7dc2c30f9edc383e4ee3f
3
+ metadata.gz: 9b4d311a5ad4fb22fd6ec57a34bf0c1a2d79434d8a90129b79b5bd0bb2336587
4
+ data.tar.gz: 19dfd1352aa055fe5ee427a5ce4f8b1cb518b8259c770c77c6360c23a7116e06
5
5
  SHA512:
6
- metadata.gz: 83b29e6ac77d1c990c15316c7449b8e7c8c212f4844a102eeab43b4f67e20b3766a87ce32bd63d505f31a1f2eb88119992f4c53721c9f2b10df70d1b744ede81
7
- data.tar.gz: 43cac16e0b7604d144f3196d2ed67fd06a3ffc7e0612acb3f9612de365cd633470c5ebd6d0c7d1912ec10736c93173b8e6b55068eb7ac708af06b1dd7de67a90
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
data/lib/squake/client.rb CHANGED
@@ -121,7 +121,7 @@ module Squake
121
121
  ::Oj.load(result_body, symbol_keys: true)
122
122
  rescue ::Oj::ParseError, TypeError, JSON::ParserError, EncodingError => e
123
123
  # in case of an error, Squake's response body is HTML not JSON
124
- { 'error' => { 'message' => e.message, 'body' => result_body } }
124
+ { error: { 'message' => e.message, 'body' => result_body } }
125
125
  end
126
126
 
127
127
  sig { params(uri: URI::Generic).returns(Net::HTTP) }
@@ -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
@@ -30,7 +30,7 @@ module Squake
30
30
  )
31
31
 
32
32
  if result.success?
33
- products = T.cast(Array(result.body), T::Array[T::Hash[Symbol, T.untyped]]).map do |product_data|
33
+ products = [result.body].flatten.map do |product_data|
34
34
  Squake::Model::Product.from_api_response(product_data)
35
35
  end
36
36
  Return.new(result: products)
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.2'
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.2
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-11 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
@@ -136,6 +136,34 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: vcr
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
139
167
  description: This gem provides an interface for the SQUAKE API to calculate and compensate
140
168
  carbon emissions.
141
169
  email: oss@squake.earth
@@ -162,6 +190,7 @@ files:
162
190
  - lib/squake/model/pricing.rb
163
191
  - lib/squake/model/product.rb
164
192
  - lib/squake/model/purchase.rb
193
+ - lib/squake/pricing.rb
165
194
  - lib/squake/products.rb
166
195
  - lib/squake/purchase.rb
167
196
  - lib/squake/response.rb
@@ -189,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
218
  - !ruby/object:Gem::Version
190
219
  version: '0'
191
220
  requirements: []
192
- rubygems_version: 3.4.19
221
+ rubygems_version: 3.2.3
193
222
  signing_key:
194
223
  specification_version: 4
195
224
  summary: The industry solution for sustainable travel and logistics.