tankard 0.1.0 → 0.2.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.
@@ -4,58 +4,139 @@ describe Tankard::Api::Style do
4
4
  let(:style) { Tankard::Api::Style.new(@request) }
5
5
 
6
6
  before do
7
- @request = mock("request")
7
+ @request = double('request')
8
8
  end
9
9
 
10
- describe "#find" do
10
+ describe '#find' do
11
11
 
12
12
  before do
13
- @request.stub(:get).with("style/1", {}).and_return({ "data" => "valid1_found"})
14
- @request.stub(:get).with("style/2", {}).and_return({ "data" => "valid2_found"})
15
- @request.stub(:get).with("style/3", {}).and_raise(Tankard::Error::HttpError)
16
- @request.stub(:get).with("style/4", {}).and_raise(Tankard::Error::HttpError)
13
+ @request.stub(:get).with('style/1', {}).and_return('data' => 'valid1_found')
14
+ @request.stub(:get).with('style/2', {}).and_return('data' => 'valid2_found')
15
+ @request.stub(:get).with('style/3', {}).and_raise(Tankard::Error::HttpError)
16
+ @request.stub(:get).with('style/4', {}).and_raise(Tankard::Error::HttpError)
17
17
  end
18
18
 
19
- it_should_behave_like "the find method" do
19
+ it_should_behave_like 'the find method' do
20
20
  let(:context) { style }
21
21
  let(:valid_items) { [1, 2] }
22
- let(:valid_responses) { ["valid1_found", "valid2_found"] }
22
+ let(:valid_responses) { %w(valid1_found valid2_found) }
23
23
  let(:invalid_items) { [3, 4] }
24
24
  let(:valid_invalid_items) { valid_items + invalid_items }
25
25
  end
26
26
  end
27
27
 
28
- describe "#id" do
28
+ describe '#id' do
29
29
 
30
- it "sets the options[:id] for the style id passed in" do
30
+ it 'sets the options[:id] for the style id passed in' do
31
31
  style.id(1)
32
- style_options = style.instance_variable_get(:"@options")
32
+ style_options = style.instance_variable_get(:"@http_request_parameters")
33
33
  expect(style_options[:id]).to eql(1)
34
34
  end
35
35
 
36
- it "returns itself" do
36
+ it 'returns itself' do
37
37
  expect(style.object_id).to eql(style.id(1).object_id)
38
38
  end
39
39
  end
40
40
 
41
- describe "when making a request" do
41
+ describe 'when making a request' do
42
42
 
43
- context "and the id for a style is not set" do
43
+ context 'and the id for a style is not set' do
44
44
 
45
- it "raises a Tankard::Error::NoStyleId" do
46
- expect { style.each { |s| p s } }.to raise_error(Tankard::Error::MissingParameter, "No style id set")
45
+ it 'raises a Tankard::Error::NoStyleId' do
46
+ expect { style.each { |s| p s } }.to raise_error(Tankard::Error::MissingParameter, 'No style id set')
47
47
  end
48
48
  end
49
49
 
50
- context "and the id for a style is set" do
50
+ context 'and the id for a style is set' do
51
51
 
52
52
  before do
53
- @request.stub(:get).with("style/1", {}).and_return({ "data" => ["style_valid"] })
53
+ @request.stub(:get).with('style/1', {}).and_return('data' => ['style_valid'])
54
54
  end
55
55
 
56
- it "uses the style id in the uri" do
57
- expect(style.id(1).collect { |x| x}).to eql(["style_valid"])
56
+ it 'uses the style id in the uri' do
57
+ expect(style.id(1).map { |x| x }).to eql(['style_valid'])
58
58
  end
59
59
  end
60
60
  end
61
- end
61
+
62
+ describe 'private methods' do
63
+
64
+ describe '#raise_if_no_id_in_options' do
65
+
66
+ context 'when an ID is not set' do
67
+
68
+ it 'raises Tankard::Error::MissingParameter' do
69
+ expect { style.send(:raise_if_no_id_in_options) }.to raise_error(Tankard::Error::MissingParameter, 'No style id set')
70
+ end
71
+ end
72
+
73
+ context 'when an ID is set' do
74
+
75
+ before do
76
+ style.instance_variable_get(:"@http_request_parameters")[:id] = 'test'
77
+ end
78
+
79
+ it 'returns the id from options' do
80
+ expect(style.send(:raise_if_no_id_in_options)).to eql('test')
81
+ end
82
+
83
+ it 'removes the id from options' do
84
+ style.send(:raise_if_no_id_in_options)
85
+ expect(style.instance_variable_get(:"@http_request_parameters")[:id]).to be_nil
86
+ end
87
+
88
+ it 'can be called multiple times and not raise error' do
89
+ style.send(:raise_if_no_id_in_options)
90
+ expect { style.send(:raise_if_no_id_in_options) }.not_to raise_error
91
+ end
92
+
93
+ it 'caches the ID for future method calls' do
94
+ style.send(:raise_if_no_id_in_options)
95
+ expect(style.send(:raise_if_no_id_in_options)).to eql('test')
96
+ end
97
+
98
+ it 'updates the cache value if the user sets a new ID' do
99
+ style.send(:raise_if_no_id_in_options)
100
+ style.instance_variable_get(:"@http_request_parameters")[:id] = 'test1'
101
+ expect(style.send(:raise_if_no_id_in_options)).to eql('test1')
102
+ end
103
+ end
104
+ end
105
+
106
+ describe '#route' do
107
+
108
+ it 'returns the route for the api request' do
109
+ expect(style.send(:route)).to eql('style')
110
+ end
111
+ end
112
+
113
+ describe '#http_request_uri' do
114
+
115
+ before do
116
+ style.stub(:route).and_return('style')
117
+ style.stub(:raise_if_no_id_in_options).and_return('123')
118
+ end
119
+
120
+ context 'no endpoint is set' do
121
+
122
+ it 'returns the route with the id' do
123
+ expect(style.send(:http_request_uri)).to eql('style/123')
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '#http_client' do
129
+
130
+ it 'returns the request variable that is passed when the class is created' do
131
+ expect(style.send(:http_client).object_id).to eql(@request.object_id)
132
+ end
133
+ end
134
+
135
+ describe '#http_request_parameters' do
136
+
137
+ it 'returns the options for the request' do
138
+ expect(style.send(:http_request_parameters).object_id).to eql(style.instance_variable_get(:"@http_request_parameters").object_id)
139
+ end
140
+ end
141
+ end
142
+ end
@@ -5,14 +5,14 @@ describe Tankard::Api::Styles do
5
5
  let(:styles) { Tankard::Api::Styles.new(@request) }
6
6
 
7
7
  before do
8
- @request = mock("request")
8
+ @request = double('request')
9
9
  end
10
10
 
11
- describe "when making a request" do
11
+ describe 'when making a request' do
12
12
 
13
- it "returns the data portion of the request" do
14
- @request.stub(:get).with("styles", {}).and_return({"data" => ["test1", "test2"]})
15
- expect(styles.collect { |x| x}).to eql(["test1", "test2"])
13
+ it 'returns the data portion of the request' do
14
+ @request.stub(:get).with('styles', {}).and_return('data' => %w(test1 test2))
15
+ expect(styles.map { |x| x }).to eql(%w(test1 test2))
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -4,24 +4,24 @@ describe Tankard::Api::Utils::Find do
4
4
 
5
5
  let(:find) { Class.new { include Tankard::Api::Utils::Find }.new }
6
6
 
7
- describe "#route" do
7
+ describe '#route' do
8
8
 
9
- it "raises NoMethodError" do
10
- expect { find.send(:route) }.to raise_error(NoMethodError, "Must implement and return the base route")
9
+ it 'raises NoMethodError' do
10
+ expect { find.send(:route) }.to raise_error(NoMethodError, 'Must implement and return the base route')
11
11
  end
12
12
  end
13
13
 
14
- describe "#http_client" do
14
+ describe '#http_client' do
15
15
 
16
- it "raises NoMethodError" do
17
- expect { find.send(:http_client) }.to raise_error(NoMethodError, "Must return the http object to make requests with")
16
+ it 'raises NoMethodError' do
17
+ expect { find.send(:http_client) }.to raise_error(NoMethodError, 'Must return the http object to make requests with')
18
18
  end
19
19
  end
20
20
 
21
- describe "#http_request_parameters" do
21
+ describe '#http_request_parameters' do
22
22
 
23
- it "raises NoMethodError" do
24
- expect { find.send(:http_request_parameters) }.to raise_error(NoMethodError, "Must return a hash like structure with request parameters")
23
+ it 'raises NoMethodError' do
24
+ expect { find.send(:http_request_parameters) }.to raise_error(NoMethodError, 'Must return a hash like structure with request parameters')
25
25
  end
26
26
  end
27
- end
27
+ end
@@ -4,113 +4,116 @@ describe Tankard::Api::Utils::PageFinders do
4
4
 
5
5
  let(:finders) { Class.new { include Tankard::Api::Utils::PageFinders }.new }
6
6
 
7
- describe "private methods" do
7
+ describe 'private methods' do
8
8
 
9
9
  before do
10
- @request = mock("request")
10
+ @request = double('request')
11
11
  end
12
12
 
13
- describe "#http_request_uri" do
13
+ describe '#http_request_uri' do
14
14
 
15
- it "raises NoMethodError" do
15
+ it 'raises NoMethodError' do
16
16
  expect { finders.send(:http_request_uri) }.to raise_error(NoMethodError)
17
17
  end
18
18
  end
19
19
 
20
- describe "#http_client" do
20
+ describe '#http_client' do
21
21
 
22
- it "raises NoMethodError" do
22
+ it 'raises NoMethodError' do
23
23
  expect { finders.send(:http_client) }.to raise_error(NoMethodError)
24
24
  end
25
25
  end
26
26
 
27
- describe "#http_request_parameters" do
27
+ describe '#http_request_parameters' do
28
28
 
29
- it "raises NoMethodError" do
29
+ it 'raises NoMethodError' do
30
30
  expect { finders.send(:http_request_parameters) }.to raise_error(NoMethodError)
31
31
  end
32
32
  end
33
33
 
34
- describe "#call_block_with_data" do
34
+ describe '#call_block_with_data' do
35
35
 
36
- it "raises Tankard::Error::InvalidResponse when no data" do
36
+ it 'raises Tankard::Error::InvalidResponse when no data' do
37
37
  expect { finders.send(:call_block_with_data, nil, nil) }.to raise_error(Tankard::Error::InvalidResponse)
38
38
  end
39
39
 
40
- it "accepts a hash of data" do
40
+ it 'accepts a hash of data' do
41
41
  result = []
42
42
  block = -> n { result.push(n) }
43
- finders.send(:call_block_with_data, {"test" => "something"}, block)
44
- expect(result).to eql([{"test"=> "something"}])
43
+ finders.send(:call_block_with_data, { 'test' => 'something' }, block)
44
+ expect(result).to eql([{ 'test' => 'something' }])
45
45
  end
46
46
 
47
- it "loops through an array of data" do
47
+ it 'loops through an array of data' do
48
48
  result = []
49
- block = -> n { result.push(n+1) }
50
- finders.send(:call_block_with_data, [1,2,3], block)
51
- expect(result).to eql([2,3,4])
49
+ block = -> n { result.push(n + 1) }
50
+ finders.send(:call_block_with_data, [1, 2, 3], block)
51
+ expect(result).to eql([2, 3, 4])
52
52
  end
53
53
  end
54
54
 
55
- describe "#find_on_single_page" do
55
+ describe '#find_on_single_page' do
56
56
 
57
- it "sends response[data] to call_block_with_data" do
58
- finders.stub!(:get_request).and_return({"data" => ["test"]})
59
- finders.should_receive(:call_block_with_data).with(["test"], nil)
60
- finders.send(:find_on_single_page, "test", @request, {}, nil)
57
+ before do
58
+ finders.stub(:http_request_uri).and_return('test')
59
+ finders.stub(:http_client).and_return(@request)
60
+ finders.stub(:call_block_with_data).with(['test'], nil)
61
+ end
62
+
63
+ it 'sends response[data] to call_block_with_data' do
64
+ finders.stub(:get_request).and_return('data' => ['test'])
65
+ finders.send(:find_on_single_page, {}, nil)
61
66
  end
62
67
 
63
- it "returns 0 when number of pages is not set" do
64
- finders.stub!(:get_request).and_return({"data" => ["test"]})
65
- finders.stub!(:call_block_with_data).with(["test"], nil)
66
- expect(finders.send(:find_on_single_page, "test", @request, {}, nil)).to eql(0)
68
+ it 'returns 0 when number of pages is not set' do
69
+ finders.stub(:get_request).and_return('data' => ['test'])
70
+ expect(finders.send(:find_on_single_page, {}, nil)).to eql(0)
67
71
  end
68
72
 
69
- it "returns a value when number of pages is set" do
70
- finders.stub!(:get_request).and_return({"data" => ["test"], "numberOfPages" => "3"})
71
- finders.stub!(:call_block_with_data).with(["test"], nil)
72
- expect(finders.send(:find_on_single_page, "test", @request, {}, nil)).to eql(3)
73
+ it 'returns a value when number of pages is set' do
74
+ finders.stub(:get_request).and_return('data' => ['test'], 'numberOfPages' => '3')
75
+ expect(finders.send(:find_on_single_page, {}, nil)).to eql(3)
73
76
  end
74
77
 
75
78
  end
76
79
 
77
- describe "#find_on_all_pages" do
80
+ describe '#find_on_all_pages' do
78
81
 
79
- it "only sets the page when the page is greater than 1" do
80
- finders.should_receive(:find_on_single_page).with("test", @request, {}, nil).and_return(2)
81
- finders.should_not_receive(:find_on_single_page).with("test", @request, {p:1}, nil)
82
- finders.should_receive(:find_on_single_page).with("test", @request, {p:2}, nil).and_return(2)
82
+ it 'only sets the page when the page is greater than 1' do
83
+ finders.should_receive(:find_on_single_page).with({}, nil).and_return(2)
84
+ finders.should_not_receive(:find_on_single_page).with({ p: 1 }, nil)
85
+ finders.should_receive(:find_on_single_page).with({ p: 2 }, nil).and_return(2)
83
86
 
84
- finders.send(:find_on_all_pages, "test", @request, {}, nil)
87
+ finders.send(:find_on_all_pages, {}, nil)
85
88
  end
86
89
  end
87
90
 
88
- describe "#find_on_single_or_all_pages" do
91
+ describe '#find_on_single_or_all_pages' do
89
92
 
90
- it "calls find_with_options when a page is set in options" do
91
- finders.should_receive(:find_on_single_page).with("test", nil, {p:2}, nil)
92
- finders.send(:find_on_single_or_all_pages, "test", nil, {p:2}, nil)
93
+ it 'calls find_with_options when a page is set in options' do
94
+ finders.should_receive(:find_on_single_page).with({ p: 2 }, nil)
95
+ finders.send(:find_on_single_or_all_pages, { p: 2 }, nil)
93
96
  end
94
97
 
95
- it "calls find_on_all_pages when a page is not set in options" do
96
- finders.should_receive(:find_on_all_pages).with("test", nil, {}, nil)
97
- finders.send(:find_on_single_or_all_pages, "test", nil, {}, nil)
98
+ it 'calls find_on_all_pages when a page is not set in options' do
99
+ finders.should_receive(:find_on_all_pages).with({}, nil)
100
+ finders.send(:find_on_single_or_all_pages, {}, nil)
98
101
  end
99
102
  end
100
103
 
101
- describe "#each" do
104
+ describe '#each' do
102
105
 
103
106
  before do
104
- finders.stub!(:http_request_uri).and_return("test")
105
- finders.stub!(:http_client).and_return(nil)
106
- finders.stub!(:http_request_parameters).and_return({})
107
+ finders.stub(:http_request_uri).and_return('test')
108
+ finders.stub(:http_client).and_return(nil)
109
+ finders.stub(:http_request_parameters).and_return({})
107
110
  end
108
111
 
109
- it "calls find_on_single_or_all_pages" do
110
- finders.should_receive(:find_on_single_or_all_pages).with("test", nil, {}, nil)
112
+ it 'calls find_on_single_or_all_pages' do
113
+ finders.should_receive(:find_on_single_or_all_pages).with({}, nil)
111
114
  finders.each
112
115
  end
113
116
  end
114
117
  end
115
118
 
116
- end
119
+ end
@@ -2,112 +2,112 @@ require 'spec_helper'
2
2
 
3
3
  describe Tankard::Client do
4
4
 
5
- let(:client) { Tankard::Client.new({api_key: "abc123"}) }
5
+ let(:client) { Tankard::Client.new(api_key: 'abc123') }
6
6
 
7
- describe "#beer" do
7
+ describe '#beer' do
8
8
 
9
- context "when called" do
9
+ context 'when called' do
10
10
 
11
- it "does not reuse an existing beer object" do
11
+ it 'does not reuse an existing beer object' do
12
12
  first_beer = client.beer
13
13
  expect(first_beer.object_id != client.beer.object_id).to be_true
14
14
  end
15
15
  end
16
16
 
17
- context "when passed a hash of options" do
17
+ context 'when passed a hash of options' do
18
18
 
19
19
  before do
20
- @request = mock("request")
21
- Tankard::Request.stub!(:new).and_return(@request)
20
+ @request = double('request')
21
+ Tankard::Request.stub(:new).and_return(@request)
22
22
  end
23
23
 
24
- it "passes the options to the beer object" do
25
- Tankard::Api::Beer.should_receive(:new).with(@request, {test: 123})
26
- client.beer({test: 123})
24
+ it 'passes the options to the beer object' do
25
+ Tankard::Api::Beer.should_receive(:new).with(@request, test: 123)
26
+ client.beer(test: 123)
27
27
  end
28
28
  end
29
29
  end
30
30
 
31
- describe "#beers" do
31
+ describe '#beers' do
32
32
 
33
- context "when called" do
33
+ context 'when called' do
34
34
 
35
- it "does not reuse an existing beer object" do
35
+ it 'does not reuse an existing beer object' do
36
36
  beers = client.beers
37
37
  expect(beers.object_id != client.beers.object_id).to be_true
38
38
  end
39
39
  end
40
40
 
41
- context "when passed a hash of options" do
41
+ context 'when passed a hash of options' do
42
42
 
43
- before do
44
- @request = mock("request")
45
- Tankard::Request.stub!(:new).and_return(@request)
43
+ before do
44
+ @request = double('request')
45
+ Tankard::Request.stub(:new).and_return(@request)
46
46
  end
47
47
 
48
- it "passes the options to the beer object" do
49
- Tankard::Api::Beers.should_receive(:new).with(@request, {test: 123})
50
- client.beers({test: 123})
48
+ it 'passes the options to the beer object' do
49
+ Tankard::Api::Beers.should_receive(:new).with(@request, test: 123)
50
+ client.beers(test: 123)
51
51
  end
52
52
  end
53
53
  end
54
54
 
55
- describe "#search" do
55
+ describe '#search' do
56
56
 
57
- context "when called" do
57
+ context 'when called' do
58
58
 
59
- it "does not reuse an existing search object" do
59
+ it 'does not reuse an existing search object' do
60
60
  search = client.search
61
61
  expect(search.object_id).not_to eql(client.search.object_id)
62
62
  end
63
63
  end
64
64
 
65
- context "when passed a hash of options" do
65
+ context 'when passed a hash of options' do
66
66
 
67
67
  before do
68
- @request = mock("request")
69
- Tankard::Request.stub!(:new).and_return(@request)
68
+ @request = double('request')
69
+ Tankard::Request.stub(:new).and_return(@request)
70
70
  end
71
71
 
72
- it "passes the options to the search object" do
73
- Tankard::Api::Search.should_receive(:new).with(@request, { test: 123 })
74
- client.search({ test: 123 })
72
+ it 'passes the options to the search object' do
73
+ Tankard::Api::Search.should_receive(:new).with(@request, test: 123)
74
+ client.search(test: 123)
75
75
  end
76
76
  end
77
77
  end
78
78
 
79
- describe "#styles" do
79
+ describe '#styles' do
80
80
 
81
- context "when called" do
81
+ context 'when called' do
82
82
 
83
- it "does not reuse an existing styles object" do
83
+ it 'does not reuse an existing styles object' do
84
84
  styles = client.styles
85
85
  expect(styles.object_id != client.styles.object_id).to be_true
86
86
  end
87
87
  end
88
88
  end
89
89
 
90
- describe "#style" do
90
+ describe '#style' do
91
91
 
92
- context "when called" do
92
+ context 'when called' do
93
93
 
94
- it "does not reuse an existing style object" do
94
+ it 'does not reuse an existing style object' do
95
95
  style = client.style
96
96
  expect(style.object_id).not_to eql(client.style.object_id)
97
97
  end
98
98
  end
99
99
 
100
- context "when passed a hash of options" do
100
+ context 'when passed a hash of options' do
101
101
 
102
102
  before do
103
- @request = mock("request")
104
- Tankard::Request.stub!(:new).and_return(@request)
103
+ @request = double('request')
104
+ Tankard::Request.stub(:new).and_return(@request)
105
105
  end
106
106
 
107
- it "passes the options to the style object" do
108
- Tankard::Api::Style.should_receive(:new).with(@request, { test: 123 })
109
- client.style({ test: 123 })
107
+ it 'passes the options to the style object' do
108
+ Tankard::Api::Style.should_receive(:new).with(@request, test: 123)
109
+ client.style(test: 123)
110
110
  end
111
111
  end
112
112
  end
113
- end
113
+ end