tankard 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +49 -2
- data/lib/tankard/api/beer.rb +77 -28
- data/lib/tankard/api/beers.rb +45 -31
- data/lib/tankard/api/search.rb +132 -0
- data/lib/tankard/api/style.rb +80 -0
- data/lib/tankard/api/styles.rb +48 -0
- data/lib/tankard/api/utils/find.rb +35 -0
- data/lib/tankard/api/utils/page_finders.rb +67 -0
- data/lib/tankard/client.rb +15 -0
- data/lib/tankard/configuration.rb +5 -6
- data/lib/tankard/error.rb +1 -1
- data/lib/tankard/version.rb +1 -1
- data/lib/tankard.rb +4 -0
- data/spec/shared_examples_for_find.rb +30 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/tankard/api/beer_spec.rb +81 -57
- data/spec/tankard/api/beers_spec.rb +24 -60
- data/spec/tankard/api/search_spec.rb +232 -0
- data/spec/tankard/api/style_spec.rb +61 -0
- data/spec/tankard/api/styles_spec.rb +18 -0
- data/spec/tankard/api/utils/find_spec.rb +27 -0
- data/spec/tankard/api/utils/page_finders_spec.rb +116 -0
- data/spec/tankard/client_spec.rb +59 -0
- data/tankard.gemspec +1 -1
- metadata +20 -3
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'tankard/api/request/get'
|
2
|
+
|
3
|
+
module Tankard
|
4
|
+
module Api
|
5
|
+
module Utils
|
6
|
+
module PageFinders
|
7
|
+
include ::Enumerable
|
8
|
+
include Tankard::Api::Request::Get
|
9
|
+
|
10
|
+
# Loads data from brewerydb and calls supplied block with resulting data
|
11
|
+
#
|
12
|
+
# @yieldparam [Hash] hash containing individual beer information
|
13
|
+
def each(&block)
|
14
|
+
find_on_single_or_all_pages(http_request_uri, http_client, http_request_parameters, block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def find_on_single_or_all_pages(uri, request, options, block)
|
20
|
+
if options[:p]
|
21
|
+
find_on_single_page(uri, request, options, block)
|
22
|
+
else
|
23
|
+
find_on_all_pages(uri, request, options, block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_on_all_pages(uri, request, options, block)
|
28
|
+
page = 0
|
29
|
+
|
30
|
+
begin
|
31
|
+
page += 1
|
32
|
+
options[:p] = page if page > 1
|
33
|
+
total_pages = find_on_single_page(uri, request, options, block)
|
34
|
+
end while page < total_pages
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_on_single_page(uri, request, options, block)
|
38
|
+
response = get_request(request, uri, options)
|
39
|
+
call_block_with_data(response["data"], block)
|
40
|
+
response["numberOfPages"].to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
def call_block_with_data(data, block)
|
44
|
+
raise Tankard::Error::InvalidResponse unless data
|
45
|
+
|
46
|
+
if data.is_a?(Hash)
|
47
|
+
block.call(data)
|
48
|
+
else
|
49
|
+
data.each { |item| block.call(item) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def http_request_uri
|
54
|
+
raise NoMethodError.new("Need to implement method")
|
55
|
+
end
|
56
|
+
|
57
|
+
def http_client
|
58
|
+
raise NoMethodError.new("Need to implement method")
|
59
|
+
end
|
60
|
+
|
61
|
+
def http_request_parameters
|
62
|
+
raise NoMethodError.new("Need to implement method")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/tankard/client.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'tankard/request'
|
2
2
|
require 'tankard/api/beer'
|
3
3
|
require 'tankard/api/beers'
|
4
|
+
require 'tankard/api/search'
|
5
|
+
require 'tankard/api/styles'
|
6
|
+
require 'tankard/api/style'
|
4
7
|
|
5
8
|
module Tankard
|
6
9
|
class Client
|
@@ -20,5 +23,17 @@ module Tankard
|
|
20
23
|
def beers(options={})
|
21
24
|
Tankard::Api::Beers.new(@tankard_request, options)
|
22
25
|
end
|
26
|
+
|
27
|
+
def search(options={})
|
28
|
+
Tankard::Api::Search.new(@tankard_request, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def styles
|
32
|
+
Tankard::Api::Styles.new(@tankard_request)
|
33
|
+
end
|
34
|
+
|
35
|
+
def style(options={})
|
36
|
+
Tankard::Api::Style.new(@tankard_request, options)
|
37
|
+
end
|
23
38
|
end
|
24
39
|
end
|
@@ -8,15 +8,10 @@ module Tankard
|
|
8
8
|
def configure
|
9
9
|
yield self
|
10
10
|
validate_api_key!
|
11
|
+
reset_client
|
11
12
|
self
|
12
13
|
end
|
13
14
|
|
14
|
-
def reset!
|
15
|
-
Tankard::Configuration::KEYS.each do |key|
|
16
|
-
instance_variable_set(:"@{key}", nil)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
15
|
private
|
21
16
|
|
22
17
|
def credentials
|
@@ -30,5 +25,9 @@ module Tankard
|
|
30
25
|
raise Tankard::Error::ConfigurationError, "api_key is not a string"
|
31
26
|
end
|
32
27
|
end
|
28
|
+
|
29
|
+
def reset_client
|
30
|
+
raise Tankard::Error::ConfigurationError, "Implement reset_client"
|
31
|
+
end
|
33
32
|
end
|
34
33
|
end
|
data/lib/tankard/error.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Tankard
|
2
2
|
module Error
|
3
3
|
ConfigurationError = Class.new(::StandardError)
|
4
|
-
|
4
|
+
MissingParameter = Class.new(::StandardError)
|
5
5
|
HttpError = Class.new(::StandardError)
|
6
6
|
LoadError = Class.new(::StandardError)
|
7
7
|
InvalidResponse = Class.new(::StandardError)
|
data/lib/tankard/version.rb
CHANGED
data/lib/tankard.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
shared_examples "the find method" do
|
2
|
+
|
3
|
+
context "when looking up a valid item" do
|
4
|
+
|
5
|
+
it "returns data on the item" do
|
6
|
+
expect(context.find(valid_items.first)).to eql(valid_responses.first)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context "when looking up an invalid item" do
|
11
|
+
|
12
|
+
it "returns nil when not found" do
|
13
|
+
expect(context.find(invalid_items.first)).to eql(nil)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when looking up multiple valid items" do
|
18
|
+
|
19
|
+
it "returns an array of data with each item" do
|
20
|
+
expect(context.find(valid_items)).to eql(valid_responses)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when looking up multiple items and one is invalid" do
|
25
|
+
|
26
|
+
it "returns an array with only the valid items" do
|
27
|
+
expect(context.find(valid_invalid_items)).to eql(valid_responses)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,41 +11,18 @@ describe Tankard::Api::Beer do
|
|
11
11
|
describe "#find" do
|
12
12
|
|
13
13
|
before do
|
14
|
-
@valid_beers = ["valid1", "valid2"]
|
15
|
-
@invalid_beers = ["invalid1", "invalid2"]
|
16
14
|
@request.stub(:get).with("beer/valid1", {}).and_return({ "data" => "valid1_found"})
|
17
15
|
@request.stub(:get).with("beer/valid2", {}).and_return({ "data" => "valid2_found"})
|
18
16
|
@request.stub(:get).with("beer/invalid1", {}).and_raise(Tankard::Error::HttpError)
|
19
17
|
@request.stub(:get).with("beer/invalid2", {}).and_raise(Tankard::Error::HttpError)
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
context "when looking up an invalid beer" do
|
30
|
-
|
31
|
-
it "returns nil when not found" do
|
32
|
-
expect(beer.find(@invalid_beers.first)).to eql(nil)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "when looking up multiple valid beers" do
|
37
|
-
|
38
|
-
it "returns an array of data with each beer" do
|
39
|
-
expect(beer.find(@valid_beers)).to eql(["valid1_found", "valid2_found"])
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "when looking up multiple beers and one is invalid" do
|
44
|
-
|
45
|
-
it "returns an array with only the valid beers" do
|
46
|
-
beer_request = @valid_beers + @invalid_beers
|
47
|
-
expect(beer.find(beer_request)).to eql(["valid1_found", "valid2_found"])
|
48
|
-
end
|
20
|
+
it_should_behave_like "the find method" do
|
21
|
+
let(:context) { beer }
|
22
|
+
let(:valid_items) { ["valid1", "valid2"] }
|
23
|
+
let(:valid_responses) { ["valid1_found", "valid2_found"] }
|
24
|
+
let(:invalid_items) { ["invalid1", "invalid2"] }
|
25
|
+
let(:valid_invalid_items) { valid_items + invalid_items }
|
49
26
|
end
|
50
27
|
end
|
51
28
|
|
@@ -116,7 +93,7 @@ describe Tankard::Api::Beer do
|
|
116
93
|
|
117
94
|
describe "#variations" do
|
118
95
|
|
119
|
-
it "sets
|
96
|
+
it "sets the options[:endpoint] to variations" do
|
120
97
|
beer.variations
|
121
98
|
beer_options = beer.instance_variable_get(:"@options")
|
122
99
|
expect(beer_options[:endpoint]).to eql("variations")
|
@@ -127,57 +104,104 @@ describe Tankard::Api::Beer do
|
|
127
104
|
end
|
128
105
|
end
|
129
106
|
|
130
|
-
describe "
|
107
|
+
describe "#params" do
|
108
|
+
|
109
|
+
it "sets parameters" do
|
110
|
+
beer.params(withSocialAccounts: "y", withGuilds: "n")
|
111
|
+
beer_options = beer.instance_variable_get(:"@options")
|
112
|
+
expect(beer_options[:withSocialAccounts]).to eql("y")
|
113
|
+
expect(beer_options[:withGuilds]).to eql("n")
|
114
|
+
end
|
131
115
|
|
132
|
-
|
116
|
+
it "merges data when called multiple times" do
|
117
|
+
beer.params(test: "n")
|
118
|
+
beer.params(test: "y")
|
119
|
+
beer_options = beer.instance_variable_get(:"@options")
|
120
|
+
expect(beer_options[:test]).to eql("y")
|
121
|
+
end
|
133
122
|
|
134
|
-
|
135
|
-
|
136
|
-
end
|
123
|
+
it "returns itself" do
|
124
|
+
expect(beer.object_id).to eql(beer.params.object_id)
|
137
125
|
end
|
126
|
+
end
|
138
127
|
|
139
|
-
|
128
|
+
describe "private methods" do
|
140
129
|
|
141
|
-
|
142
|
-
|
143
|
-
|
130
|
+
describe "#raise_if_no_id_in_options" do
|
131
|
+
|
132
|
+
context "when an ID is not set" do
|
144
133
|
|
145
|
-
|
146
|
-
|
134
|
+
it "raises Tankard::Error::MissingParameter" do
|
135
|
+
expect { beer.send(:raise_if_no_id_in_options) }.to raise_error(Tankard::Error::MissingParameter, "No Beer ID is set")
|
136
|
+
end
|
147
137
|
end
|
148
|
-
end
|
149
138
|
|
150
|
-
|
139
|
+
context "when an ID is set" do
|
151
140
|
|
152
|
-
|
153
|
-
|
141
|
+
before do
|
142
|
+
beer.instance_variable_get(:"@options")[:id] = "test"
|
143
|
+
end
|
144
|
+
|
145
|
+
it "returns the id from options" do
|
146
|
+
expect(beer.send(:raise_if_no_id_in_options)).to eql("test")
|
147
|
+
end
|
148
|
+
|
149
|
+
it "removes the id from options" do
|
150
|
+
beer.send(:raise_if_no_id_in_options)
|
151
|
+
expect(beer.instance_variable_get(:"@options")[:id]).to be_nil
|
152
|
+
end
|
154
153
|
end
|
154
|
+
end
|
155
155
|
|
156
|
-
|
157
|
-
|
156
|
+
describe "#route" do
|
157
|
+
|
158
|
+
it "returns the route for the api request" do
|
159
|
+
expect(beer.send(:route)).to eql("beer")
|
158
160
|
end
|
159
161
|
end
|
160
162
|
|
161
|
-
|
163
|
+
describe "#http_request_uri" do
|
162
164
|
|
163
165
|
before do
|
164
|
-
|
165
|
-
|
166
|
+
beer.stub!(:route).and_return("beer")
|
167
|
+
beer.stub!(:raise_if_no_id_in_options).and_return("123")
|
166
168
|
end
|
167
169
|
|
168
|
-
|
169
|
-
|
170
|
+
context "no endpoint is set" do
|
171
|
+
|
172
|
+
it "returns the route with the id" do
|
173
|
+
expect(beer.send(:http_request_uri)).to eql("beer/123")
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "endpoint is set" do
|
178
|
+
|
179
|
+
before do
|
180
|
+
beer.instance_variable_get(:"@options")[:endpoint] = "events"
|
181
|
+
end
|
182
|
+
|
183
|
+
it "returns the route with the id and endpoint" do
|
184
|
+
expect(beer.send(:http_request_uri)).to eql("beer/123/events")
|
185
|
+
end
|
186
|
+
|
187
|
+
it "removes the endpoint from options" do
|
188
|
+
beer.send(:http_request_uri)
|
189
|
+
expect(beer.instance_variable_get(:"@options")[:endpoint]).to be_nil
|
190
|
+
end
|
170
191
|
end
|
171
192
|
end
|
172
193
|
|
173
|
-
|
194
|
+
describe "#http_client" do
|
174
195
|
|
175
|
-
|
176
|
-
|
196
|
+
it "returns the request variable that is passed when the class is created" do
|
197
|
+
expect(beer.send(:http_client).object_id).to eql(@request.object_id)
|
177
198
|
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "#http_request_parameters" do
|
178
202
|
|
179
|
-
it "
|
180
|
-
expect
|
203
|
+
it "returns the options for the request" do
|
204
|
+
expect(beer.send(:http_request_parameters).object_id).to eql(beer.instance_variable_get(:"@options").object_id)
|
181
205
|
end
|
182
206
|
end
|
183
207
|
end
|
@@ -33,83 +33,47 @@ describe Tankard::Api::Beers do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
describe "
|
36
|
+
describe "#params" do
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "and a page is not set" do
|
47
|
-
|
48
|
-
it "queries multiple pages" do
|
49
|
-
beers.should_receive(:find_on_all_pages)
|
50
|
-
beers.each { |x| x }
|
51
|
-
end
|
38
|
+
it "sets parameters" do
|
39
|
+
beers.params(withSocialAccounts: "y", withGuilds: "n")
|
40
|
+
beers_options = beers.instance_variable_get(:"@options")
|
41
|
+
expect(beers_options[:withSocialAccounts]).to eql("y")
|
42
|
+
expect(beers_options[:withGuilds]).to eql("n")
|
52
43
|
end
|
53
44
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
it "passes them to the request" do
|
62
|
-
expect(@beers_with_options.collect { |x| x }).to eql(["beers"])
|
63
|
-
end
|
45
|
+
it "merges params when called multiple times" do
|
46
|
+
beers.params(test: "n")
|
47
|
+
beers.params(test: "y")
|
48
|
+
beers_options = beers.instance_variable_get(:"@options")
|
49
|
+
expect(beers_options[:test]).to eql("y")
|
64
50
|
end
|
65
51
|
|
66
|
-
|
67
|
-
|
68
|
-
before do
|
69
|
-
@beers_with_options = Tankard::Api::Beers.new(@request, test: "123", p: 2)
|
70
|
-
@request.stub(:get).with("beers", Hashie::Mash.new(test: "123", p: 2)).and_return({ "data" => ["beers"]})
|
71
|
-
end
|
72
|
-
|
73
|
-
it "passes them to the request" do
|
74
|
-
expect(@beers_with_options.collect { |x| x }).to eql(["beers"])
|
75
|
-
end
|
52
|
+
it "returns itself" do
|
53
|
+
expect(beers.object_id).to eql(beers.params.object_id)
|
76
54
|
end
|
55
|
+
end
|
77
56
|
|
78
|
-
|
57
|
+
describe "private methods" do
|
79
58
|
|
80
|
-
|
81
|
-
@beers = Tankard::Api::Beers.new(@request, p: 1)
|
82
|
-
@request.stub(:get).with("beers", Hashie::Mash.new(p: 1)).and_return({})
|
83
|
-
end
|
59
|
+
describe "#http_request_uri" do
|
84
60
|
|
85
|
-
it "
|
86
|
-
expect
|
61
|
+
it "returns the string beers" do
|
62
|
+
expect(beers.send(:http_request_uri)).to eql("beers")
|
87
63
|
end
|
88
64
|
end
|
89
65
|
|
90
|
-
|
91
|
-
|
92
|
-
before do
|
93
|
-
@beers = Tankard::Api::Beers.new(@request)
|
94
|
-
@request.stub(:get).with("beers", Hashie::Mash.new(p: 1)).and_return({"numberOfPages" => 2, "data" => ["test1", "test2"]})
|
95
|
-
@request.stub(:get).with("beers", Hashie::Mash.new(p: 2)).and_return({})
|
96
|
-
end
|
66
|
+
describe "#http_client" do
|
97
67
|
|
98
|
-
it "
|
99
|
-
expect
|
68
|
+
it "returns the request variable that is passed in when the class is initialized" do
|
69
|
+
expect(beers.send(:http_client).object_id).to eql(@request.object_id)
|
100
70
|
end
|
101
71
|
end
|
102
72
|
|
103
|
-
|
104
|
-
|
105
|
-
before do
|
106
|
-
@beers = Tankard::Api::Beers.new(@request)
|
107
|
-
@request.stub(:get).with("beers", Hashie::Mash.new(p: 1)).and_return({"numberOfPages" => 2, "data" => ["test1", "test2"]})
|
108
|
-
@request.stub(:get).with("beers", Hashie::Mash.new(p: 2)).and_return({"numberOfPages" => 2, "data" => ["test3", "test4"]})
|
109
|
-
end
|
73
|
+
describe "#http_request_parameters" do
|
110
74
|
|
111
|
-
it "
|
112
|
-
expect(
|
75
|
+
it "returns the options for the request" do
|
76
|
+
expect(beers.send(:http_request_parameters).object_id).to eql(beers.instance_variable_get(:"@options").object_id)
|
113
77
|
end
|
114
78
|
end
|
115
79
|
end
|