-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a618082
commit f48f22d
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'hashie' | ||
require 'tankard/api/utils/page_finders' | ||
|
||
module Tankard | ||
module Api | ||
# Access for the /adjuncts route on brewerydb | ||
# | ||
# @see http://www.brewerydb.com/developers/docs-endpoint/adjunct_index | ||
# @author Matthew Shafer | ||
class Adjuncts | ||
include Tankard::Api::Utils::PageFinders | ||
# @!parse include ::Enumerable | ||
|
||
# Initialize a new object | ||
# | ||
# @param request [Tankard::Request] | ||
# @param options [Hash] | ||
# @return [Tankard::Api::Adjuncts] | ||
def initialize(request, options = {}) | ||
@http_client = request | ||
@http_request_parameters = Hashie::Mash.new(options) | ||
end | ||
|
||
# @!method each(&block) | ||
# Calls the given block once for each adjunct | ||
# | ||
# @yieldparam [Hash] hash containing individual adjunct information | ||
# @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid | ||
# @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api | ||
# @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned | ||
# @raise [Tankard::Error::LoadError] when multi json is unable to decode json | ||
|
||
# page number to query | ||
# | ||
# @param beer_id [String] | ||
# @return [self] returns itself | ||
def page(number) | ||
@http_request_parameters.p = number | ||
self | ||
end | ||
|
||
# Additional parameters to send with the request | ||
# | ||
# @param options [Hash] | ||
# @return [self] returns itself | ||
def params(options = {}) | ||
options.each_pair do |key, value| | ||
@http_request_parameters[key] = value | ||
end | ||
self | ||
end | ||
|
||
private | ||
|
||
attr_reader :http_client | ||
attr_reader :http_request_parameters | ||
|
||
def http_request_uri | ||
'adjuncts' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'spec_helper' | ||
|
||
describe Tankard::Api::Adjuncts do | ||
let(:adjuncts) { Tankard::Api::Adjuncts.new(@request) } | ||
|
||
before do | ||
@request = double('request') | ||
end | ||
|
||
describe '#page' do | ||
|
||
it 'sets the http_request_parameters[:p] for the page number' do | ||
adjuncts.page(1) | ||
adjuncts_options = adjuncts.instance_variable_get(:"@http_request_parameters") | ||
expect(adjuncts_options[:p]).to eql(1) | ||
end | ||
|
||
it 'returns self' do | ||
expect(adjuncts.object_id).to eql(adjuncts.page(1).object_id) | ||
end | ||
end | ||
|
||
describe '#params' do | ||
|
||
it 'sets parameters' do | ||
adjuncts.params(p: 5) | ||
adjuncts_options = adjuncts.instance_variable_get(:"@http_request_parameters") | ||
expect(adjuncts_options[:p]).to eql(5) | ||
end | ||
|
||
it 'merges params when called multiple times' do | ||
adjuncts.params(p: 5) | ||
adjuncts.params(p: 8) | ||
adjuncts_options = adjuncts.instance_variable_get(:"@http_request_parameters") | ||
expect(adjuncts_options[:p]).to eql(8) | ||
end | ||
|
||
it 'returns itself' do | ||
expect(adjuncts.object_id).to eql(adjuncts.params.object_id) | ||
end | ||
end | ||
|
||
describe 'private method' do | ||
|
||
describe '#http_request_uri' do | ||
|
||
it 'returns the string adjuncts' do | ||
expect(adjuncts.send(:http_request_uri)).to eql('adjuncts') | ||
end | ||
end | ||
|
||
describe '#http_client' do | ||
|
||
it 'returns the request variable that is passed to the class when initialized' do | ||
expect(adjuncts.send(:http_client).object_id).to eql(@request.object_id) | ||
end | ||
end | ||
|
||
describe '#http_request_parameters' do | ||
|
||
it 'returns the http_request_parameters for the request' do | ||
expect(adjuncts.send(:http_request_parameters).object_id).to eql(adjuncts.instance_variable_get(:"@http_request_parameters").object_id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters