Skip to content

Commit

Permalink
Add support for accept-language headers (#63)
Browse files Browse the repository at this point in the history
* Add support for accept-language

* Correct value
  • Loading branch information
pcothenet authored May 16, 2022
1 parent b51bb15 commit e8d29a0
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.2
3.1.2
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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).

## [1.22.0] - 2022-05-16

### Added

- Adds support for the `accept_language` option on `projects`, to add support for the `Accept-Language` header.

## [1.21.0] - 2022-05-03

### Added
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
patch_ruby (1.21.0)
patch_ruby (1.22.0)
typhoeus (~> 1.0, >= 1.0.1)

GEM
Expand Down Expand Up @@ -73,6 +73,7 @@ PLATFORMS
arm64-darwin-20
arm64-darwin-21
x86_64-darwin-20
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ Patch::Project.retrieve_projects(type: type)
# Retrieve a list of projects with at least 100 grams of available offsets
minimum_available_mass = 100
Patch::Project.retrieve_projects(minimum_available_mass: minimum_available_mass)

# Retrieve a project in a different language
# See http://docs.patch.test:3000/#/internationalization for more information and support
project_id = 'pro_test_1234'
Patch::Project.retrieve_project(project_id, accept_language: 'fr')

# Retrieve a list of projects in a different language
# See http://docs.patch.test:3000/#/internationalization for more information and support
Patch::Project.retrieve_projects(accept_language: 'fr')
```
## Contributing

Expand Down
6 changes: 6 additions & 0 deletions lib/patch_ruby/api/projects_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def initialize(api_client = ApiClient.default)
# Retrieves a project available on Patch's platform.
# @param id [String]
# @param [Hash] opts the optional parameters
# @option opts [String] :accept_language
# @return [ProjectResponse]
def retrieve_project(id, opts = {})

Expand All @@ -39,6 +40,7 @@ def retrieve_project(id, opts = {})
# Retrieves a project available on Patch's platform.
# @param id [String]
# @param [Hash] opts the optional parameters
# @option opts [String] :accept_language
# @return [Array<(ProjectResponse, Integer, Hash)>] ProjectResponse data, response status code and response headers
def retrieve_project_with_http_info(id, opts = {})
if @api_client.config.debugging
Expand All @@ -58,6 +60,7 @@ def retrieve_project_with_http_info(id, opts = {})
header_params = opts[:header_params] || {}
# HTTP header 'Accept' (if needed)
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
header_params[:'Accept-Language'] = opts[:'accept_language'] if !opts[:'accept_language'].nil?

# form parameters
form_params = opts[:form_params] || {}
Expand Down Expand Up @@ -95,6 +98,7 @@ def retrieve_project_with_http_info(id, opts = {})
# @option opts [String] :country
# @option opts [String] :type
# @option opts [Integer] :minimum_available_mass
# @option opts [String] :accept_language
# @return [ProjectListResponse]
def retrieve_projects(opts = {})

Expand All @@ -109,6 +113,7 @@ def retrieve_projects(opts = {})
# @option opts [String] :country
# @option opts [String] :type
# @option opts [Integer] :minimum_available_mass
# @option opts [String] :accept_language
# @return [Array<(ProjectListResponse, Integer, Hash)>] ProjectListResponse data, response status code and response headers
def retrieve_projects_with_http_info(opts = {})
if @api_client.config.debugging
Expand All @@ -128,6 +133,7 @@ def retrieve_projects_with_http_info(opts = {})
header_params = opts[:header_params] || {}
# HTTP header 'Accept' (if needed)
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
header_params[:'Accept-Language'] = opts[:'accept_language'] if !opts[:'accept_language'].nil?

# form parameters
form_params = opts[:form_params] || {}
Expand Down
2 changes: 1 addition & 1 deletion lib/patch_ruby/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ApiClient
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
def initialize(config = Configuration.default)
@config = config
@user_agent = "patch-ruby/1.21.0"
@user_agent = "patch-ruby/1.22.0"
@default_headers = {
'Content-Type' => 'application/json',
'User-Agent' => @user_agent
Expand Down
34 changes: 0 additions & 34 deletions lib/patch_ruby/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ class Project
# An array of objects containing available inventory for a project. Available inventory is grouped by a project's vintage year and returns amount and pricing available for a given vintage year.
attr_accessor :inventory

class EnumAttributeValidator
attr_reader :datatype
attr_reader :allowable_values

def initialize(datatype, allowable_values)
@allowable_values = allowable_values.map do |value|
case datatype.to_s
when /Integer/i
value.to_i
when /Float/i
value.to_f
else
value
end
end
end

def valid?(value)
!value || allowable_values.include?(value)
end
end

# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
Expand Down Expand Up @@ -351,8 +329,6 @@ def valid?
return false if @production.nil?
return false if @name.nil?
return false if @description.nil?
mechanism_validator = EnumAttributeValidator.new('String', ["removal", "avoidance"])
return false unless mechanism_validator.valid?(@mechanism)
return false if @country.nil?
return false if @developer.nil?
return false if @average_price_per_tonne_cents_usd.nil?
Expand All @@ -363,16 +339,6 @@ def valid?
true
end

# Custom attribute writer method checking allowed values (enum).
# @param [Object] mechanism Object to be assigned
def mechanism=(mechanism)
validator = EnumAttributeValidator.new('String', ["removal", "avoidance"])
unless validator.valid?(mechanism)
fail ArgumentError, "invalid value for \"mechanism\", must be one of #{validator.allowable_values}."
end
@mechanism = mechanism
end

# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)
Expand Down
2 changes: 1 addition & 1 deletion lib/patch_ruby/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
=end

module Patch
VERSION = '1.21.0'
VERSION = '1.22.0'
end
78 changes: 43 additions & 35 deletions spec/integration/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,48 @@
end
end

describe 'returned fields' do
it 'returns the expected fields' do
project = Patch::Project.retrieve_projects(page: 1).data.first

keys = attributes_for(:project).keys
expect(project.to_hash.keys).to include(*keys)

expect(project.photos).to be_an_instance_of(Array)
expect(project.average_price_per_tonne_cents_usd)
.to be_an_instance_of(Integer)
expect(project.remaining_mass_g).to be_an_instance_of(Integer)
expect(project.longitude).to be_an_instance_of(Float)
expect(project.latitude).to be_an_instance_of(Float)

expect(project.technology_type)
.to be_an_instance_of(Patch::TechnologyType)
expect(project.technology_type.name).to be_an_instance_of(String)
expect(project.technology_type.slug).to be_an_instance_of(String)

parent_type = project.technology_type.parent_technology_type
expect(parent_type).to be_an_instance_of(Patch::ParentTechnologyType)
expect(parent_type.name).to be_an_instance_of(String)
expect(parent_type.slug).to be_an_instance_of(String)

expect(project.highlights).to be_an_instance_of(Array)

inventory = project.inventory
expect(inventory).to be_an_instance_of(Array)
expect(inventory[0]).to be_an_instance_of(Patch::Inventory)
expect(inventory[0].vintage_year).to be_an_instance_of(Integer)
expect(inventory[0].amount_available).to be_an_instance_of(Integer)
expect(inventory[0].price).to be_an_instance_of(Integer)
expect(inventory[0].currency).to be_an_instance_of(String)
expect(inventory[0].unit).to be_an_instance_of(String)
end
it 'returns the expected fields' do
project = Patch::Project.retrieve_projects(page: 1).data.first

keys = attributes_for(:project).keys
expect(project.to_hash.keys).to include(*keys)

expect(project.photos).to be_an_instance_of(Array)
expect(project.average_price_per_tonne_cents_usd)
.to be_an_instance_of(Integer)
expect(project.remaining_mass_g).to be_an_instance_of(Integer)
expect(project.longitude).to be_an_instance_of(Float)
expect(project.latitude).to be_an_instance_of(Float)

expect(project.technology_type)
.to be_an_instance_of(Patch::TechnologyType)
expect(project.technology_type.name).to be_an_instance_of(String)
expect(project.technology_type.slug).to be_an_instance_of(String)

parent_type = project.technology_type.parent_technology_type
expect(parent_type).to be_an_instance_of(Patch::ParentTechnologyType)
expect(parent_type.name).to be_an_instance_of(String)
expect(parent_type.slug).to be_an_instance_of(String)

expect(project.highlights).to be_an_instance_of(Array)

inventory = project.inventory
expect(inventory).to be_an_instance_of(Array)
expect(inventory[0]).to be_an_instance_of(Patch::Inventory)
expect(inventory[0].vintage_year).to be_an_instance_of(Integer)
expect(inventory[0].amount_available).to be_an_instance_of(Integer)
expect(inventory[0].price).to be_an_instance_of(Integer)
expect(inventory[0].currency).to be_an_instance_of(String)
expect(inventory[0].unit).to be_an_instance_of(String)
end

it 'retrieves projects in the requested language' do
projects_response = Patch::Project.retrieve_projects(accept_language: 'fr')

expect(projects_response.data.first.name).to include 'Projet' # French

project_id = projects_response.data.first.id
project_response = Patch::Project.retrieve_project(project_id, accept_language: 'fr')
expect(project_response.data.name).to include 'Projet' # Frenc
end
end

0 comments on commit e8d29a0

Please sign in to comment.