brewery_db 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +20 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE +22 -0
  5. data/README.md +93 -0
  6. data/Rakefile +3 -0
  7. data/brewery_db.gemspec +22 -0
  8. data/lib/brewery_db.rb +36 -0
  9. data/lib/brewery_db/client.rb +36 -0
  10. data/lib/brewery_db/config.rb +21 -0
  11. data/lib/brewery_db/resource.rb +36 -0
  12. data/lib/brewery_db/resources/beers.rb +13 -0
  13. data/lib/brewery_db/resources/breweries.rb +13 -0
  14. data/lib/brewery_db/resources/categories.rb +13 -0
  15. data/lib/brewery_db/resources/glassware.rb +13 -0
  16. data/lib/brewery_db/resources/search.rb +25 -0
  17. data/lib/brewery_db/resources/styles.rb +13 -0
  18. data/lib/brewery_db/response.rb +18 -0
  19. data/lib/brewery_db/version.rb +3 -0
  20. data/spec/brewery_db/client_spec.rb +44 -0
  21. data/spec/brewery_db/config_spec.rb +54 -0
  22. data/spec/brewery_db/resource_spec.rb +33 -0
  23. data/spec/brewery_db/resources/beers_spec.rb +93 -0
  24. data/spec/brewery_db/resources/breweries_spec.rb +65 -0
  25. data/spec/brewery_db/resources/categories_spec.rb +49 -0
  26. data/spec/brewery_db/resources/glassware_spec.rb +47 -0
  27. data/spec/brewery_db/resources/search_spec.rb +72 -0
  28. data/spec/brewery_db/resources/styles_spec.rb +83 -0
  29. data/spec/brewery_db/response_spec.rb +19 -0
  30. data/spec/brewery_db_spec.rb +18 -0
  31. data/spec/fixtures/beers.yml +445 -0
  32. data/spec/fixtures/breweries.yml +409 -0
  33. data/spec/fixtures/categories.yml +102 -0
  34. data/spec/fixtures/glassware.yml +65 -0
  35. data/spec/fixtures/search.yml +314 -0
  36. data/spec/fixtures/styles.yml +317 -0
  37. data/spec/spec_helper.rb +16 -0
  38. data/spec/support/shared/a_resource.rb +11 -0
  39. data/spec/support/vcr.rb +6 -0
  40. metadata +183 -0
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .rvmrc
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ bin
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tyler Hunt
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.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # BreweryDB
2
+
3
+ A Ruby library for interfacing with the [BreweryDB][] API.
4
+
5
+ [brewerydb]: http://www.brewerydb.com/
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ``` ruby
13
+ gem 'brewery_db'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install brewery_db
23
+
24
+
25
+ ## Configuration
26
+
27
+ You must have a valid API key to use the BreweryDB API. If you don't yet have
28
+ one, you may [request one here][api-key].
29
+
30
+ [api-key]: http://www.brewerydb.com/developers
31
+
32
+ Use the following method to configure your API key:
33
+
34
+ ``` ruby
35
+ BreweryDB.configure do |config|
36
+ config.api_key = API_KEY
37
+ end
38
+ ```
39
+
40
+ If you'd like to use multiple instance of the API with different keys, you may
41
+ instantiate the `BreweryDB::Client` directly and treat those instances the same
42
+ as the `BreweryDB` module:
43
+
44
+ ``` ruby
45
+ brewery_db = BreweryDB::Client.new
46
+
47
+ brewery_db.configure do |config|
48
+ config.api_key = API_KEY
49
+ end
50
+ ```
51
+
52
+
53
+ ## Usage
54
+
55
+ Once the API key has been configured, resources may be called off the module
56
+ directly or off your client instances:
57
+
58
+ ``` ruby
59
+ BreweryDB.beers.all(abv: '5.5')
60
+ BreweryDB.beers.find('vYlBZQ')
61
+
62
+ BreweryDB.breweries.all(established: 2006)
63
+ BreweryDB.breweries.find('d1zSa7')
64
+
65
+ BreweryDB.categories.all
66
+ BreweryDB.categories.find(1)
67
+
68
+ BreweryDB.glassware.all
69
+ BreweryDB.glassware.find(1)
70
+
71
+ BreweryDB.search.all(q: 'IPA')
72
+ BreweryDB.search.beers(q: 'IPA')
73
+ BreweryDB.search.breweries(q: 'IPA')
74
+ BreweryDB.search.guilds(q: 'IPA')
75
+ BreweryDB.search.events(q: 'IPA')
76
+
77
+ BreweryDB.styles.all
78
+ BreweryDB.styles.find(1)
79
+ ```
80
+
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it.
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
86
+ 3. Commit your changes (`git commit -am 'Added some feature'`).
87
+ 4. Push to the branch (`git push origin my-new-feature`).
88
+ 5. Create a new Pull Request.
89
+
90
+
91
+ ## Copyright
92
+
93
+ Copyright © 2012 Tyler Hunt. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ require './lib/brewery_db/version'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'brewery_db'
5
+ gem.version = BreweryDB::VERSION
6
+ gem.summary = 'A Ruby library for interfacing with the BreweryDB API.'
7
+ gem.homepage = 'http://github.com/tylerhunt/brewery_db'
8
+ gem.author = 'Tyler Hunt'
9
+
10
+ gem.required_ruby_version = '>= 1.9'
11
+
12
+ gem.add_dependency 'faraday', '~> 0.8.0'
13
+ gem.add_dependency 'faraday_middleware'
14
+ gem.add_dependency 'hashie', '~> 1.1'
15
+ gem.add_development_dependency 'rspec', '~> 2.0'
16
+ gem.add_development_dependency 'vcr', '~> 2.0'
17
+
18
+ gem.files = `git ls-files`.split($\)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ['lib']
22
+ end
data/lib/brewery_db.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'brewery_db/version'
2
+
3
+ module BreweryDB
4
+ autoload :Client, 'brewery_db/client'
5
+ autoload :Config, 'brewery_db/config'
6
+ autoload :Resource, 'brewery_db/resource'
7
+ autoload :Response, 'brewery_db/response'
8
+
9
+ module Resources
10
+ autoload :Beers, 'brewery_db/resources/beers'
11
+ autoload :Breweries, 'brewery_db/resources/breweries'
12
+ autoload :Categories, 'brewery_db/resources/categories'
13
+ autoload :Glassware, 'brewery_db/resources/glassware'
14
+ autoload :Search, 'brewery_db/resources/search'
15
+ autoload :Styles, 'brewery_db/resources/styles'
16
+ end
17
+
18
+ extend self
19
+
20
+ def respond_to?(method, include_private=false)
21
+ super || client.respond_to?(method, include_private)
22
+ end
23
+
24
+ def method_missing(method, *args, &block)
25
+ if client.respond_to?(method)
26
+ client.send(method, *args, &block)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def client
33
+ @client ||= Client.new
34
+ end
35
+ private :client
36
+ end
@@ -0,0 +1,36 @@
1
+ module BreweryDB
2
+ class Client
3
+ def config
4
+ @config ||= Config.new
5
+ end
6
+
7
+ def configure
8
+ yield(config)
9
+ config
10
+ end
11
+
12
+ def beers
13
+ @beers ||= Resources::Beers.new(self)
14
+ end
15
+
16
+ def breweries
17
+ @breweries ||= Resources::Breweries.new(self)
18
+ end
19
+
20
+ def categories
21
+ @categories ||= Resources::Categories.new(self)
22
+ end
23
+
24
+ def glassware
25
+ @glassware ||= Resources::Glassware.new(self)
26
+ end
27
+
28
+ def search
29
+ @search ||= Resources::Search.new(self)
30
+ end
31
+
32
+ def styles
33
+ @styles ||= Resources::Styles.new(self)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ require 'faraday'
2
+
3
+ module BreweryDB
4
+ class Config
5
+ ADAPTER = Faraday.default_adapter
6
+ ENDPOINT = 'http://api.brewerydb.com/v2'
7
+ MIDDLEWARE = []
8
+ USER_AGENT = "BreweryDB Ruby Gem #{BreweryDB::VERSION}"
9
+
10
+ attr_accessor :adapter
11
+ attr_accessor :api_key
12
+ attr_accessor :endpoint
13
+ attr_accessor :user_agent
14
+
15
+ def initialize
16
+ self.adapter = ADAPTER
17
+ self.endpoint = ENDPOINT
18
+ self.user_agent = USER_AGENT
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module BreweryDB
5
+ class Resource
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ # TODO: Make this private once the appropriate test coverage is in place.
11
+ def connection
12
+ # TODO: Use an instance-level option once faraday_middleware is updated.
13
+ FaradayMiddleware::Mashify.mash_class = Response
14
+
15
+ Faraday.new(
16
+ url: @client.config.endpoint,
17
+ headers: { user_agent: @client.config.user_agent }
18
+ ) do |connection|
19
+ connection.response(:mashify)
20
+ connection.response(:json, content_type: /\bjson$/)
21
+
22
+ connection.adapter(@client.config.adapter)
23
+ end
24
+ end
25
+
26
+ def get(path, params={})
27
+ connection.get(path, default_params.merge(params)).body
28
+ end
29
+ private :get
30
+
31
+ def default_params
32
+ { key: @client.config.api_key }
33
+ end
34
+ private :default_params
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module BreweryDB
2
+ module Resources
3
+ class Beers < Resource
4
+ def all(params={})
5
+ get('beers', params)
6
+ end
7
+
8
+ def find(id, params={})
9
+ get('beer/%s' % id, params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module BreweryDB
2
+ module Resources
3
+ class Breweries < Resource
4
+ def all(params={})
5
+ get('breweries', params)
6
+ end
7
+
8
+ def find(id, params={})
9
+ get('brewery/%s' % id, params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module BreweryDB
2
+ module Resources
3
+ class Categories < Resource
4
+ def all(params={})
5
+ get('categories', params)
6
+ end
7
+
8
+ def find(id, params={})
9
+ get('category/%s' % id, params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module BreweryDB
2
+ module Resources
3
+ class Glassware < Resource
4
+ def all(params={})
5
+ get('glassware', params)
6
+ end
7
+
8
+ def find(id, params={})
9
+ get('glass/%s' % id, params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module BreweryDB
2
+ module Resources
3
+ class Search < Resource
4
+ def all(params={})
5
+ get('search', params)
6
+ end
7
+
8
+ def beers(params={})
9
+ all(params.merge(type: 'beer'))
10
+ end
11
+
12
+ def breweries(params={})
13
+ all(params.merge(type: 'brewery'))
14
+ end
15
+
16
+ def guilds(params={})
17
+ all(params.merge(type: 'guild'))
18
+ end
19
+
20
+ def events(params={})
21
+ all(params.merge(type: 'event'))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module BreweryDB
2
+ module Resources
3
+ class Styles < Resource
4
+ def all(params={})
5
+ get('styles', params)
6
+ end
7
+
8
+ def find(id, params={})
9
+ get('style/%s' % id, params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'hashie'
2
+
3
+ module BreweryDB
4
+ class Response < Hashie::Mash
5
+ def convert_key(key)
6
+ key = key.to_s.dup
7
+ key.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
8
+ key.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
9
+ key.tr('-', '_').downcase
10
+ end
11
+ protected :convert_key
12
+
13
+ def convert_value(value, duping=false)
14
+ value.is_a?(String) ? super.gsub("\r\n", "\n") : super
15
+ end
16
+ protected :convert_value
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module BreweryDB
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe BreweryDB::Client do
4
+ context '#config' do
5
+ subject { described_class.new.config }
6
+
7
+ it { should be_a(BreweryDB::Config) }
8
+
9
+ its(:adapter) { should == BreweryDB::Config::ADAPTER }
10
+ its(:api_key) { should == nil }
11
+ its(:endpoint) { should == BreweryDB::Config::ENDPOINT }
12
+ end
13
+
14
+ context '#configure' do
15
+ subject do
16
+ described_class.new.configure do |config|
17
+ config.adapter = :typhoeus
18
+ config.api_key = 'A1029384756B'
19
+ config.endpoint = 'http://api.playground.brewerydb.com'
20
+ end
21
+ end
22
+
23
+ its(:adapter) { should == :typhoeus }
24
+ its(:api_key) { should == 'A1029384756B' }
25
+ its(:endpoint) { should == 'http://api.playground.brewerydb.com' }
26
+ end
27
+
28
+ {
29
+ beers: BreweryDB::Resources::Beers,
30
+ breweries: BreweryDB::Resources::Breweries,
31
+ categories: BreweryDB::Resources::Categories,
32
+ glassware: BreweryDB::Resources::Glassware,
33
+ search: BreweryDB::Resources::Search,
34
+ styles: BreweryDB::Resources::Styles
35
+ }.each do |method, resource|
36
+ context "##{method}" do
37
+ specify do
38
+ endpoint = resource.new(subject)
39
+ resource.should_receive(:new).and_return(endpoint)
40
+ subject.send(method).should == endpoint
41
+ end
42
+ end
43
+ end
44
+ end