Skip to content

Commit

Permalink
add adjuncts route
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewshafer committed Apr 14, 2014
1 parent a618082 commit f48f22d
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/tankard/api/adjuncts.rb
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
5 changes: 5 additions & 0 deletions lib/tankard/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'tankard/api/search'
require 'tankard/api/styles'
require 'tankard/api/style'
require 'tankard/api/adjuncts'

module Tankard
# Interaction point for various endpoints
Expand All @@ -20,6 +21,10 @@ def initialize(options = {})
@tankard_request = Tankard::Request.new(@api_key)
end

def adjuncts(options = {})
Tankard::Api::Adjuncts.new(@tankard_request, options)
end

def beer(options = {})
Tankard::Api::Beer.new(@tankard_request, options)
end
Expand Down
66 changes: 66 additions & 0 deletions spec/tankard/api/adjuncts_spec.rb
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
24 changes: 24 additions & 0 deletions spec/tankard/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@

let(:client) { Tankard::Client.new(api_key: 'abc123') }

describe '#adjuncts' do

context 'when called' do

it 'does not reuse an existing adjuncts object' do
first_adjuncts = client.adjuncts
expect(first_adjuncts.object_id).to_not eql(client.adjuncts.object_id)
end
end

context 'when passed a hash of options' do

before do
@request = double('request')
Tankard::Request.stub(:new).and_return(@request)
end

it 'passes the options to the beer object' do
Tankard::Api::Adjuncts.should_receive(:new).with(@request, test: 123)
client.adjuncts(test: 123)
end
end
end

describe '#beer' do

context 'when called' do
Expand Down

0 comments on commit f48f22d

Please sign in to comment.