brewery_db 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +2 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CHANGELOG.md +15 -0
  6. data/README.md +81 -29
  7. data/Rakefile +5 -0
  8. data/brewery_db.gemspec +6 -5
  9. data/lib/brewery_db.rb +15 -19
  10. data/lib/brewery_db/client.rb +19 -7
  11. data/lib/brewery_db/collection.rb +42 -0
  12. data/lib/brewery_db/config.rb +7 -6
  13. data/lib/brewery_db/mash.rb +18 -0
  14. data/lib/brewery_db/middleware/error_handler.rb +28 -0
  15. data/lib/brewery_db/request.rb +27 -0
  16. data/lib/brewery_db/resource.rb +16 -22
  17. data/lib/brewery_db/resources/beers.rb +2 -2
  18. data/lib/brewery_db/resources/breweries.rb +2 -2
  19. data/lib/brewery_db/resources/brewery.rb +16 -0
  20. data/lib/brewery_db/resources/categories.rb +2 -2
  21. data/lib/brewery_db/resources/glassware.rb +2 -2
  22. data/lib/brewery_db/resources/search.rb +1 -1
  23. data/lib/brewery_db/resources/styles.rb +2 -2
  24. data/lib/brewery_db/response.rb +26 -12
  25. data/lib/brewery_db/version.rb +1 -1
  26. data/lib/brewery_db/web_hook.rb +32 -0
  27. data/spec/brewery_db/client_spec.rb +41 -30
  28. data/spec/brewery_db/config_spec.rb +25 -28
  29. data/spec/brewery_db/{response_spec.rb → mash_spec.rb} +1 -1
  30. data/spec/brewery_db/middleware/error_handler_spec.rb +49 -0
  31. data/spec/brewery_db/resource_spec.rb +35 -22
  32. data/spec/brewery_db/resources/beers_spec.rb +10 -82
  33. data/spec/brewery_db/resources/breweries_spec.rb +10 -54
  34. data/spec/brewery_db/resources/brewery_spec.rb +27 -0
  35. data/spec/brewery_db/resources/categories_spec.rb +10 -38
  36. data/spec/brewery_db/resources/glassware_spec.rb +10 -36
  37. data/spec/brewery_db/resources/search_spec.rb +19 -61
  38. data/spec/brewery_db/resources/styles_spec.rb +10 -72
  39. data/spec/brewery_db/web_hook_spec.rb +79 -0
  40. data/spec/fixtures/BreweryDB_Resource/_get/a_list_of_resources/can_be_enumerated.yml +515 -0
  41. data/spec/fixtures/BreweryDB_Resource/_get/a_not_found_request/raises_an_exception.yml +38 -0
  42. data/spec/fixtures/BreweryDB_Resource/_get/a_not_found_request/sets_the_exception_message_to_the_error_message_in_the_response.yml +38 -0
  43. data/spec/fixtures/BreweryDB_Resource/_get/an_OK_request/name/.yml +54 -0
  44. data/spec/fixtures/BreweryDB_Resources_Beers/_all/fetches_all_of_the_beers_at_once.yml +889 -0
  45. data/spec/fixtures/BreweryDB_Resources_Beers/_find/fetches_only_the_beer_asked_for.yml +61 -0
  46. data/spec/fixtures/BreweryDB_Resources_Breweries/_all/fetches_all_of_the_breweries_at_once.yml +430 -0
  47. data/spec/fixtures/BreweryDB_Resources_Breweries/_find/fetches_only_the_brewery_asked_for.yml +57 -0
  48. data/spec/fixtures/BreweryDB_Resources_Categories/_all/fetches_all_of_the_cagtegories_at_once.yml +49 -0
  49. data/spec/fixtures/BreweryDB_Resources_Categories/_find/fetches_only_the_category_asked_for.yml +39 -0
  50. data/spec/fixtures/BreweryDB_Resources_Glassware/_all/fetches_all_of_the_glassware_at_once.yml +45 -0
  51. data/spec/fixtures/BreweryDB_Resources_Glassware/_find/fetches_only_the_glassware_asked_for.yml +39 -0
  52. data/spec/fixtures/BreweryDB_Resources_Search/_all/fetches_all_of_the_search_results_at_once.yml +1132 -0
  53. data/spec/fixtures/BreweryDB_Resources_Styles/_all/fetches_all_of_the_styles_at_once.yml +1866 -0
  54. data/spec/fixtures/BreweryDB_Resources_Styles/_find/fetches_only_the_style_asked_for.yml +50 -0
  55. data/spec/spec_helper.rb +4 -4
  56. data/spec/support/shared/a_resource.rb +3 -9
  57. data/spec/support/vcr.rb +6 -1
  58. metadata +82 -42
  59. data/spec/brewery_db_spec.rb +0 -18
  60. data/spec/fixtures/beers.yml +0 -445
  61. data/spec/fixtures/breweries.yml +0 -409
  62. data/spec/fixtures/categories.yml +0 -102
  63. data/spec/fixtures/glassware.yml +0 -65
  64. data/spec/fixtures/search.yml +0 -314
  65. data/spec/fixtures/styles.yml +0 -317
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe BreweryDB::Response do
3
+ describe BreweryDB::Mash do
4
4
  it { should be_a(Hashie::Mash) }
5
5
 
6
6
  context '#convert_key' do
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe BreweryDB::Middleware::ErrorHandler do
4
+ subject(:handler) { described_class.new }
5
+
6
+ describe '#on_complete' do
7
+ let(:error_body) { stub(error_message: error_message, status: 'failure') }
8
+ let(:error_message) { 'error' }
9
+ let(:over_ratelimit) { { 'x-ratelimit-remaining' => '0' } }
10
+
11
+ it 'does not raise an error when the status is 200' do
12
+ expect { handler.on_complete(status: 200) }.to_not raise_error
13
+ end
14
+
15
+ it 'raises a bad request error when the status is 400' do
16
+ expect {
17
+ handler.on_complete(status: 400, body: error_body)
18
+ }.to raise_error(BreweryDB::BadRequest, 'Status => 400. Error message => error')
19
+ end
20
+
21
+ it 'raises an rate limit exceeded error when the status is 401 and x-ratelimit-remaining is 0' do
22
+ expect {
23
+ handler.on_complete(status: 401, body: error_body, response_headers: over_ratelimit)
24
+ }.to raise_error(BreweryDB::RateLimitExceeded, 'Status => 401. Error message => error')
25
+ end
26
+
27
+ it 'raises an unauthorized error when the status is 401 and not over rate limit' do
28
+ expect {
29
+ handler.on_complete(status: 401, body: error_body, response_headers: {})
30
+ }.to raise_error(BreweryDB::Unauthorized, 'Status => 401. Error message => error')
31
+ end
32
+
33
+ it 'raises a not found error when the status is 404' do
34
+ expect {
35
+ handler.on_complete(status: 404, body: error_body)
36
+ }.to raise_error(BreweryDB::NotFound, 'Status => 404. Error message => error')
37
+ end
38
+
39
+ context 'the status is unknown' do
40
+ let(:error_message) { 'SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction' }
41
+
42
+ it 'raises an error with the status and error message' do
43
+ expect {
44
+ handler.on_complete(status: 500, body: error_body)
45
+ }.to raise_error(BreweryDB::Error, "Status => 500. Error message => #{error_message}")
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,33 +1,46 @@
1
- require 'spec_helper'
2
-
3
- describe BreweryDB::Resource do
4
- let(:client) { BreweryDB::Client.new }
1
+ # encoding: UTF-8
5
2
 
6
- context '.new' do
7
- it 'accepts a client' do
8
- described_class.new(client).should be_a(described_class)
9
- end
3
+ require 'spec_helper'
10
4
 
11
- it 'raises an exception without a client' do
12
- expect { described_class.new }.to raise_error
5
+ describe BreweryDB::Resource, :resource do
6
+ context '#get', :vcr do
7
+ let(:resource) do
8
+ Class.new(BreweryDB::Resource) {
9
+ def ok
10
+ get('breweries', name: 'Rogue Ales').data
11
+ end
12
+
13
+ def list
14
+ get('breweries', established: 2006).collection
15
+ end
16
+
17
+ def not_found
18
+ get('brewery/NOT_FOUND').data
19
+ end
20
+ }.new(config)
13
21
  end
14
- end
15
-
16
- context '#connection' do
17
- let(:endpoint) { URI.parse(BreweryDB::Config::ENDPOINT) }
18
22
 
19
- subject { described_class.new(client).connection }
23
+ context 'an OK request' do
24
+ subject { resource.ok.first }
20
25
 
21
- it { should be_a(Faraday::Connection) }
26
+ its(:name) { should == 'Rogue Ales' }
27
+ end
22
28
 
23
- its(:'builder.handlers') { should include(FaradayMiddleware::ParseJson) }
29
+ context 'a list of resources' do
30
+ it 'can be enumerated' do
31
+ resource.list.inject(0) { |tally, r| tally + 1 }.should eq 55
32
+ end
33
+ end
24
34
 
25
- its(:scheme) { should == endpoint.scheme }
26
- its(:host) { should == endpoint.host }
27
- its(:path_prefix) { should == endpoint.path }
35
+ context 'a not found request' do
36
+ it 'raises an exception' do
37
+ expect { resource.not_found }.to raise_error(BreweryDB::NotFound)
38
+ end
28
39
 
29
- its(:headers) do
30
- should == { 'User-Agent' => BreweryDB::Config::USER_AGENT }
40
+ it 'sets the exception message to the error message in the response' do
41
+ exception = resource.not_found rescue $!
42
+ exception.message.should match /not\s+found/
43
+ end
31
44
  end
32
45
  end
33
46
  end
@@ -2,92 +2,20 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe BreweryDB::Resources::Beers do
6
- it_behaves_like 'a resource' do
7
- context '#all' do
8
- let(:response) { described_class.new(client).all(abv: '5.5') }
5
+ describe BreweryDB::Resources::Beers, :resource do
6
+ context '#all', :vcr do
7
+ let(:response) { described_class.new(config).all(abv: '5.5') }
9
8
 
10
- subject { response }
11
-
12
- its(:current_page) { should == 1 }
13
- its(:number_of_pages) { should == 8 }
14
- its(:status) { should == 'success' }
15
- its(:data) { should be_a(Array) }
16
- its(:data) { should have(50).results }
17
-
18
- context 'result' do
19
- subject { response.data.first }
20
-
21
- it { should have(14).keys }
22
-
23
- its(:id) { should == 'VjQIQW' }
24
- its(:name) { should == '2011 Christmas Ale' }
25
- its(:description) { should == "Every year since 1975 the brewers at Anchor have brewed a distinctive and unique Christmas Ale, which is available from early November to mid-January. The Ale's recipe is different every year—as is the tree on the label—but the intent with which we offer it remains the same: joy and celebration of the newness of life." }
26
- its(:abv) { should == '5.5' }
27
- its(:glassware_id) { should == 5 }
28
- its(:available_id) { should == 4 }
29
- its(:is_organic) { should == 'N' }
30
- its(:status) { should == 'verified' }
31
- its(:status_display) { should == 'Verified' }
32
- its(:'glass.id') { should == 5 }
33
- its(:'glass.name') { should == 'Pint' }
34
- its(:'glass.create_date') { should == '2012-03-26 04:00:04' }
35
- its(:'available.name') { should == 'Seasonal' }
36
- its(:'available.description') { should == 'Available at the same time of year, every year.' }
37
- its(:style_id) { should == 98 }
38
- its(:'style.id') { should == 98 }
39
- its(:'style.category.id') { should == 23 }
40
- its(:'style.category.name') { should == 'Specialty Beer' }
41
- its(:'style.category.bjcp_category') { should == '23' }
42
- its(:'style.category.create_date') { should == '2012-03-26 04:00:04' }
43
- its(:'style.category_id') { should == 23 }
44
- its(:'style.name') { should == 'Specialty Beer' }
45
- its(:'style.simple_url') { should == 'specialty-beer' }
46
- its(:'style.create_date') { should == '2012-03-26 04:00:04' }
47
- its(:create_date) { should == '2012-03-26 04:00:59' }
48
- its(:update_date) { should be_nil }
49
- end
9
+ it 'fetches all of the beers at once' do
10
+ response.count.should eq 495
50
11
  end
12
+ end
51
13
 
52
- context '#find' do
53
- let(:response) { described_class.new(client).find('vYlBZQ') }
54
-
55
- subject { response }
56
-
57
- its(:status) { should == 'success' }
58
-
59
- context 'data' do
60
- subject { response.data }
61
-
62
- it { should have(16).keys }
14
+ context '#find', :vcr do
15
+ let(:response) { described_class.new(config).find('99Uj1n') }
63
16
 
64
- its(:id) { should == 'vYlBZQ' }
65
- its(:name) { should == 'Peacemaker' }
66
- its(:description) { should == 'Peace is a dangerous thing. Peacemaker is a west coast style American Pale Ale that uses several hop varieties to produces a unique and bountiful hop aroma with well balanced bitterness. A special blend of American and European malts make this a very well rounded, characterful beer.' }
67
- its(:abv) { should == '5.7' }
68
- its(:glassware_id) { should == 5 }
69
- its(:available_id) { should == 1 }
70
- its(:is_organic) { should == 'N' }
71
- its(:status) { should == 'verified' }
72
- its(:status_display) { should == 'Verified' }
73
- its(:'glass.id') { should == 5 }
74
- its(:'glass.name') { should == 'Pint' }
75
- its(:'glass.create_date') { should == '2012-03-26 04:00:04' }
76
- its(:'available.name') { should == 'Year Round' }
77
- its(:'available.description') { should == 'Available year round as a staple beer.' }
78
- its(:style_id) { should == 33 }
79
- its(:'style.id') { should == 33 }
80
- its(:'style.category.id') { should == 10 }
81
- its(:'style.category.name') { should == 'American Ale' }
82
- its(:'style.category.bjcp_category') { should == '10' }
83
- its(:'style.category.create_date') { should == '2012-03-26 04:00:04' }
84
- its(:'style.category_id') { should == 10 }
85
- its(:'style.name') { should == 'American Pale Ale' }
86
- its(:'style.simple_url') { should == 'american-pale-ale' }
87
- its(:'style.create_date') { should == '2012-03-26 04:00:04' }
88
- its(:create_date) { should == '2012-03-26 04:02:10' }
89
- its(:update_date) { should == '2012-03-26 04:38:49' }
90
- end
17
+ it 'fetches only the beer asked for' do
18
+ response.id.should == '99Uj1n'
91
19
  end
92
20
  end
93
21
  end
@@ -2,64 +2,20 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe BreweryDB::Resources::Breweries do
6
- it_behaves_like 'a resource' do
7
- context '#all' do
8
- let(:response) { described_class.new(client).all(established: 2006) }
5
+ describe BreweryDB::Resources::Breweries, :resource do
6
+ context '#all', :vcr do
7
+ let(:response) { described_class.new(config).all(established: 2006) }
9
8
 
10
- subject { response }
11
-
12
- its(:current_page) { should == 1 }
13
- its(:number_of_pages) { should == 1 }
14
- its(:status) { should == 'success' }
15
- its(:data) { should be_a(Array) }
16
- its(:data) { should have(38).results }
17
-
18
- context 'result' do
19
- subject { response.data.first }
20
-
21
- it { should have(9).keys }
22
-
23
- its(:id) { should == 'Qt4daP' }
24
- its(:name) { should == '612 Brew LLC' }
25
- its(:description) { should == "At 612Brew, the newest beer company in the 612, we want to make your newest favorite beer you’ve never had.\n\nWith our operation moving to a larger location in Uptown, we have the opportunity to make and test out much larger batches of hand crafted beer and present them for you to try. You never know what we may brew next, a Porter, a Pilsner, a Bock or a Belgian. We are excited for each new beer that we make and hope you get excited to try them." }
26
- its(:website) { should == 'http://612brew.com/' }
27
- its(:established) { should == '2006' }
28
- its(:is_organic) { should == 'N' }
29
- its(:images) { should be_nil }
30
- its(:status) { should == 'verified' }
31
- its(:status_display) { should == 'Verified' }
32
- its(:create_date) { should == '2012-03-25 04:00:17' }
33
- its(:update_date) { should be_nil }
34
- end
9
+ it 'fetches all of the breweries at once' do
10
+ response.count.should eq 55
35
11
  end
12
+ end
36
13
 
37
- context '#find' do
38
- let(:response) { described_class.new(client).find('d1zSa7') }
39
-
40
- subject { response }
41
-
42
- its(:status) { should == 'success' }
43
-
44
- context 'data' do
45
- subject { response.data }
46
-
47
- it { should have(11).keys }
14
+ context '#find', :vcr do
15
+ let(:response) { described_class.new(config).find('Idm5Y5') }
48
16
 
49
- its(:id) { should == 'd1zSa7' }
50
- its(:name) { should == 'Lonerider Brewing Company' }
51
- its(:description) { should == "We opened our doors on January 23rd, 2009 and since then we have been impressed with the enthusiasm of craft beer aficionados we meet daily. The craft brewing industry is booming and you can find a tremendous variety out there. North Carolina has undoubtedly become the Southern State for beer with Asheville reigning supreme and Raleigh/Durham/CH not far behind. We hope you continue to support the variety and encourage new brewers to experiment with and craft new styles for everybody’s pleasure.\n\n\"Effect change; don't be an audience. Walk your own path, and instead of thinking outside the box, imagine if there was no box.\"" }
52
- its(:website) { should == 'http://www.loneriderbeer.com/' }
53
- its(:established) { should == '2009' }
54
- its(:is_organic) { should == 'N' }
55
- its(:'images.icon') { should == 'http://s3.amazonaws.com/brewerydbapi/brewery/d1zSa7/upload_kwaFB7-icon.png' }
56
- its(:'images.medium') { should == 'http://s3.amazonaws.com/brewerydbapi/brewery/d1zSa7/upload_kwaFB7-medium.png' }
57
- its(:'images.large') { should == 'http://s3.amazonaws.com/brewerydbapi/brewery/d1zSa7/upload_kwaFB7-large.png' }
58
- its(:status) { should == 'verified' }
59
- its(:status_display) { should == 'Verified' }
60
- its(:create_date) { should == '2012-03-25 04:00:30' }
61
- its(:update_date) { should == '2012-03-25 04:40:10' }
62
- end
17
+ it 'fetches only the brewery asked for' do
18
+ response.id.should == 'Idm5Y5'
63
19
  end
64
20
  end
65
21
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe BreweryDB::Resources::Brewery do
4
+ context '#beers' do
5
+ let(:config) { stub }
6
+ let(:id) { 'KlSsWY' }
7
+ let(:response) { stub(data: nil) }
8
+
9
+ subject { described_class.new(config, id: id) }
10
+
11
+ context 'without params' do
12
+ it 'returns the beers for a brewery' do
13
+ subject.should_receive(:get).with('brewery/KlSsWY/beers', {}) { response }
14
+ subject.beers
15
+ end
16
+ end
17
+
18
+ context 'with params' do
19
+ let(:params) { stub }
20
+
21
+ it 'returns the beers for a brewery with params' do
22
+ subject.should_receive(:get).with('brewery/KlSsWY/beers', params) { response }
23
+ subject.beers(params)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -2,48 +2,20 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe BreweryDB::Resources::Categories do
6
- it_behaves_like 'a resource' do
7
- context '#all' do
8
- let(:response) { described_class.new(client).all }
5
+ describe BreweryDB::Resources::Categories, :resource do
6
+ context '#all', :vcr do
7
+ let(:response) { described_class.new(config).all }
9
8
 
10
- subject { response }
11
-
12
- its(:current_page) { should be_nil }
13
- its(:number_of_pages) { should be_nil }
14
- its(:status) { should == 'success' }
15
- its(:data) { should be_a(Array) }
16
- its(:data) { should have(28).results }
17
-
18
- context 'result' do
19
- subject { response.data.first }
20
-
21
- it { should have(4).keys }
22
-
23
- its(:id) { should == 1 }
24
- its(:name) { should == 'Light Lager' }
25
- its(:bjcp_category) { should == '1' }
26
- its(:create_date) { should == '2012-04-05 04:00:04' }
27
- end
9
+ it 'fetches all of the cagtegories at once' do
10
+ response.length.should eq 12
28
11
  end
12
+ end
29
13
 
30
- context '#find' do
31
- let(:response) { described_class.new(client).find(1) }
32
-
33
- subject { response }
34
-
35
- its(:status) { should == 'success' }
36
-
37
- context 'data' do
38
- subject { response.data }
39
-
40
- it { should have(4).keys }
14
+ context '#find', :vcr do
15
+ let(:response) { described_class.new(config).find(1) }
41
16
 
42
- its(:id) { should == 1 }
43
- its(:name) { should == 'Light Lager' }
44
- its(:bjcp_category) { should == '1' }
45
- its(:create_date) { should == '2012-04-05 04:00:04' }
46
- end
17
+ it 'fetches only the category asked for' do
18
+ response.id.should == 1
47
19
  end
48
20
  end
49
21
  end
@@ -2,46 +2,20 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe BreweryDB::Resources::Glassware do
6
- it_behaves_like 'a resource' do
7
- context '#all' do
8
- let(:response) { described_class.new(client).all }
5
+ describe BreweryDB::Resources::Glassware, :resource do
6
+ context '#all', :vcr do
7
+ let(:response) { described_class.new(config).all }
9
8
 
10
- subject { response }
11
-
12
- its(:current_page) { should be_nil }
13
- its(:number_of_pages) { should be_nil }
14
- its(:status) { should == 'success' }
15
- its(:data) { should be_a(Array) }
16
- its(:data) { should have(15).results }
17
-
18
- context 'result' do
19
- subject { response.data.first }
20
-
21
- it { should have(3).keys }
22
-
23
- its(:id) { should == 1 }
24
- its(:name) { should == 'Flute' }
25
- its(:create_date) { should == '2012-01-03 02:41:33' }
26
- end
9
+ it 'fetches all of the glassware at once' do
10
+ response.length.should eq 12
27
11
  end
12
+ end
28
13
 
29
- context '#find' do
30
- let(:response) { described_class.new(client).find(1) }
31
-
32
- subject { response }
33
-
34
- its(:status) { should == 'success' }
35
-
36
- context 'data' do
37
- subject { response.data }
38
-
39
- it { should have(3).keys }
14
+ context '#find', :vcr do
15
+ let(:response) { described_class.new(config).find(1) }
40
16
 
41
- its(:id) { should == 1 }
42
- its(:name) { should == 'Flute' }
43
- its(:create_date) { should == '2012-01-03 02:41:33' }
44
- end
17
+ it 'fetches only the glassware asked for' do
18
+ response.id.should eq 1
45
19
  end
46
20
  end
47
21
  end