beerbelly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmVjMDJhYTBjMTg2MWQ1NmI1ODVjOGI1OTc3MGRkMTE2MzdjMmI1Yw==
5
+ data.tar.gz: !binary |-
6
+ NjE0M2MzNjE2OWRlODE4MzJlMGQ5ZWJkZTA2NmE5ZTYyMjdiYTk3OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NTI2N2Y5MjE4MDRlNGY2NGExYmViZDgxMGUwYTRlMTY1Yjc2NGI0MjRjODNj
10
+ ZTkxODJiMDNjN2JmOGFiZTRlMDJiZDg3ZGVmODk3OWY4MWFiZjAzZjkzZGQw
11
+ ZDZhMGRmNDlhMjUwMmIxNWE2Y2I3NGIyN2Y3ODdkYjcxYjhiYTI=
12
+ data.tar.gz: !binary |-
13
+ MzhkMmI0ZmFlNGU2ZjRjN2UyZmU1YmY2MzcwOGU5NzA3NzExNjQ3NmM3YWYy
14
+ MTcyYTY0ODYyYWU4ODcyZmI5NjYwMmJjYjQyZWQwMmQyZTU3ZDBmYjVkZTRh
15
+ NjUzZjU2NmY5NWE0OTM2NTM4MjU3OTI2YjQ0OGJiMjkzZTE2ZjE=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,2 @@
1
+ 1.9.3-p392
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in beerbelly.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dinesh Vasudevan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # Beerbelly [![Code Climate](https://codeclimate.com/github/dinks/beerbelly.png)](https://codeclimate.com/github/dinks/beerbelly)
2
+
3
+ - An implementation for The [Open Beer Database](http://openbeerdatabase.com)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'beerbelly'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install beerbelly
18
+
19
+ ## Usage
20
+
21
+ b = Beerbelly.beers
22
+ b.length # 50
23
+ b.first # #<Beerbelly::Beer>
24
+
25
+
26
+ One could query with search strings
27
+
28
+ b = Beerbelly.beers(query: 'Strawberry')
29
+
30
+ ## Generator
31
+
32
+ Generate the initializer using the command
33
+
34
+ `rails generate beerbelly:initializer --access_token=test`
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
43
+
44
+
45
+ [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/dinks/beerbelly/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
46
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'beerbelly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "beerbelly"
8
+ spec.version = Beerbelly::VERSION
9
+ spec.authors = ["Dinesh Vasudevan"]
10
+ spec.email = ["dinesh.vasudevan@gmail.com"]
11
+ spec.description = %q{ An implementation for the Open Beer Database }
12
+ spec.summary = %q{ Users will be able to view all/one beer/brewery }
13
+ spec.homepage = "https://github.com/dinks/beerbelly"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_runtime_dependency "httparty", "~> 0.12.0"
24
+
25
+ # For tests
26
+ spec.add_development_dependency "rspec", "~> 2.14.1"
27
+ spec.add_development_dependency "vcr", "~> 2.7.0"
28
+ spec.add_development_dependency "webmock", "~> 1.15.2"
29
+ end
@@ -0,0 +1,25 @@
1
+ require 'beerbelly/version'
2
+ require 'beerbelly/client'
3
+
4
+ module Beerbelly
5
+
6
+ class << self
7
+ def client
8
+ @client ||= Beerbelly::Client.new
9
+ end
10
+
11
+ private
12
+
13
+ def method_missing(method_name, *arguments, &block)
14
+ return super unless client.respond_to?(method_name)
15
+ client.send(method_name, *arguments, &block)
16
+ end
17
+
18
+ # Always define respond_to_missing? when overriding method_missing
19
+ # http://robots.thoughtbot.com/always-define-respond-to-missing-when-overriding
20
+ def respond_to_missing?(method_name, include_private = false)
21
+ client.respond_to?(method_name) || super
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'uri'
4
+ require 'httparty'
5
+
6
+ require 'beerbelly/configuration'
7
+ require 'beerbelly/client/response'
8
+ require 'beerbelly/client/beer'
9
+ require 'beerbelly/client/brewery'
10
+
11
+ module Beerbelly
12
+ class Client
13
+
14
+ include Beerbelly::Client::Beer
15
+ include Beerbelly::Client::Brewery
16
+
17
+ attr_accessor :configuration
18
+
19
+ def configure
20
+ yield configuration
21
+ end
22
+
23
+ protected
24
+
25
+ def get_resource(path, options={})
26
+ request_resource :get, path, options
27
+ end
28
+
29
+ private
30
+
31
+ def request_resource(method, path, options={})
32
+ resource = URI "#{configuration.base_url}#{path}.json"
33
+ resource.query = URI.encode_www_form sanitize_options(options)
34
+
35
+ response = HTTParty.send(method, resource.to_s)
36
+ end
37
+
38
+ def respond_with_resource(response, type)
39
+ Response.new response, type
40
+ end
41
+
42
+ def configuration
43
+ @configuration ||= Configuration.new
44
+ end
45
+
46
+ ALLOWED_OPTIONS = ['order', 'page', 'per_page', 'query', 'token'].freeze
47
+ def sanitize_options(options)
48
+ options.merge!(configuration.to_hash).reject { |key, value| !ALLOWED_OPTIONS.include?(key.to_s) }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ class Client
5
+ module Beer
6
+ def beers(options={})
7
+ respond_with_resource get_beers(options), 'beers'
8
+ end
9
+
10
+ def beer(id, options={})
11
+ respond_with_resource get_beer(id, options), 'beer'
12
+ end
13
+
14
+ private
15
+
16
+ def get_beers(options={})
17
+ get_resource 'beers', options
18
+ end
19
+
20
+ def get_beer(id, options={})
21
+ get_resource "beers/#{id}", options
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ class Client
5
+ module Brewery
6
+ def breweries(options={})
7
+ respond_with_resource get_breweries(options), 'breweries'
8
+ end
9
+
10
+ def brewery(id, options={})
11
+ respond_with_resource get_brewery(id, options), 'brewery'
12
+ end
13
+
14
+ private
15
+
16
+ def get_breweries(options={})
17
+ get_resource 'breweries', options
18
+ end
19
+
20
+ def get_brewery(id, options={})
21
+ get_resource "breweries/#{id}", options
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ class Client
5
+ class Error < Struct.new(:code)
6
+ def to_s
7
+ case code
8
+ when 400
9
+ 'Invalid query'
10
+ when 401
11
+ 'Unauthorized'
12
+ when 404
13
+ 'Not found'
14
+ when 406
15
+ 'Invalid format'
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'beerbelly/client/resources/brewery'
4
+
5
+ module Beerbelly
6
+ class Beer < Resource
7
+ attr_accessor :id, :name, :description, :abv, :created_at, :updated_at, :brewery
8
+
9
+ def brewery=(b)
10
+ @brewery = Beerbelly::Brewery.build b
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ class Brewery < Resource
5
+ attr_accessor :id, :name, :url, :created_at, :updated_at
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ class Resource
5
+ class << self
6
+ def build(data)
7
+ new_object = new
8
+ data.each do |key, value|
9
+ new_object.send("#{key}=", value) if new_object.respond_to?("#{key}=")
10
+ end
11
+ new_object
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ require "beerbelly/client/error"
4
+ require "beerbelly/client/resources/resource"
5
+ require "beerbelly/client/resources/beer"
6
+ require "beerbelly/client/resources/brewery"
7
+
8
+ module Beerbelly
9
+ class Client
10
+ class Response
11
+ extend Forwardable
12
+
13
+ attr_reader :page, :pages, :total, :objects
14
+
15
+ def_delegators :@objects, :[], :length, :first, :last
16
+
17
+ def initialize(response, type)
18
+ return Beerbelly::Client::Error.new(response.code) if response.code != 200
19
+
20
+ case type
21
+ when 'beers'
22
+ @objects = response[type].collect { |beer| Beerbelly::Beer.build beer }
23
+ when 'beer'
24
+ @objects = [ Beerbelly::Beer.build(response.to_hash) ]
25
+ when 'breweries'
26
+ @objects = response[type].collect { |brewery| Beerbelly::Brewery.build brewery }
27
+ when 'brewery'
28
+ @objects = [ Beerbelly::Brewery.build(response.to_hash) ]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ class Configuration
5
+ attr_accessor :access_token
6
+
7
+ def base_url
8
+ "http://api.openbeerdatabase.com/#{current_version}"
9
+ end
10
+
11
+ def to_hash
12
+ { token: access_token }
13
+ end
14
+
15
+ private
16
+
17
+ def current_version
18
+ 'v1/'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module Beerbelly
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rails/generators'
4
+
5
+ module Beerbelly
6
+ class InitializerGenerator < Rails::Generators::Base
7
+ desc 'Creates the initializer file for Beerbelly'
8
+
9
+ # Option for access_token
10
+ class_option :access_token, token: :string, desc: "Set the access token"
11
+
12
+ def self.source_root
13
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
14
+ end
15
+
16
+ def install
17
+ template 'beerbelly.rb', 'config/initializers/beerbelly.rb'
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: UTF-8
2
+
3
+ # Initializer for Beerbelly
4
+ Beerbelly.configure do |c|
5
+ c.access_token = '<%= options[:access_token] %>'
6
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Beerbelly::Client::Beer do
6
+
7
+ before(:each) do
8
+ BeerbellySetup.configure
9
+ end
10
+
11
+ context 'get beers' do
12
+
13
+ let(:beers) do
14
+ VCR.use_cassette('all_beer') do
15
+ beers = Beerbelly.beers
16
+ end
17
+ end
18
+
19
+ it 'should have 50 entries' do
20
+ expect(beers.length).to eql(50)
21
+ end
22
+
23
+ end
24
+
25
+ context 'get beer' do
26
+
27
+ let(:beer) do
28
+ VCR.use_cassette('one_beer') do
29
+ beers = Beerbelly.beer(1)
30
+ end
31
+ end
32
+
33
+ it 'should have 1 entries' do
34
+ expect(beer.length).to eql(1)
35
+ end
36
+
37
+ end
38
+
39
+ end