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.
@@ -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
@@ -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
- NoBeerId = Class.new(::StandardError)
4
+ MissingParameter = Class.new(::StandardError)
5
5
  HttpError = Class.new(::StandardError)
6
6
  LoadError = Class.new(::StandardError)
7
7
  InvalidResponse = Class.new(::StandardError)
@@ -1,3 +1,3 @@
1
1
  module Tankard
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/tankard.rb CHANGED
@@ -25,5 +25,9 @@ module Tankard
25
25
  return super unless client.respond_to?(method_name)
26
26
  client.send(method_name, *args, &block)
27
27
  end
28
+
29
+ def reset_client
30
+ @client.value = nil
31
+ end
28
32
  end
29
33
  end
@@ -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
@@ -12,6 +12,7 @@ end
12
12
  require 'tankard'
13
13
  require 'rspec'
14
14
  require 'webmock/rspec'
15
+ require 'shared_examples_for_find'
15
16
 
16
17
  WebMock.disable_net_connect!(allow: 'coveralls.io')
17
18
 
@@ -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
- context "when looking up a valid beer" do
23
-
24
- it "returns data on that specific beer" do
25
- expect(beer.find(@valid_beers.first)).to eql("valid1_found")
26
- end
27
- end
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 teh options[:endpoint] to variations" do
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 "when making a request" do
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
- context "the id for a beer is not set" do
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
- it "raises a Tankard::Error::NoBeerId error" do
135
- expect { beer.each { |b| p b } }.to raise_error(Tankard::Error::NoBeerId)
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
- context "the id for a beer is set" do
128
+ describe "private methods" do
140
129
 
141
- before do
142
- @request.stub(:get).with("beer/valid1", {}).and_return({ "data" => { "valid1_found" => "beer", "valid1_found_more" => "more_details" }})
143
- end
130
+ describe "#raise_if_no_id_in_options" do
131
+
132
+ context "when an ID is not set" do
144
133
 
145
- it "uses the beer id in the uri" do
146
- expect(beer.id("valid1").collect { |x| x }).to eql([{"valid1_found" => "beer", "valid1_found_more" => "more_details" }])
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
- context "the endpoint is set" do
139
+ context "when an ID is set" do
151
140
 
152
- before do
153
- @request.stub(:get).with("beer/valid1/breweries", {}).and_return({ "data" => ["valid1_found"]})
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
- it "adds the endpoint to the request" do
157
- expect(beer.id("valid1").breweries.collect { |x| x }).to eql(["valid1_found"])
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
- context "additional options are set" do
163
+ describe "#http_request_uri" do
162
164
 
163
165
  before do
164
- @beer_with_options = Tankard::Api::Beer.new(@request, test: "123", id: "valid1")
165
- @request.stub(:get).with("beer/valid1", Hashie::Mash.new(test: "123")).and_return({ "data" => ["valid1_found"]})
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
- it "passes them to the request" do
169
- expect(@beer_with_options.collect { |x| x }).to eql(["valid1_found"])
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
- context "no data is returned" do
194
+ describe "#http_client" do
174
195
 
175
- before do
176
- @request.stub(:get).with("beer/valid1", {}).and_return({})
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 "gracefully fails" do
180
- expect { beer.id("valid1").collect { |x| x} }.to raise_error(Tankard::Error::InvalidResponse)
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 "when making a request" do
36
+ describe "#params" do
37
37
 
38
- context "and a page is set" do
39
-
40
- it "only queries a single page" do
41
- beers.should_receive(:find_on_single_page)
42
- beers.page(1).each { |x| x }
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
- context "and additional options are set without a page" do
55
-
56
- before do
57
- @beers_with_options = Tankard::Api::Beers.new(@request, test: "123")
58
- @request.stub(:get).with("beers", Hashie::Mash.new(test: "123", p: 1)).and_return({ "data" => ["beers"]})
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
- context "and additional options are set with a page" do
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
- context "and no data is returned on a single page request" do
57
+ describe "private methods" do
79
58
 
80
- before do
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 "raises a Tankard::Error::InvalidResponse error" do
86
- expect { @beers.collect { |x| x} }.to raise_error(Tankard::Error::InvalidResponse)
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
- context "and no data is returned on a page in a multi page request" do
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 "raises a Tankard::Error::InvalidResponse error" do
99
- expect { @beers.collect { |x| x} }.to raise_error(Tankard::Error::InvalidResponse)
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
- context "and a page is not specified" do
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 "runs through all of the pages returned by brewerydb" do
112
- expect(@beers.collect { |x| x}).to eql(["test1", "test2", "test3", "test4"])
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