Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewshafer committed Apr 30, 2013
0 parents commit 7abce58
Show file tree
Hide file tree
Showing 20 changed files with 788 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
33 changes: 33 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
language: ruby
rvm:
- jruby-19mode
- rbx-19mode
- 1.9.3
- 2.0.0
- ruby-head
- jruby-head
jdk:
- openjdk6
- openjdk7
- oraclejdk7
matrix:
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
exclude:
- rvm: 1.9.3
jdk: openjdk7
- rvm: 1.9.3
jdk: oraclejdk7
- rvm: 2.0.0
jdk: openjdk7
- rvm: 2.0.0
jdk: oraclejdk7
- rvm: ruby-head
jdk: openjdk7
- rvm: ruby-head
jdk: oraclejdk7
- rvm: rbx-19mode
jdk: openjdk7
- rvm: rbx-19mode
jdk: oraclejdk7
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in tankard.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 Matthew Shafer

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task default: :spec
29 changes: 29 additions & 0 deletions lib/tankard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "tankard/version"
require "tankard/configuration"
require "tankard/error"
require "tankard/client"
require "atomic"

module Tankard
@client = ::Atomic.new

class << self
include Configuration

def client
@client.compare_and_swap(nil, Tankard::Client.new(credentials)) unless @client.value
@client.value
end

def respond_to?(method)
return client.respond_to?(method)
end

private

def method_missing(method_name, *args, &block)
return super unless client.respond_to?(method_name)
client.send(method_name, *args, &block)
end
end
end
94 changes: 94 additions & 0 deletions lib/tankard/api/beer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'hashie'

module Tankard
module Api
class Beer
include ::Enumerable

def initialize(request, options={})
@request = request
@options = Hashie::Mash.new(options)
end

def each(&block)
find_on_single_page(block)
end

def find(beer_id, options={})
@options.merge!(options)

if beer_id.is_a?(Array)
beer_id.map { |beer| request_data_with_nil_on_error("beer/#{beer}", @options) }.compact
else
request_data_with_nil_on_error("beer/#{beer_id}", @options)
end
end

def id(beer_id)
@options.id = beer_id
self
end

def breweries
@options.endpoint = "breweries"
self
end

def events
@options.endpoint = "events"
self
end

def ingredients
@options.endpoint = "ingredients"
self
end

def social_accounts
@options.endpoint = "socialaccounts"
self
end

def variations
@options.endpoint = "variations"
self
end

private

def request_data_from_options
endpoint = "beer/#{raise_if_no_id_in_options}"

if @options.endpoint?
endpoint += "/#{@options.delete(:endpoint)}"
end

request_data_with_nil_on_error(endpoint, @options)
end

def raise_if_no_id_in_options
raise Tankard::Error::NoBeerId unless @options.id?
@options.delete(:id)
end

def find_on_single_page(block)
data = request_data_from_options
raise Tankard::Error::InvalidResponse unless data

if data.is_a?(Hash)
block.call(data)
else
data.each { |beer| block.call(beer) }
end
end

def request_data_with_nil_on_error(uri, options)
begin
@request.get(uri, options)["data"]
rescue Tankard::Error::HttpError
nil
end
end
end
end
end
76 changes: 76 additions & 0 deletions lib/tankard/api/beers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require 'hashie'

module Tankard
module Api
class Beers
include ::Enumerable

def initialize(request, options={})
@request = request
@options = Hashie::Mash.new(options)
end

def each(&block)
if options_have_page_set
find_on_single_page(block)
else
find_on_all_pages(block)
end
end

def name(beer_name)
@options[:name] = beer_name
self
end

def page(number)
@options[:p] = number
self
end

private

def options_have_page_set
# need to make the keys respond to both symbol and string
@options.has_key?(:p)
end

def find_on_all_pages(block)
page = 0
options = @options.clone
begin
page += 1
options[:p] = page
response = request_with_nil_on_error("beers", options)
total_pages = response["numberOfPages"]
data = response["data"]
data.each { |beer| block.call(beer) }
end while page < total_pages
end

def find_on_single_page(block)
data = request_data_with_nil_on_error("beers", @options)
raise Tankard::Error::InvalidResponse unless data

if data.is_a?(Hash)
block.call(data)
else
data.each { |beer| block.call(beer) }
end
end

def request_data_with_nil_on_error(uri, options)
request_body = request_with_nil_on_error(uri, options)
request_body ? request_body["data"] : nil
end

def request_with_nil_on_error(uri, options)
begin
@request.get(uri, options)
rescue Tankard::Error::HttpError
nil
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/tankard/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'tankard/request'
require 'tankard/api/beer'
require 'tankard/api/beers'

module Tankard
class Client

def initialize(options={})
Tankard::Configuration::KEYS.each do |key|
instance_variable_set(:"@#{key}", options[key])
end

@tankard_request = Tankard::Request.new(@api_key)
end

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

def beers(options={})
Tankard::Api::Beers.new(@tankard_request, options)
end
end
end
34 changes: 34 additions & 0 deletions lib/tankard/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Tankard
module Configuration
attr_writer :api_key

KEYS = [:api_key]
BREWERYDB_URL = "http://api.brewerydb.com/v2/"

def configure
yield self
validate_api_key!
self
end

def reset!
Tankard::Configuration::KEYS.each do |key|
instance_variable_set(:"@{key}", nil)
end
end

private

def credentials
{
api_key: @api_key
}
end

def validate_api_key!
unless @api_key.is_a?(String)
raise Tankard::Error::ConfigurationError, "api_key is not a string"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/tankard/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Tankard
module Error
ConfigurationError = Class.new(::StandardError)
NoBeerId = Class.new(::StandardError)
HttpError = Class.new(::StandardError)
LoadError = Class.new(::StandardError)
InvalidResponse = Class.new(::StandardError)
end
end
Loading

0 comments on commit 7abce58

Please sign in to comment.