Skip to content

Commit e6f0167

Browse files
authored
Merge pull request #17 from patch-technology/ta/order-price-creation
order price creation
2 parents 51f12f6 + adfaed0 commit e6f0167

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
## [1.2.3] - 2020-09-28
10+
## [1.2.4] - 2020-10-14
11+
12+
### Added
13+
14+
- `total_price_cents_usd` field to `orders`
15+
- allows users to create an order by total price
16+
17+
### Changed
18+
19+
- order creation requires either `mass_g` or `total_price_cents_usd`, but not both
20+
21+
## [1.2.3] - 2020-10-05
1122

1223
### Added
1324

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
patch_ruby (1.2.3)
4+
patch_ruby (1.2.4)
55
json (~> 2.1, >= 2.1.0)
66
typhoeus (~> 1.0, >= 1.0.1)
77

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,27 @@ end
4545
### Orders
4646
In Patch, orders represent a purchase of carbon offsets or negative emissions by mass. Place orders directly if you know the amount of carbon dioxide you would like to sequester. If you do not know how much to purchase, use an estimate.
4747

48+
In Patch, orders represent a purchase of carbon offsets or negative emissions by mass.
49+
Place orders directly if you know the amount of carbon dioxide you would like to sequester.
50+
If you do not know how much to purchase, use an estimate.
51+
You can also create an order with a maximum desired price, and we'll allocate enough mass to
52+
fulfill the order for you.
53+
4854
[API Reference](https://docs.usepatch.com/#/?id=orders)
4955

5056
#### Examples
5157
```ruby
52-
# Create an order
58+
# Create an order - you can create an order
59+
# providing either mass_g or total_price_cents_usd, but not both
60+
61+
# Create order with mass
5362
mass = 1_000_000 # Pass in the mass in grams (i.e. 1 metric tonne)
5463
Patch::Order.create_order(mass_g: mass)
5564

65+
# Create an order with maximum total price
66+
total_price_cents_usd = 5_00 # Pass in the total price in cents (i.e. 5 dollars)
67+
Patch::Order.create_order(total_price_cents_usd: total_price_cents_usd)
68+
5669
## You can also specify a project-id field (optional) to be used instead of the preferred one
5770
project_id = 'pro_test_1234' # Pass in the project's ID
5871
Patch::Order.create_order(mass_g: mass, project_id: project_id)

lib/patch_ruby/models/create_order_request.rb

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Patch
1616
class CreateOrderRequest
1717
attr_accessor :mass_g
1818

19+
attr_accessor :total_price_cents_usd
20+
1921
attr_accessor :project_id
2022

2123
attr_accessor :metadata
@@ -24,6 +26,7 @@ class CreateOrderRequest
2426
def self.attribute_map
2527
{
2628
:'mass_g' => :'mass_g',
29+
:'total_price_cents_usd' => :'total_price_cents_usd',
2730
:'project_id' => :'project_id',
2831
:'metadata' => :'metadata'
2932
}
@@ -33,6 +36,7 @@ def self.attribute_map
3336
def self.openapi_types
3437
{
3538
:'mass_g' => :'Integer',
39+
:'total_price_cents_usd' => :'Integer',
3640
:'project_id' => :'String',
3741
:'metadata' => :'Object'
3842
}
@@ -68,6 +72,10 @@ def initialize(attributes = {})
6872
self.mass_g = attributes[:'mass_g']
6973
end
7074

75+
if attributes.key?(:'total_price_cents_usd')
76+
self.total_price_cents_usd = attributes[:'total_price_cents_usd']
77+
end
78+
7179
if attributes.key?(:'project_id')
7280
self.project_id = attributes[:'project_id']
7381
end
@@ -81,54 +89,61 @@ def initialize(attributes = {})
8189
# @return Array for valid properties with the reasons
8290
def list_invalid_properties
8391
invalid_properties = Array.new
84-
if @mass_g.nil?
85-
invalid_properties.push('invalid value for "mass_g", mass_g cannot be nil.')
86-
end
87-
88-
if @mass_g > 2000000000
92+
if !@mass_g.nil? && @mass_g > 2000000000
8993
invalid_properties.push('invalid value for "mass_g", must be smaller than or equal to 2000000000.')
9094
end
9195

92-
if @mass_g < 1
96+
if !@mass_g.nil? && @mass_g < 1
9397
invalid_properties.push('invalid value for "mass_g", must be greater than or equal to 1.')
9498
end
9599

100+
if !@total_price_cents_usd.nil? && @total_price_cents_usd < 1
101+
invalid_properties.push('invalid value for "total_price_cents_usd", must be greater than or equal to 1.')
102+
end
103+
96104
invalid_properties
97105
end
98106

99107
# Check to see if the all the properties in the model are valid
100108
# @return true if the model is valid
101109
def valid?
102-
return false if @mass_g.nil?
103-
return false if @mass_g > 2000000000
104-
return false if @mass_g < 1
110+
return false if !@mass_g.nil? && @mass_g > 2000000000
111+
return false if !@mass_g.nil? && @mass_g < 1
112+
return false if !@total_price_cents_usd.nil? && @total_price_cents_usd < 1
105113
true
106114
end
107115

108116
# Custom attribute writer method with validation
109117
# @param [Object] mass_g Value to be assigned
110118
def mass_g=(mass_g)
111-
if mass_g.nil?
112-
fail ArgumentError, 'mass_g cannot be nil'
113-
end
114-
115-
if mass_g > 2000000000
119+
if !mass_g.nil? && mass_g > 2000000000
116120
fail ArgumentError, 'invalid value for "mass_g", must be smaller than or equal to 2000000000.'
117121
end
118122

119-
if mass_g < 1
123+
if !mass_g.nil? && mass_g < 1
120124
fail ArgumentError, 'invalid value for "mass_g", must be greater than or equal to 1.'
121125
end
122126

123127
@mass_g = mass_g
124128
end
125129

130+
# Custom attribute writer method with validation
131+
# @param [Object] total_price_cents_usd Value to be assigned
132+
def total_price_cents_usd=(total_price_cents_usd)
133+
if !total_price_cents_usd.nil? && total_price_cents_usd < 1
134+
fail ArgumentError, 'invalid value for "total_price_cents_usd", must be greater than or equal to 1.'
135+
end
136+
137+
@total_price_cents_usd = total_price_cents_usd
138+
end
139+
126140
# Checks equality by comparing each attribute.
127141
# @param [Object] Object to be compared
128142
def ==(o)
129143
return true if self.equal?(o)
130144
self.class == o.class &&
131145
mass_g == o.mass_g &&
146+
total_price_cents_usd == o.total_price_cents_usd &&
132147
project_id == o.project_id &&
133148
metadata == o.metadata
134149
end
@@ -142,7 +157,7 @@ def eql?(o)
142157
# Calculates hash code according to all attributes.
143158
# @return [Integer] Hash code
144159
def hash
145-
[mass_g, project_id, metadata].hash
160+
[mass_g, total_price_cents_usd, project_id, metadata].hash
146161
end
147162

148163
# Builds the object from hash

lib/patch_ruby/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
=end
1212

1313
module Patch
14-
VERSION = '1.2.3'
14+
VERSION = '1.2.4'
1515
end

spec/integration/orders_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@
4747
expect(create_order_response.data.patch_fee_cents_usd).not_to be_empty
4848
end
4949

50+
it 'supports create with a total price' do
51+
retrieve_project_response = Patch::Project.retrieve_project(
52+
Constants::BIOMASS_TEST_PROJECT_ID
53+
)
54+
55+
project_id = retrieve_project_response.data.id
56+
total_price_cents_usd = 5_00
57+
58+
create_order_response = Patch::Order.create_order(
59+
total_price_cents_usd: total_price_cents_usd,
60+
project_id: project_id
61+
)
62+
63+
expect(create_order_response.success).to eq true
64+
65+
order = create_order_response.data
66+
67+
expect(order.id).not_to be_nil
68+
expect(order.mass_g).to eq(5_000_000)
69+
expect(order.price_cents_usd.to_i).to eq(500)
70+
expect(order.patch_fee_cents_usd).not_to be_empty
71+
expect(
72+
order.price_cents_usd.to_i + order.patch_fee_cents_usd.to_i
73+
).to eq(total_price_cents_usd)
74+
end
75+
5076
it 'supports create with metadata' do
5177
metadata = { user: 'john doe' }
5278

0 commit comments

Comments
 (0)