untappd-api 0.0.2 → 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.
Files changed (48) hide show
  1. data/README.rdoc +16 -6
  2. data/lib/untappd.rb +28 -0
  3. data/lib/untappd/api.rb +79 -0
  4. data/lib/untappd/client.rb +17 -0
  5. data/lib/untappd/client/beer.rb +65 -0
  6. data/lib/untappd/client/brewery.rb +48 -0
  7. data/lib/untappd/client/checkin.rb +113 -0
  8. data/lib/untappd/client/user.rb +95 -0
  9. data/lib/untappd/client/venue.rb +34 -0
  10. data/lib/untappd/configuration.rb +52 -0
  11. data/lib/untappd/error.rb +27 -0
  12. data/lib/untappd/version.rb +3 -0
  13. data/spec/fixtures/add_comment.json +1 -0
  14. data/spec/fixtures/badges.json +1 -0
  15. data/spec/fixtures/beer.json +1 -0
  16. data/spec/fixtures/beer_feed.json +1 -0
  17. data/spec/fixtures/beer_search.json +1 -0
  18. data/spec/fixtures/brewery.json +1 -0
  19. data/spec/fixtures/brewery_feed.json +1 -0
  20. data/spec/fixtures/brewery_search.json +1 -0
  21. data/spec/fixtures/checkin.json +1 -0
  22. data/spec/fixtures/checkin_details.json +1 -0
  23. data/spec/fixtures/delete_comment.json +1 -0
  24. data/spec/fixtures/distinct_beers.json +1 -0
  25. data/spec/fixtures/friend_feed.json +1 -0
  26. data/spec/fixtures/friends.json +1 -0
  27. data/spec/fixtures/public_feed.json +1 -0
  28. data/spec/fixtures/toast.json +1 -0
  29. data/spec/fixtures/trending_beers.json +1 -0
  30. data/spec/fixtures/untoast.json +1 -0
  31. data/spec/fixtures/user.json +1 -0
  32. data/spec/fixtures/user_feed.json +1 -0
  33. data/spec/fixtures/venue.json +1 -0
  34. data/spec/fixtures/venue_feed.json +1 -0
  35. data/spec/fixtures/wish_list.json +1 -0
  36. data/spec/spec_helper.rb +47 -0
  37. data/spec/untappd/api_spec.rb +68 -0
  38. data/spec/untappd/client/beer_spec.rb +102 -0
  39. data/spec/untappd/client/brewery_spec.rb +81 -0
  40. data/spec/untappd/client/checkin_spec.rb +185 -0
  41. data/spec/untappd/client/user_spec.rb +340 -0
  42. data/spec/untappd/client/venue_spec.rb +56 -0
  43. data/spec/untappd_spec.rb +80 -0
  44. data/untappd-api.gemspec +8 -2
  45. metadata +130 -66
  46. data/lib/untappd-api.rb +0 -192
  47. data/lib/untappd-api/version.rb +0 -3
  48. data/spec/untappd-api_spec.rb +0 -5
@@ -4,12 +4,22 @@ A simple Ruby wrapper for accessing the Untappd API.
4
4
  For more information, see http://untappd.com/api/dashboard.
5
5
 
6
6
  == Usage
7
- Each method returns a Hash representing the response.
7
+ Each method returns a Hashie::Mash representing the response.
8
8
 
9
9
  === Example:
10
- require 'untappd-api'
10
+ require 'untappd'
11
11
 
12
- client = Untappd::Base.new(API_KEY)
13
- beer = client.beer_info(:bid => 17904)["results"]
14
- beer["name"] # => "Third Coast Old Ale"
15
- beer["brewery"] #=> "Bell's Brewery, Inc."
12
+ Untappd.configure do |config|
13
+ config.application_key = "YOUR_API_KEY")
14
+ config.username = "your_username" # Some API calls require a signed in user
15
+ config.password = "your_password" # Most API methods do not require a user
16
+ end
17
+
18
+ Untappd.beer(1234)
19
+ Untappd.user(user: 'gambrinus') # Returns the signed in user if no :user option passed
20
+
21
+ # You can change the signed in user after initial configuration
22
+ Untappd.username = "another_user"
23
+ Untappd.password = "that_guys_password"
24
+
25
+ All methods are documented in the lib/untappd/client directory.
@@ -0,0 +1,28 @@
1
+ require 'untappd/error'
2
+ require 'untappd/configuration'
3
+ require 'untappd/api'
4
+ require 'untappd/client'
5
+ require 'untappd/version'
6
+
7
+ # Adapted from Ruby Twitter gem by John Nunemaker
8
+ # @see https://github.com/jnunemaker/twitter
9
+ module Untappd
10
+ extend Configuration
11
+
12
+ # Alias for Untappd::Client.new
13
+ #
14
+ # @return [Untappd::Client]
15
+ def self.client(options={})
16
+ Untappd::Client.new(options)
17
+ end
18
+
19
+ # Delegate to Untappd::Client
20
+ def self.method_missing(method, *args, &block)
21
+ return super unless client.respond_to?(method)
22
+ client.send(method, *args, &block)
23
+ end
24
+
25
+ def self.respond_to?(method)
26
+ client.respond_to?(method) || super
27
+ end
28
+ end
@@ -0,0 +1,79 @@
1
+ require 'httparty'
2
+ require 'hashie'
3
+
4
+ module Untappd
5
+ class API
6
+ include HTTParty
7
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
8
+
9
+ # Override these setters so that HTTParty class methods get updated as well
10
+ def endpoint=(val)
11
+ @endpoint = val
12
+ @klass.base_uri(endpoint)
13
+ end
14
+
15
+ def application_key=(val)
16
+ @application_key = val
17
+ @klass.default_params(:key => application_key)
18
+ end
19
+
20
+ # Create a new API
21
+ def initialize(options={})
22
+ @klass = self.class
23
+ options = Untappd.options.merge(options)
24
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
25
+ send("#{key}=", options[key])
26
+ end
27
+
28
+ # Set HTTParty class configurations
29
+ @klass.format(:json)
30
+ end
31
+
32
+ private
33
+
34
+ # Wrap calls to the HTTParty get and post methods and return results as Hashie::Mash
35
+ def get(path, options={})
36
+ response = @klass.get(path, :query => options,
37
+ :basic_auth => auth).parsed_response
38
+ handle_error(response)
39
+ Hashie::Mash.new(response).results
40
+ end
41
+
42
+ def post(path, options={})
43
+ response = @klass.post(path, :body => options,
44
+ :basic_auth => auth)
45
+ handle_error(response)
46
+ Hashie::Mash.new(response)
47
+ end
48
+
49
+ def auth
50
+ if username && password
51
+ { :username => username,
52
+ :password => Digest::MD5.hexdigest(password) }
53
+ end
54
+ end
55
+
56
+ def handle_error(response)
57
+ message = response['error']
58
+
59
+ case response['http_code']
60
+ when 400
61
+ raise BadRequest, message
62
+ when 401
63
+ raise Unauthorized, message
64
+ when 403
65
+ raise General, message
66
+ when 404
67
+ raise NotFound, message
68
+ when 500
69
+ raise InternalError, message
70
+ when 502
71
+ raise BadGateway, message
72
+ when 503
73
+ raise Unavailable, message
74
+ when 504
75
+ raise GatewayTimeout, message
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,17 @@
1
+ module Untappd
2
+
3
+ class Client < API
4
+ require 'untappd/client/beer'
5
+ require 'untappd/client/brewery'
6
+ require 'untappd/client/checkin'
7
+ require 'untappd/client/user'
8
+ require 'untappd/client/venue'
9
+
10
+ include Untappd::Client::Beer
11
+ include Untappd::Client::Brewery
12
+ include Untappd::Client::Checkin
13
+ include Untappd::Client::User
14
+ include Untappd::Client::Venue
15
+
16
+ end
17
+ end
@@ -0,0 +1,65 @@
1
+ module Untappd
2
+ class Client
3
+ module Beer
4
+
5
+ # Returns the feed for the specified beer.
6
+ #
7
+ # @format :json
8
+ # @authenticated false
9
+ # @param id [String, Integer] The ID of the beer.
10
+ # @option options [String, Integer] :since The ID of the last recent check-in.
11
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
12
+ # @return [Array] The requested beer feed.
13
+ # @example Retrieve the feed for beer with ID 3839.
14
+ # Untappd.beer_feed(399)
15
+ def beer_feed(id, options={})
16
+ options.merge!(:bid => id)
17
+ get('/beer_checkins', options)
18
+ end
19
+
20
+ # Returns info for the specified beer.
21
+ #
22
+ # @format :json
23
+ # @authenticated false
24
+ # @param id [String, Integer] The ID of the beer.
25
+ # @return [Hashie::Mash] The requested beer.
26
+ # @example Retrieve the beer with ID 3839.
27
+ # Untappd.beer(3839)
28
+ def beer(id)
29
+ options = { :bid => id }
30
+ get('/beer_info', options)
31
+ end
32
+
33
+ # Search beers with the specified query.
34
+ #
35
+ # @format :json
36
+ # @authenticated false
37
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
38
+ # @option options [String, Integer] :sort Alphabetical order ("name") or checkin count order ("count").
39
+ # @return [Array] The search results.
40
+ # @example Search beers for "Pale ale".
41
+ # Untappd.beer_search("Pale ale")
42
+ def beer_search(query, options={})
43
+ options.merge!(:q => query)
44
+ get('/beer_search', options)
45
+ end
46
+
47
+ # Returns the currently trending beers.
48
+ #
49
+ # @format :json
50
+ # @authenticated false
51
+ # @option options [String] :type "all", "macro", "micro", "local". "all" is set as default.
52
+ # @option options [String, Integer] :limit The number of records to return. (max 10)
53
+ # @option options [String, Integer] :age "daily", "weekly", "monthly".
54
+ # @option options [String, Integer] :geolat Latitude to filter results by. Required when :type => "local".
55
+ # @option options [String, Integer] :geolng Longitude to filter results by. Required when :type => "local".
56
+ # @return [Array] The search results.
57
+ # @example Return all trending beers.
58
+ # Untappd.trending_beers
59
+ def trending_beers(options={})
60
+ get('/trending', options)
61
+ end
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,48 @@
1
+ module Untappd
2
+ class Client
3
+ module Brewery
4
+
5
+ # Returns the feed for the specified brewery.
6
+ #
7
+ # @format :json
8
+ # @authenticated false
9
+ # @param id [String, Integer] The ID of the brewery.
10
+ # @option options [String, Integer] :since The ID of the last recent check-in.
11
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
12
+ # @return [Array] The requested brewery feed.
13
+ # @example Retrieve the feed for brewery with ID 399.
14
+ # Untappd.brewery_feed(399)
15
+ def brewery_feed(id, options={})
16
+ options.merge!(:brewery_id => id)
17
+ get('/brewery_checkins', options)
18
+ end
19
+
20
+ # Returns info for the specified brewery.
21
+ #
22
+ # @format :json
23
+ # @authenticated false
24
+ # @param id [String, Integer] The ID of the brewery.
25
+ # @return [Hashie::Mash] The requested brewery.
26
+ # @example Retrieve brewery with ID 399.
27
+ # Untappd.brewery(399)
28
+ def brewery(id)
29
+ options = { :brewery_id => id }
30
+ get('/brewery_info', options)
31
+ end
32
+
33
+ # Search breweries with the specified query.
34
+ #
35
+ # @format :json
36
+ # @authenticated false
37
+ # @param query [String] The term that you want to search.
38
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
39
+ # @return [Array] The search results.
40
+ # @example Search breweries for "Bell's".
41
+ # Untappd.brewery_search("Bell's")
42
+ def brewery_search(query, options={})
43
+ options.merge!(:q => query)
44
+ get('/brewery_search', options)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,113 @@
1
+ module Untappd
2
+ class Client
3
+ module Checkin
4
+
5
+ # Returns all public checkins, otherwise known as "The Pub".
6
+ #
7
+ # @format :json
8
+ # @authenticated false
9
+ # @option options [String, Integer] :since The ID of the last recent check-in.
10
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
11
+ # @option options [String, Integer] :geolat Latitude to filter results by.
12
+ # @option options [String, Integer] :geolng Longitude to filter results by.
13
+ # @return [Array] The requested beer feed.
14
+ # @example Retrieve the public feed.
15
+ # Untappd.public_feed
16
+ def public_feed(options={})
17
+ get('/thepub', options)
18
+ end
19
+
20
+ # Alias for public_feed
21
+ alias_method :the_pub, :public_feed
22
+
23
+ # Returns info for the specified checkin.
24
+ #
25
+ # @format :json
26
+ # @authenticated false
27
+ # @param id [String, Integer] The ID of the checkin.
28
+ # @return [Hashie::Mash] The requested checkin.
29
+ # @example Retrieve the checkin with ID 669114 .
30
+ # Untappd.beer(3839)
31
+ def checkin_details(id)
32
+ options = { :id => id }
33
+ get('/details', options)
34
+ end
35
+
36
+ # Perform a checkin with the authenticated user.
37
+ #
38
+ # @format :json
39
+ # @authenticated true
40
+ # @param gmt_offset [String, Integer] The numeric value of hours the user is from GMT (Greenwich Mean Time).
41
+ # @param beer_id [String, Integer] The ID of the beer you wish to check into.
42
+ # @option options [String] :foursquare_id The MD5 hash ID of the Venue you want to attach the beer checkin to.
43
+ # @option options [String] :shout The text you would like included as a comment. Max of 140 characters.
44
+ # @option options [String, Integer] :rating_value The score you would like to add for the beer. Must be whole numbers from 1 to 5.
45
+ # @option options [String, Integer] :user_lat Latitude of the user. Required if you want to add a location to the checkin.
46
+ # @option options [String, Integer] :user_lng Longitude of the user. Required if you want to add a location to the checkin.
47
+ # @option options [String] :facebook Set to "on" to post checkin to Facebook. Defaults to "off".
48
+ # @option options [String] :twitter Set to "on" to post checkin to Twitter. Defaults to "off".
49
+ # @option options [String] :foursquare Set to "on" to pass checkin on to Foursquare. Defaults to "off".
50
+ # @option options [String] :gowalla Set to "on" to pass checkin on to Gowalla. Defaults to "off".
51
+ # @return [Hashie::Mash] The results of the checkin. Includes info for beer, venue, badges earned, and recommendations.
52
+ # @example Checkin to beer with ID 6284 from Eastern time zone (GMT - 5 hours).
53
+ # Untappd.checkin(-5, 6284)
54
+ def checkin(beer_id, gmt_offset, options={})
55
+ options.merge!(:gmt_offset => gmt_offset, :bid => beer_id)
56
+ post('/checkin', options)
57
+ end
58
+
59
+ # Add a comment to a checkin.
60
+ #
61
+ # @format :json
62
+ # @authenticated true
63
+ # @param checkin_id [String, Integer] The ID of the checkin you wish to comment on.
64
+ # @param comment [String] The comment text you would like to add. Max 140 characters.
65
+ # @return [Hashie::Mash] The results of adding the comment. Includes user and comment details.
66
+ # @example Comment on checkin with ID 669114 and comment text of "Nice."
67
+ # Untappd.add_comment(669114, "Nice.")
68
+ def add_comment(checkin_id, comment)
69
+ options = { :checkin_id => checkin_id, :comment => comment }
70
+ post('/add_comment', options)
71
+ end
72
+
73
+ # Delete a comment made by the authenticated user.
74
+ #
75
+ # @format :json
76
+ # @authenticated true
77
+ # @param comment_id [String, Integer] The ID of the comment you wish to delete.
78
+ # @return [Hashie::Mash] The results of deleting the comment.
79
+ # @example Delete comment with ID 669114.
80
+ # Untappd.delete_comment(669114)
81
+ def delete_comment(comment_id)
82
+ options = { :comment_id => comment_id }
83
+ post('/delete_comment', options)
84
+ end
85
+
86
+ # Toast a checkin.
87
+ #
88
+ # @format :json
89
+ # @authenticated true
90
+ # @param checkin_id [String, Integer] The ID of the checkin you wish to toast.
91
+ # @return [Hashie::Mash] The results of the toast.
92
+ # @example Toast checkin with ID 669114.
93
+ # Untappd.toast(669114)
94
+ def toast(checkin_id)
95
+ options = { :checkin_id => checkin_id }
96
+ post('/toast', options)
97
+ end
98
+
99
+ # Remove a toast made by the authenticated user from a checkin.
100
+ #
101
+ # @format :json
102
+ # @authenticated true
103
+ # @param checkin_id [String, Integer] The ID of the checkin you wish to remove your toast from.
104
+ # @return [Hashie::Mash] The results of the toast removal.
105
+ # @example Remove toast from checkin with ID 669114.
106
+ # Untappd.remove_toast(669114)
107
+ def untoast(checkin_id)
108
+ options = { :checkin_id => checkin_id }
109
+ post('/delete_toast', options)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,95 @@
1
+ module Untappd
2
+ class Client
3
+ module User
4
+
5
+ # Returns the feed for the specified user.
6
+ #
7
+ # @format :json
8
+ # @authenticated false unless :name option is omitted.
9
+ # @option options [String] :user The user whose feed you wish to request. If not provided, the feed of the authenticated user will be returned.
10
+ # @option options [String, Integer] :since The ID of the last recent check-in.
11
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
12
+ # @return [Hashie::Mash] The requested user feed.
13
+ # @example Retrieve the feed for user with username "gambrinus".
14
+ # Untappd.user_feed(:user => "gambrinus")
15
+ def user_feed(options={})
16
+ get('/user_feed', options)
17
+ end
18
+
19
+ # Returns the friend feed for the authenticated user.
20
+ #
21
+ # @format :json
22
+ # @authenticated true
23
+ # @option options [String, Integer] :since The ID of the last recent check-in.
24
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
25
+ # @return [Hashie::Mash] The requested friend feed of the authenticated user.
26
+ # @example Retrieve the friend feed for the authenticated user.
27
+ # Untappd.friend_feed
28
+ def friend_feed(options={})
29
+ get('/feed', options)
30
+ end
31
+
32
+ # Returns the info for the requested user.
33
+ #
34
+ # @format :json
35
+ # @authenticated false unless :user option is omitted.
36
+ # @option options [String] :user The username of the user you wish to request info for. Returns info for authenticated user if omitted.
37
+ # @return [Hashie::Mash] The requested user info.
38
+ # @example Retrieve the info for the user with username "gambrinus".
39
+ # Untappd.user(:user => "gambrinus')
40
+ def user(options={})
41
+ get('/user', options)
42
+ end
43
+
44
+ # Returns the badge info for the requested user.
45
+ #
46
+ # @format :json
47
+ # @authenticated false unless :user option is omitted.
48
+ # @option options [String] :user The username of the user you wish to request info for. Returns info for authenticated user if omitted.
49
+ # @return [Hashie::Mash] The badge info for the requested user.
50
+ # @example Retrieve the badge info for the user with username "gambrinus".
51
+ # Untappd.badges(:user => "gambrinus')
52
+ def badges(options={})
53
+ get('/user_badge', options)
54
+ end
55
+
56
+ # Returns the friends for the requested user.
57
+ #
58
+ # @format :json
59
+ # @authenticated false unless :user option is omitted.
60
+ # @option options [String] :user The username of the user you wish to request info for. Returns info for authenticated user if omitted.
61
+ # @return [Hashie::Mash] The friends of the requested user.
62
+ # @example Retrieve the friends of the user with username "gambrinus".
63
+ # Untappd.friends(:user => "gambrinus')
64
+ def friends(options={})
65
+ get('/friends', options)
66
+ end
67
+
68
+ # Returns the wish list of the requested user.
69
+ #
70
+ # @format :json
71
+ # @authenticated false unless :user option is omitted.
72
+ # @option options [String] :user The username of the user you wish to request info for. Returns info for authenticated user if omitted.
73
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
74
+ # @return [Hashie::Mash] The wish list of the requested user.
75
+ # @example Retrieve the wish list of the user with username "gambrinus".
76
+ # Untappd.wish_list(:user => "gambrinus')
77
+ def wish_list(options={})
78
+ get('/wish_list', options)
79
+ end
80
+
81
+ # Returns the distinct beers of the requested user.
82
+ #
83
+ # @format :json
84
+ # @authenticated false unless :user option is omitted.
85
+ # @option options [String] :user The username of the user you wish to request info for. Returns info for authenticated user if omitted.
86
+ # @option options [String, Integer] :offset The offset you would like the dataset to begin with.
87
+ # @return [Hashie::Mash] The distinct beers of the requested user.
88
+ # @example Retrieve the distinct beers of the user with username "gambrinus".
89
+ # Untappd.distinct_beers(:user => "gambrinus')
90
+ def distinct_beers(options={})
91
+ get('/user_distinct', options)
92
+ end
93
+ end
94
+ end
95
+ end