tankard 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15d6c67b39b0fbce1ddce69adcdbca40366f1281
4
- data.tar.gz: e7db9eff7e9a74a9e4794bb509f1a7d73d4980a7
3
+ metadata.gz: caf2e4adbd1c27dad9227737cec3b9ccb7947d4d
4
+ data.tar.gz: ce3dbc3b145c38afdcb37c80472ac042d121269d
5
5
  SHA512:
6
- metadata.gz: 48d66cb484f2726da1caa59f15905f46a1494742793d16a00083c1fb5ae21472f776d0997029f7c77362183eca5e4db79b32463466c9df456fdbd65387efdd2b
7
- data.tar.gz: f06bb814da4bb553a938dd77584cdbd0a41fa8bce2ef4aca962bf5e5b615a280be51ad7d78175ba78c1db074511fc0a30cea8b3d13ffc581d531986cfe183c47
6
+ metadata.gz: 4734b1c05ec5529b9af9105c70308aacf74a5cdd04b76e570c461beb2775ff2a430ec1a3947f64b37d7a065bae75500e91b13e17d42aa233276080d4ff597b72
7
+ data.tar.gz: 1b27b4d244bb37e09453d9908d8fd91667244f47a5b22de60fe9f36ca89d68aa4128f127795574596811f960aa30740b23dae2b99aea8e7d40f4eb9c360a1575
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## v0.1.0 (05/15/2013)
2
+ * Add params method to beers. Allows passing additional parameters in an additional way.
3
+ * Add params method to beer. Allows passing additional parameters in an additional way.
4
+ * Add styles method & class. Allows querying of the /styles route
5
+ * Add style method & class. Allows querying of the /style/:id route
6
+ * Add search method & class. Allows querying of the /search route
7
+ * Add documentation for beer
8
+ * Add documentation for beers
9
+ * Add documentation for search
10
+ * Add documentation for style
11
+ * Add documentation for styles
12
+
1
13
  ## v0.0.1
2
14
  * Support for querying a beer
3
15
  * Support for querying a list of beers
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  [![Dependency Status](https://gemnasium.com/matthewshafer/tankard.png)](https://gemnasium.com/matthewshafer/tankard)
4
4
  [![Code Climate](https://codeclimate.com/github/matthewshafer/tankard.png)](https://codeclimate.com/github/matthewshafer/tankard)
5
5
  [![Coverage Status](https://coveralls.io/repos/matthewshafer/tankard/badge.png?branch=master)](https://coveralls.io/r/matthewshafer/tankard)
6
+ [![Gem Version](https://badge.fury.io/rb/tankard.png)](http://rubygems.org/gems/tankard)
6
7
 
7
8
  Allows easy quering of the BreweryDB Api
8
9
 
@@ -14,11 +15,18 @@ I've also been pretty interested in Jruby as of late and wanted to make Tankard
14
15
 
15
16
  ## Installation
16
17
 
17
- Soon...
18
+ ```ruby
19
+ gem install "tankard"
20
+ ```
21
+
22
+ or add it to your gemfile
18
23
 
19
24
  ## Documentation
20
25
 
21
- Soon...
26
+ Everytime a commit is pushed to github the documentation regenerates.
27
+ You can find this at http://rubydoc.info/github/matthewshafer/tankard/frames
28
+
29
+ If you visit rubygems you can find the documentation for a specific gem release.
22
30
 
23
31
  ## Usage
24
32
 
@@ -42,6 +50,45 @@ Tankard.beer.id("some_id").breweries.each { |x| p x}
42
50
  beer = Tankard.beer.find(["first_id", "second_id"])
43
51
  ```
44
52
 
53
+ Alternatively you can send parameters to the request in two ways. Here are examples:
54
+
55
+ ```ruby
56
+ Tankard.beer(id: "some_id", endpoint: "breweries", anotherParam: "something").each { |x| p x }
57
+
58
+ Tankard.beer.id("some_id").breweries.params(anotherParam: "something").each { |x| p x }
59
+ ```
60
+
61
+ ### Beers
62
+
63
+ This would return an array with all beers greater than 10% (to_a comes from enumerable in this case)
64
+
65
+ ```ruby
66
+ Tankard.beers.params(abv: "+10").to_a
67
+ ```
68
+ ### Search
69
+
70
+ Here is how we could search for everything that matches "stone".
71
+ We would get results that contain more than just beer (EX I would probably get a result for Stone Brewing)
72
+
73
+ ```ruby
74
+ Tankard.search.query("stone").each { |x| p x }
75
+ ```
76
+ ### Style
77
+
78
+ This works similiar to Beer, it just has less options
79
+
80
+ ```ruby
81
+ Tankard.style.id("some_id").each { |x| p x }
82
+ ```
83
+
84
+ ### Styles
85
+
86
+ Styles don't have any options so if I would like an array with all styles I can do something like
87
+
88
+ ```ruby
89
+ Tankard.styles.to_a
90
+ ```
91
+
45
92
  ## Contributing
46
93
 
47
94
  ### Issues
@@ -1,87 +1,136 @@
1
1
  require 'hashie'
2
2
  require 'tankard/api/request/get'
3
+ require 'tankard/api/utils/page_finders'
4
+ require 'tankard/api/utils/find'
3
5
 
4
6
  module Tankard
5
7
  module Api
8
+ # Access for the /beer route on brewerydb
9
+ #
10
+ # @see http://www.brewerydb.com/developers/docs-endpoint/beer_index
11
+ # @author Matthew Shafer
6
12
  class Beer
7
- include ::Enumerable
8
13
  include Tankard::Api::Request::Get
9
-
14
+ include Tankard::Api::Utils::PageFinders
15
+ include Tankard::Api::Utils::Find
16
+ # @!parse include ::Enumerable
17
+
18
+ # Initialize a new object
19
+ #
20
+ # @param request [Tankard::Request]
21
+ # @param options [Hash]
22
+ # @return [Tankard::Api::Beer]
10
23
  def initialize(request, options={})
11
24
  @request = request
12
25
  @options = Hashie::Mash.new(options)
13
26
  end
14
27
 
15
- def each(&block)
16
- find_on_single_page(block)
17
- end
18
-
19
- def find(beer_id, options={})
20
- @options.merge!(options)
21
-
22
- if beer_id.is_a?(Array)
23
- beer_id.map { |beer| request_data_with_nil_on_http_error(@request, "beer/#{beer}", @options) }.compact
24
- else
25
- request_data_with_nil_on_http_error(@request, "beer/#{beer_id}", @options)
26
- end
27
- end
28
-
28
+ # @!method find(id_or_array, options={})
29
+ # Find a single or multiple beers by their id
30
+ #
31
+ # @param id_or_array [String, Array]
32
+ # @param options [Hash]
33
+ # @return [Hash, Array] if a string with a beer id is passed to find then the hash of the beer is returned.
34
+ # if an array is passed to find an array containing hashes with each beer is returned.
35
+ # if a beer is not found nothing for that beer is returned.
36
+
37
+ # @!method each(&block)
38
+ # Calls the given block once for each beer
39
+ #
40
+ # @yieldparam [Hash] hash containing individual beer information
41
+ # @raise [Tankard::Error::MissingParameter] when the id is not set
42
+ # @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid
43
+ # @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api
44
+ # @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned
45
+ # @raise [Tankard::Error::LoadError] when multi json is unable to decode json
46
+
47
+ # BeerID to query
48
+ #
49
+ # @param beer_id [String]
50
+ # @return [self] returns itself
29
51
  def id(beer_id)
30
52
  @options.id = beer_id
31
53
  self
32
54
  end
33
55
 
56
+ # Sets the request to beer/:id/breweries
57
+ #
58
+ # @return [self] returns itself
34
59
  def breweries
35
60
  @options.endpoint = "breweries"
36
61
  self
37
62
  end
38
63
 
64
+ # Sets the request to beer/:id/events
65
+ #
66
+ # @return [self] returns itself
39
67
  def events
40
68
  @options.endpoint = "events"
41
69
  self
42
70
  end
43
71
 
72
+ # Sets the request to beer/:id/ingredients
73
+ #
74
+ # @return [self] returns itself
44
75
  def ingredients
45
76
  @options.endpoint = "ingredients"
46
77
  self
47
78
  end
48
79
 
80
+ # Sets the request to beer/:id/socialaccounts
81
+ #
82
+ # @return [self] returns itself
49
83
  def social_accounts
50
84
  @options.endpoint = "socialaccounts"
51
85
  self
52
86
  end
53
87
 
88
+ # Sets the request to beer/:id/variations
89
+ #
90
+ # @return [self] returns itself
54
91
  def variations
55
92
  @options.endpoint = "variations"
56
93
  self
57
94
  end
58
95
 
96
+ # Additional parameters to send with the request
97
+ #
98
+ # @param options [Hash]
99
+ # @return [self] returns itself
100
+ def params(options={})
101
+ options.each_pair do |key,value|
102
+ @options[key] = value
103
+ end
104
+ self
105
+ end
106
+
59
107
  private
60
108
 
61
- def request_data_from_options
62
- endpoint = "beer/#{raise_if_no_id_in_options}"
109
+ def http_request_uri
110
+ endpoint = "#{route}/#{raise_if_no_id_in_options}"
63
111
 
64
112
  if @options.endpoint?
65
113
  endpoint += "/#{@options.delete(:endpoint)}"
66
114
  end
67
115
 
68
- request_data(@request, endpoint, @options)
116
+ endpoint
69
117
  end
70
118
 
71
119
  def raise_if_no_id_in_options
72
- raise Tankard::Error::NoBeerId unless @options.id?
120
+ raise Tankard::Error::MissingParameter, "No Beer ID is set" unless @options.id?
73
121
  @options.delete(:id)
74
122
  end
75
123
 
76
- def find_on_single_page(block)
77
- data = request_data_from_options
78
- raise Tankard::Error::InvalidResponse unless data
124
+ def route
125
+ "beer"
126
+ end
79
127
 
80
- if data.is_a?(Hash)
81
- block.call(data)
82
- else
83
- data.each { |beer| block.call(beer) }
84
- end
128
+ def http_client
129
+ @request
130
+ end
131
+
132
+ def http_request_parameters
133
+ @options
85
134
  end
86
135
  end
87
136
  end
@@ -1,64 +1,78 @@
1
1
  require 'hashie'
2
2
  require 'tankard/api/request/get'
3
+ require 'tankard/api/utils/page_finders'
3
4
 
4
5
  module Tankard
5
6
  module Api
7
+ # Access for the /beers route on brewerydb
8
+ #
9
+ # @see http://www.brewerydb.com/developers/docs-endpoint/beer_index
10
+ # @author Matthew Shafer
6
11
  class Beers
7
- include ::Enumerable
8
12
  include Tankard::Api::Request::Get
13
+ include Tankard::Api::Utils::PageFinders
14
+ # @!parse include ::Enumerable
9
15
 
16
+ # Initializes a new object
17
+ #
18
+ # @param request [Tankard::Request]
19
+ # @param options [Hash]
20
+ # @return [Tankard::Api::Beers]
10
21
  def initialize(request, options={})
11
22
  @request = request
12
23
  @options = Hashie::Mash.new(options)
13
24
  end
14
25
 
15
- def each(&block)
16
- if options_have_page_set
17
- find_on_single_page(block)
18
- else
19
- find_on_all_pages(block)
20
- end
21
- end
26
+ # @!method each(&block)
27
+ # Calls the given block once for each beer
28
+ #
29
+ # @yieldparam [Hash] hash containing individual beer information
30
+ # @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid
31
+ # @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api
32
+ # @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned
33
+ # @raise [Tankard::Error::LoadError] when multi json is unable to decode json
22
34
 
35
+ # Beer name to query with
36
+ #
37
+ # @param beer_name [String]
38
+ # @return [self] returns itself
23
39
  def name(beer_name)
24
40
  @options[:name] = beer_name
25
41
  self
26
42
  end
27
43
 
44
+ # Page number to request
45
+ #
46
+ # @param number [Integer]
47
+ # @return [self] returns itself
28
48
  def page(number)
29
49
  @options[:p] = number
30
50
  self
31
51
  end
32
52
 
53
+ # Additional parameters to send with the request
54
+ #
55
+ # @param options [Hash]
56
+ # @return [self] returns itself
57
+ def params(options={})
58
+ options.each_pair do |key,value|
59
+ @options[key] = value
60
+ end
61
+ self
62
+ end
63
+
33
64
  private
34
65
 
35
- def options_have_page_set
36
- @options.has_key?(:p)
66
+ def http_request_uri
67
+ "beers"
37
68
  end
38
69
 
39
- def find_on_all_pages(block)
40
- page = 0
41
- options = @options.clone
42
- begin
43
- page += 1
44
- options[:p] = page
45
- response = get_request(@request, "beers", options)
46
- total_pages = response["numberOfPages"].to_i
47
- data = response["data"]
48
- raise Tankard::Error::InvalidResponse unless data
49
- data.each { |beer| block.call(beer) }
50
- end while page < total_pages
70
+ def http_client
71
+ @request
51
72
  end
52
73
 
53
- def find_on_single_page(block)
54
- data = request_data(@request, "beers", @options)
55
- raise Tankard::Error::InvalidResponse unless data
56
-
57
- if data.is_a?(Hash)
58
- block.call(data)
59
- else
60
- data.each { |beer| block.call(beer) }
61
- end
74
+ def http_request_parameters
75
+ @options
62
76
  end
63
77
  end
64
78
  end
@@ -0,0 +1,132 @@
1
+ require 'hashie'
2
+ require 'tankard/api/utils/page_finders'
3
+
4
+ module Tankard
5
+ module Api
6
+ # Access for the /search route on brewerydb
7
+ #
8
+ # @see http://www.brewerydb.com/developers/docs-endpoint/search_index
9
+ # @author Matthew Shafer
10
+ class Search
11
+ include ::Enumerable
12
+ include Tankard::Api::Utils::PageFinders
13
+ # @!parse include ::Enumerable
14
+
15
+ # Initializes a new object
16
+ #
17
+ # @param request [Tankard::Request]
18
+ # @param options [Hash]
19
+ # @return [Tankard::Api::Search]
20
+ def initialize(request, options={})
21
+ @request = request
22
+ @options = Hashie::Mash.new(options)
23
+ end
24
+
25
+ # Calls the given block once for each result
26
+ #
27
+ # @yieldparam [Hash] hash containing individual beer information
28
+ # @raise [Tankard::Error::MissingParameter] when the id is not set
29
+ # @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid
30
+ # @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api
31
+ # @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned
32
+ # @raise [Tankard::Error::LoadError] when multi json is unable to decode json
33
+ def each(&block)
34
+ raise_if_required_options_not_set
35
+ super(&block)
36
+ end
37
+
38
+ # Query to search for
39
+ #
40
+ # @param search_query [String]
41
+ # @return [self] returns itself
42
+ def query(search_query)
43
+ @options.q = search_query
44
+ self
45
+ end
46
+
47
+ # Page number to request
48
+ #
49
+ # @param number [Integer]
50
+ # @return [self] returns itself
51
+ def page(number)
52
+ @options.p = number
53
+ self
54
+ end
55
+
56
+ # Additional parameters to send with the request
57
+ #
58
+ # @param options [Hash]
59
+ # @return [self] returns itself
60
+ def params(options={})
61
+ options.each_pair do |key,value|
62
+ @options[key] = value
63
+ end
64
+ self
65
+ end
66
+
67
+ # Type of search to perform
68
+ #
69
+ # @param search_type [String]
70
+ # @return [self] returns itself
71
+ def type(search_type)
72
+ @options.type = search_type
73
+ self
74
+ end
75
+
76
+ # search for a specific upc.
77
+ # sets the endpoint to upc and passes upc_code as a parameter
78
+ #
79
+ # @param upc_code [Integer]
80
+ # @return [self] returns itself
81
+ def upc(upc_code)
82
+ @options.code = upc_code
83
+ @options.endpoint = "upc"
84
+ self
85
+ end
86
+
87
+ # search for berweries around a specific point.
88
+ # sets the endpoint to geo/point and passes lat/lng as parameters
89
+ #
90
+ # @param latitude [Float]
91
+ # @param longitude [Float]
92
+ # @return [self] returns itself
93
+ def geo_point(latitude, longitude)
94
+ @options.lat = latitude
95
+ @options.lng = longitude
96
+ @options.endpoint = "geo/point"
97
+ self
98
+ end
99
+
100
+ private
101
+
102
+ def http_request_uri
103
+ endpoint = "search"
104
+
105
+ if @options.endpoint?
106
+ endpoint += "/#{@options.delete(:endpoint)}"
107
+ end
108
+
109
+ endpoint
110
+ end
111
+
112
+ def http_client
113
+ @request
114
+ end
115
+
116
+ def http_request_parameters
117
+ @options
118
+ end
119
+
120
+ def raise_if_required_options_not_set
121
+ case @options.endpoint
122
+ when nil
123
+ raise Tankard::Error::MissingParameter, "No search query set" unless @options.q?
124
+ when "upc"
125
+ raise Tankard::Error::MissingParameter, "missing parameter: code" unless @options.code?
126
+ when "geo/point"
127
+ raise Tankard::Error::MissingParameter, "missing Parameters: lat, lng" unless @options.lat? && @options.lng?
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,80 @@
1
+ require 'hashie'
2
+ require 'tankard/api/request/get'
3
+ require 'tankard/api/utils/page_finders'
4
+ require 'tankard/api/utils/find'
5
+
6
+ module Tankard
7
+ module Api
8
+ # Access for the /style route on brewerydb
9
+ #
10
+ # @see http://www.brewerydb.com/developers/docs-endpoint/style_index
11
+ # @author Matthew Shafer
12
+ class Style
13
+ include Tankard::Api::Request::Get
14
+ include Tankard::Api::Utils::PageFinders
15
+ include Tankard::Api::Utils::Find
16
+ # @!parse include ::Enumerable
17
+
18
+ # Initializes a new object
19
+ #
20
+ # @param request [Tankard::Request]
21
+ # @param options [Hash]
22
+ # @return [Tankard::Api::Style]
23
+ def initialize(request, options={})
24
+ @request = request
25
+ @options = Hashie::Mash.new(options)
26
+ end
27
+
28
+ # @!method find(id_or_array, options={})
29
+ # Find a single or multiple styles by their id
30
+ #
31
+ # @param id_or_array [String, Array]
32
+ # @param options [Hash]
33
+ # @return [Hash, Array] if a string with a style id is passed to find then the hash of the style is returned.
34
+ # if an array is passed to find an array containing hashes with each style is returned.
35
+ # if a style is not found nothing for that style is returned.
36
+
37
+ # @!method each(&block)
38
+ # Calls the given block once for each style
39
+ #
40
+ # @yieldparam [Hash] hash containing individual style information
41
+ # @raise [Tankard::Error::MissingParameter] when the id is not set
42
+ # @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid
43
+ # @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api
44
+ # @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned
45
+ # @raise [Tankard::Error::LoadError] when multi json is unable to decode json
46
+
47
+ # Style id to query
48
+ #
49
+ # @param style_id [String]
50
+ # @return [self] returns itself
51
+ def id(style_id)
52
+ @options.id = style_id
53
+ self
54
+ end
55
+
56
+ private
57
+
58
+ def raise_if_no_id_in_options
59
+ raise Tankard::Error::MissingParameter, "No style id set" unless @options.id?
60
+ @options.delete(:id)
61
+ end
62
+
63
+ def route
64
+ "style"
65
+ end
66
+
67
+ def http_request_uri
68
+ "#{route}/#{raise_if_no_id_in_options}"
69
+ end
70
+
71
+ def http_client
72
+ @request
73
+ end
74
+
75
+ def http_request_parameters
76
+ @options
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,48 @@
1
+ require 'hashie'
2
+ require 'tankard/api/request/get'
3
+ require 'tankard/api/utils/page_finders'
4
+
5
+ module Tankard
6
+ module Api
7
+ # Access for the /styles route on brewerydb
8
+ #
9
+ # @see http://www.brewerydb.com/developers/docs-endpoint/style_index
10
+ # @author Matthew Shafer
11
+ class Styles
12
+ include Tankard::Api::Request::Get
13
+ include Tankard::Api::Utils::PageFinders
14
+ # @!parse include ::Enumerable
15
+
16
+ # @!method each(&block)
17
+ # Calls the given block once for each style
18
+ #
19
+ # @yieldparam [Hash] hash containing individual style information
20
+ # @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid
21
+ # @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api
22
+ # @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned
23
+ # @raise [Tankard::Error::LoadError] when multi json is unable to decode json
24
+
25
+ # Initializes a new object
26
+ #
27
+ # @param request [Tankard::Request]
28
+ # @return [Tankard::Api::Styles]
29
+ def initialize(request)
30
+ @request = request
31
+ end
32
+
33
+ private
34
+
35
+ def http_request_uri
36
+ "styles"
37
+ end
38
+
39
+ def http_client
40
+ @request
41
+ end
42
+
43
+ def http_request_parameters
44
+ {}
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,35 @@
1
+ require 'tankard/api/request/get'
2
+
3
+ module Tankard
4
+ module Api
5
+ module Utils
6
+ module Find
7
+ include Tankard::Api::Request::Get
8
+
9
+ def find(id_or_array, options={})
10
+ options = http_request_parameters.merge!(options)
11
+
12
+ if id_or_array.is_a?(Array)
13
+ id_or_array.map { |id| request_data_with_nil_on_http_error(http_client, "#{route}/#{id}", options) }.compact
14
+ else
15
+ request_data_with_nil_on_http_error(http_client, "#{route}/#{id_or_array}", options)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def route
22
+ raise NoMethodError.new("Must implement and return the base route")
23
+ end
24
+
25
+ def http_client
26
+ raise NoMethodError.new("Must return the http object to make requests with")
27
+ end
28
+
29
+ def http_request_parameters
30
+ raise NoMethodError.new("Must return a hash like structure with request parameters")
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end