Skip to content

Commit

Permalink
Merge pull request #20 from patch-technology/lovisa/version-1.2.6
Browse files Browse the repository at this point in the history
[v1.3.0] Add Flight/Shipping/Vehicle estimates
  • Loading branch information
biglovisa authored Feb 9, 2021
2 parents d0d3893 + c4ee047 commit 88e34d0
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.3.0] - 2021-02-08

### Added

- Adds support for creating carbon emission estimates for flights, shipping, and vehicles. See the [docs](https://docs.usepatch.com/#/?id=estimates) for more information.

## [1.2.5] - 2020-01-08

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
patch_ruby (1.2.5)
patch_ruby (1.3.0)
json (~> 2.1, >= 2.1.0)
typhoeus (~> 1.0, >= 1.0.1)

Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,36 @@ Estimates allow API users to get a quote for the cost of compensating a certain

#### Examples
```ruby
# Create an estimate
# Create a mass estimate
mass = 1_000_000 # Pass in the mass in grams (i.e. 1 metric tonne)
Patch::Estimate.create_mass_estimate(mass_g: mass)

# Create a flight estimate
distance_m = 1_000_000 # Pass in the distance traveled in meters
Patch::Estimate.create_flight_estimate(distance_m: distance_m)

# Create a shipping estimate
distance_m = 1_000_000 # Pass in the shipping distance in meters
package_mass_g = 10_000 # Pass in the weight of the package shipped in grams
transportation_method = "air" # Pass in the transportation method (air, rail, road, sea)
Patch::Estimate.create_shipping_estimate(
distance_m: distance_m,
package_mass_g: package_mass_g,
transportation_method: transportation_method
)

# Create a vehicle estimate
distance_m = 1_000_000 # Pass in the shipping distance in meters
make = "Toyota" # Pass in the car make
model = "Corolla" # Pass in the car model
year = 2000 # Pass in the car year
Patch::Estimate.create_vehicle_estimate(
distance_m: distance_m,
make: make,
model: model,
year: year
)

## You can also specify a project-id field (optional) to be used instead of the preferred one
project_id = 'pro_test_1234' # Pass in the project's ID
Patch::Estimate.create_mass_estimate(mass_g: mass, project_id: project_id)
Expand Down
195 changes: 195 additions & 0 deletions lib/patch_ruby/api/estimates_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
module Patch
class EstimatesApi
OPERATIONS = [
:create_flight_estimate,
:create_mass_estimate,
:create_shipping_estimate,
:create_vehicle_estimate,
:retrieve_estimate,
:retrieve_estimates,
]
Expand All @@ -25,6 +28,70 @@ class EstimatesApi
def initialize(api_client = ApiClient.default)
@api_client = api_client
end
# Create a flight estimate given the distance traveled in meters
# Creates a flight estimate for the amount of CO2 to be compensated. An order in the `draft` state may be created based on the parameters, linked to the estimate.
# @param create_flight_estimate_request [CreateFlightEstimateRequest]
# @param [Hash] opts the optional parameters
# @return [EstimateResponse]
def create_flight_estimate(create_flight_estimate_request, opts = {})
data, _status_code, _headers = create_flight_estimate_with_http_info(create_flight_estimate_request, opts)
data
end

# Create a flight estimate given the distance traveled in meters
# Creates a flight estimate for the amount of CO2 to be compensated. An order in the `draft` state may be created based on the parameters, linked to the estimate.
# @param create_flight_estimate_request [CreateFlightEstimateRequest]
# @param [Hash] opts the optional parameters
# @return [Array<(EstimateResponse, Integer, Hash)>] EstimateResponse data, response status code and response headers
def create_flight_estimate_with_http_info(create_flight_estimate_request, opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug 'Calling API: EstimatesApi.create_flight_estimate ...'
end
# verify the required parameter 'create_flight_estimate_request' is set
if @api_client.config.client_side_validation && create_flight_estimate_request.nil?
fail ArgumentError, "Missing the required parameter 'create_flight_estimate_request' when calling EstimatesApi.create_flight_estimate"
end
# resource path
local_var_path = '/v1/estimates/flight'

# query parameters
query_params = opts[:query_params] || {}

# header parameters
header_params = opts[:header_params] || {}
# HTTP header 'Accept' (if needed)
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
# HTTP header 'Content-Type'
header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])

# form parameters
form_params = opts[:form_params] || {}

# http body (model)
post_body = opts[:body] || @api_client.object_to_http_body(create_flight_estimate_request)

# return_type
return_type = opts[:return_type] || 'EstimateResponse'

# auth_names
auth_names = opts[:auth_names] || ['bearer_auth']

new_options = opts.merge(
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names,
:return_type => return_type
)

data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
if @api_client.config.debugging
@api_client.config.logger.debug "API called: EstimatesApi#create_flight_estimate\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
return data, status_code, headers
end

# Create an estimate based on mass of CO2
# Creates an estimate for the mass of CO2 to be compensated. An order in the `draft` state will also be created, linked to the estimate.
# @param create_mass_estimate_request [CreateMassEstimateRequest]
Expand Down Expand Up @@ -89,6 +156,134 @@ def create_mass_estimate_with_http_info(create_mass_estimate_request, opts = {})
return data, status_code, headers
end

# Create a shipping estimate given the distance traveled in meters, package weight, and transportation method.
# Creates a shipping estimate for the amount of CO2 to be compensated. An order in the `draft` state may be created based on the parameters.
# @param create_shipping_estimate_request [CreateShippingEstimateRequest]
# @param [Hash] opts the optional parameters
# @return [EstimateResponse]
def create_shipping_estimate(create_shipping_estimate_request, opts = {})
data, _status_code, _headers = create_shipping_estimate_with_http_info(create_shipping_estimate_request, opts)
data
end

# Create a shipping estimate given the distance traveled in meters, package weight, and transportation method.
# Creates a shipping estimate for the amount of CO2 to be compensated. An order in the &#x60;draft&#x60; state may be created based on the parameters.
# @param create_shipping_estimate_request [CreateShippingEstimateRequest]
# @param [Hash] opts the optional parameters
# @return [Array<(EstimateResponse, Integer, Hash)>] EstimateResponse data, response status code and response headers
def create_shipping_estimate_with_http_info(create_shipping_estimate_request, opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug 'Calling API: EstimatesApi.create_shipping_estimate ...'
end
# verify the required parameter 'create_shipping_estimate_request' is set
if @api_client.config.client_side_validation && create_shipping_estimate_request.nil?
fail ArgumentError, "Missing the required parameter 'create_shipping_estimate_request' when calling EstimatesApi.create_shipping_estimate"
end
# resource path
local_var_path = '/v1/estimates/shipping'

# query parameters
query_params = opts[:query_params] || {}

# header parameters
header_params = opts[:header_params] || {}
# HTTP header 'Accept' (if needed)
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
# HTTP header 'Content-Type'
header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])

# form parameters
form_params = opts[:form_params] || {}

# http body (model)
post_body = opts[:body] || @api_client.object_to_http_body(create_shipping_estimate_request)

# return_type
return_type = opts[:return_type] || 'EstimateResponse'

# auth_names
auth_names = opts[:auth_names] || ['bearer_auth']

new_options = opts.merge(
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names,
:return_type => return_type
)

data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
if @api_client.config.debugging
@api_client.config.logger.debug "API called: EstimatesApi#create_shipping_estimate\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
return data, status_code, headers
end

# Create a vehicle estimate given the distance traveled in meters and the type of vehicle
# Creates an estimate and calculates the amount of CO2 to be compensated depending on the distance and the vehicle. An order in the `draft` state may be created based on the parameters, linked to the estimate.
# @param create_vehicle_estimate_request [CreateVehicleEstimateRequest]
# @param [Hash] opts the optional parameters
# @return [EstimateResponse]
def create_vehicle_estimate(create_vehicle_estimate_request, opts = {})
data, _status_code, _headers = create_vehicle_estimate_with_http_info(create_vehicle_estimate_request, opts)
data
end

# Create a vehicle estimate given the distance traveled in meters and the type of vehicle
# Creates an estimate and calculates the amount of CO2 to be compensated depending on the distance and the vehicle. An order in the &#x60;draft&#x60; state may be created based on the parameters, linked to the estimate.
# @param create_vehicle_estimate_request [CreateVehicleEstimateRequest]
# @param [Hash] opts the optional parameters
# @return [Array<(EstimateResponse, Integer, Hash)>] EstimateResponse data, response status code and response headers
def create_vehicle_estimate_with_http_info(create_vehicle_estimate_request, opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug 'Calling API: EstimatesApi.create_vehicle_estimate ...'
end
# verify the required parameter 'create_vehicle_estimate_request' is set
if @api_client.config.client_side_validation && create_vehicle_estimate_request.nil?
fail ArgumentError, "Missing the required parameter 'create_vehicle_estimate_request' when calling EstimatesApi.create_vehicle_estimate"
end
# resource path
local_var_path = '/v1/estimates/vehicle'

# query parameters
query_params = opts[:query_params] || {}

# header parameters
header_params = opts[:header_params] || {}
# HTTP header 'Accept' (if needed)
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
# HTTP header 'Content-Type'
header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])

# form parameters
form_params = opts[:form_params] || {}

# http body (model)
post_body = opts[:body] || @api_client.object_to_http_body(create_vehicle_estimate_request)

# return_type
return_type = opts[:return_type] || 'EstimateResponse'

# auth_names
auth_names = opts[:auth_names] || ['bearer_auth']

new_options = opts.merge(
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names,
:return_type => return_type
)

data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
if @api_client.config.debugging
@api_client.config.logger.debug "API called: EstimatesApi#create_vehicle_estimate\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
return data, status_code, headers
end

# Retrieves an estimate
# Retrieves a given estimate and its associated order. You can only retrieve estimates associated with the organization you are querying for.
# @param id [String]
Expand Down
13 changes: 12 additions & 1 deletion lib/patch_ruby/models/create_mass_estimate_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ module Patch
class CreateMassEstimateRequest
attr_accessor :mass_g

attr_accessor :create_order

attr_accessor :project_id

# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
:'mass_g' => :'mass_g',
:'create_order' => :'create_order',
:'project_id' => :'project_id'
}
end
Expand All @@ -30,6 +33,7 @@ def self.attribute_map
def self.openapi_types
{
:'mass_g' => :'Integer',
:'create_order' => :'Boolean',
:'project_id' => :'String'
}
end
Expand All @@ -38,6 +42,8 @@ def self.openapi_types
def self.openapi_nullable
nullable_properties = Set.new

nullable_properties.add("create_order")

nullable_properties
end

Expand Down Expand Up @@ -71,6 +77,10 @@ def initialize(attributes = {})
self.mass_g = attributes[:'mass_g']
end

if attributes.key?(:'create_order')
self.create_order = attributes[:'create_order']
end

if attributes.key?(:'project_id')
self.project_id = attributes[:'project_id']
end
Expand Down Expand Up @@ -128,6 +138,7 @@ def ==(o)
return true if self.equal?(o)
self.class == o.class &&
mass_g == o.mass_g &&
create_order == o.create_order &&
project_id == o.project_id
end

Expand All @@ -140,7 +151,7 @@ def eql?(o)
# Calculates hash code according to all attributes.
# @return [Integer] Hash code
def hash
[mass_g, project_id].hash
[mass_g, create_order, project_id].hash
end

# Builds the object from hash
Expand Down
Loading

0 comments on commit 88e34d0

Please sign in to comment.