Skip to content

Commit

Permalink
Merge pull request #5 from matthewshafer/fix-repeat-queries
Browse files Browse the repository at this point in the history
fixes repeat calls to some queries
  • Loading branch information
matthewshafer committed Mar 24, 2014
2 parents fbd9163 + fc78537 commit 9909a39
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lib/tankard/api/beer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ def params(options = {})
attr_reader :http_request_parameters

def http_request_uri
@request_endpoint = "/#{@http_request_parameters.delete(:endpoint)}" if @http_request_parameters.endpoint?
endpoint = "#{route}/#{raise_if_no_id_in_options}"

endpoint += "/#{@http_request_parameters.delete(:endpoint)}" if @http_request_parameters.endpoint?

endpoint += @request_endpoint if @request_endpoint
endpoint
end

def raise_if_no_id_in_options
fail Tankard::Error::MissingParameter, 'No Beer ID is set' unless @http_request_parameters.id?
@http_request_parameters.delete(:id)
@beer_id = @http_request_parameters.delete(:id) if @http_request_parameters.id?
fail Tankard::Error::MissingParameter, 'No Beer ID is set' unless @beer_id
@beer_id
end

def route
Expand Down
5 changes: 2 additions & 3 deletions lib/tankard/api/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ def geo_point(latitude, longitude)
attr_reader :http_request_parameters

def http_request_uri
@request_endpoint = "/#{@http_request_parameters.delete(:endpoint)}" if @http_request_parameters.endpoint?
endpoint = 'search'

endpoint += "/#{@http_request_parameters.delete(:endpoint)}" if @http_request_parameters.endpoint?

endpoint += @request_endpoint if @request_endpoint
endpoint
end

Expand Down
5 changes: 3 additions & 2 deletions lib/tankard/api/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def id(style_id)
attr_reader :http_request_parameters

def raise_if_no_id_in_options
fail Tankard::Error::MissingParameter, 'No style id set' unless @http_request_parameters.id?
@http_request_parameters.delete(:id)
@style_id = @http_request_parameters.delete(:id) if @http_request_parameters.id?
fail Tankard::Error::MissingParameter, 'No style id set' unless @style_id
@style_id
end

def route
Expand Down
27 changes: 27 additions & 0 deletions spec/tankard/api/beer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@
beer.send(:raise_if_no_id_in_options)
expect(beer.instance_variable_get(:"@http_request_parameters")[:id]).to be_nil
end

it 'can be called multiple times and not raise error' do
beer.send(:raise_if_no_id_in_options)
expect { beer.send(:raise_if_no_id_in_options) }.not_to raise_error
end

it 'caches the ID for future method calls' do
beer.send(:raise_if_no_id_in_options)
expect(beer.send(:raise_if_no_id_in_options)).to eql('test')
end

it 'updates the cache value if the user sets a new ID' do
beer.send(:raise_if_no_id_in_options)
beer.instance_variable_get(:"@http_request_parameters")[:id] = 'test1'
expect(beer.send(:raise_if_no_id_in_options)).to eql('test1')
end
end
end

Expand Down Expand Up @@ -188,6 +204,17 @@
beer.send(:http_request_uri)
expect(beer.instance_variable_get(:"@http_request_parameters")[:endpoint]).to be_nil
end

it 'caches the endpoint for future method calls' do
beer.send(:http_request_uri)
expect(beer.send(:http_request_uri)).to eql('beer/123/events')
end

it 'updates the cached endpoint if the user sets a new one' do
beer.send(:http_request_uri)
beer.instance_variable_get(:"@http_request_parameters")[:endpoint] = 'breweries'
expect(beer.send(:http_request_uri)).to eql('beer/123/breweries')
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/tankard/api/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@
search.send(:http_request_uri)
expect(search.instance_variable_get(:"@http_request_parameters")[:endpoint]).to eql(nil)
end

it 'caches the endpoint for future method calls' do
search.send(:http_request_uri)
expect(search.send(:http_request_uri)).to eql('search/upc')
end

it 'updates the cached endpoint if the user sets a new one' do
search.send(:http_request_uri)
search.instance_variable_get(:"@http_request_parameters")[:endpoint] = 'geo/point'
expect(search.send(:http_request_uri)).to eql('search/geo/point')
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/tankard/api/style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@
style.send(:raise_if_no_id_in_options)
expect(style.instance_variable_get(:"@http_request_parameters")[:id]).to be_nil
end

it 'can be called multiple times and not raise error' do
style.send(:raise_if_no_id_in_options)
expect { style.send(:raise_if_no_id_in_options) }.not_to raise_error
end

it 'caches the ID for future method calls' do
style.send(:raise_if_no_id_in_options)
expect(style.send(:raise_if_no_id_in_options)).to eql('test')
end

it 'updates the cache value if the user sets a new ID' do
style.send(:raise_if_no_id_in_options)
style.instance_variable_get(:"@http_request_parameters")[:id] = 'test1'
expect(style.send(:raise_if_no_id_in_options)).to eql('test1')
end
end
end

Expand Down

0 comments on commit 9909a39

Please sign in to comment.