skittles 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,7 +17,11 @@ http://rdoc.info/github/anthonator/skittles/
17
17
  config.access_token = '...'
18
18
  end
19
19
 
20
- Skittles.venue('211152')
20
+ begin
21
+ Skittles.venue('211152')
22
+ rescue Skittles::Error => e
23
+ puts "You recieved the #{e.type} error and a status code of #{e.code} which means #{e.detail}."
24
+ end
21
25
 
22
26
  == Contributing to Skittles
23
27
 
@@ -1,3 +1,5 @@
1
+ require File.expand_path('../skittles/utils', __FILE__)
2
+ require File.expand_path('../skittles/error', __FILE__)
1
3
  require File.expand_path('../skittles/version', __FILE__)
2
4
  require File.expand_path('../skittles/configuration', __FILE__)
3
5
  require File.expand_path('../skittles/api', __FILE__)
@@ -9,7 +9,7 @@ module Skittles
9
9
  # Get details of a photo.
10
10
  #
11
11
  # @param id [String] The id of the photo to retrieve additional information for.
12
- # @return A complete photo object.
12
+ # @return [Hashie::Mash] A complete photo object.
13
13
  # @requires_acting_user Yes
14
14
  # @see http://developer.foursquare.com/docs/photos/photos.html
15
15
  def photo(id)
@@ -29,35 +29,29 @@ module Skittles
29
29
  # @option options [Decimal] llAcc Accuracy of the user's latitude and longitude, in meters.
30
30
  # @option options [Decimal] alt Altitude of the user's location, in meters.
31
31
  # @option options [Decimal] altAcc Vertical accuracy of the user's location, in meters.
32
- # @return The photo that was just created.
32
+ # @return [Hashie::Mash] The photo that was just created.
33
33
  # @requires_acting_user Yes
34
34
  # @see http://developer.foursquare.com/docs/photos/add.html
35
35
  def add_photo(file, options = {})
36
- resp = nil
37
36
  options.merge!({
38
37
  :file => UploadIO.new(file, 'image/jpeg', 'image.jpg'),
39
38
  :oauth_token => access_token
40
39
  })
41
40
  uri = URI.parse("#{endpoint}/photos/add")
42
- File.open(file) do |file|
41
+ File.open(file) do
43
42
  req = Net::HTTP::Post::Multipart.new(uri.path, options)
44
43
  http = Net::HTTP.new(uri.host, uri.port)
45
44
  http.use_ssl = true if uri.scheme == 'https'
46
- resp = http.start do |http|
47
- http.request(req)
45
+ resp = http.start do |net|
46
+ net.request(req)
47
+ end
48
+
49
+ case resp.code.to_i
50
+ when 200..299
51
+ return Skittles::Utils.parse_json(resp.body).response.photo
52
+ when 400..599
53
+ Skittles::Utils.handle_foursquare_error(resp)
48
54
  end
49
- end
50
- case resp.code.to_i
51
- when 200..299
52
- return Hashie::Mash.new(Yajl::Parser.new.parse(resp.body)).response.photo
53
- when 401
54
- e = OAuth2::AccessDenied.new("Received HTTP 401 during request.")
55
- e.response = resp
56
- raise e
57
- else
58
- e = OAuth2::HTTPError.new("Received HTTP #{resp.code} during request.")
59
- e.response = resp
60
- raise e
61
55
  end
62
56
  end
63
57
  end
@@ -17,6 +17,7 @@ module Skittles
17
17
  # @param id [String] Name of setting to change, sendToTwitter, sendToFacebook, receivePings, receiveCommentPings.
18
18
  # @param value [Integer] 1 for true, and 0 for false.
19
19
  # @requires_acting_user Yes
20
+ # @return [Hashie::Mash] A confirmation message.
20
21
  # @see http://developer.foursquare.com/docs/settings/set.html
21
22
  def set_setting(id, value)
22
23
  post("settings/#{id}/set", { :value => value }).settings
@@ -68,7 +68,7 @@ module Skittles
68
68
  # Allows you to remove a tip from your to-do list or done list.
69
69
  #
70
70
  # @param id [String] The tip you want to unmark.
71
- # @return The tip being acted on.
71
+ # @return [Hashie::Mash] The tip being acted on.
72
72
  # @requires_acting_user No
73
73
  # @see http://developer.foursquare.com/docs/tips/unmark.html
74
74
  def tip_unmark(id)
@@ -102,7 +102,6 @@ module Skittles
102
102
  # @option options [String] crossStreet The nearest intersecting street or streets.
103
103
  # @option options [String] phone The phone number of the venue.
104
104
  # @option options [String] primaryCategoryId The id of the category to which you want to assign this venue.
105
- # @return nil
106
105
  # @requires_acting_user Yes
107
106
  # @see http://developer.foursquare.com/docs/venues/proposeedit.html
108
107
  def proposeedit(id, name, address, city, state, zip, ll, options = {})
@@ -0,0 +1,28 @@
1
+ module Skittles
2
+ # Custom error class for rescuing from all known Foursquare errors.
3
+ class Error < StandardError; attr_accessor :code, :type, :detail end
4
+
5
+ # Raised when Foursquare returns HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Foursquare returns HTTP status code 401
9
+ class Unauthorized < Error; end
10
+
11
+ # Raised when Foursquare returns HTTP status code 404
12
+ class NotFound < Error; end
13
+
14
+ # Raised when Foursquare returns HTTP status code 405
15
+ class MethodNotAllowed < Error; end
16
+
17
+ # Raised when Foursquare returns HTTP status code 500
18
+ class InternalServerError < Error; end
19
+
20
+ # Raised when Foursquare returns HTTP status code 502
21
+ class BadGateway < Error; end
22
+
23
+ # Raised when Foursquare returns HTTP status code 503
24
+ class ServiceUnavailable < Error; end
25
+
26
+ # Raised when Foursquare returns HTTP status code 504
27
+ class GatewayTimeout < Error; end
28
+ end
@@ -5,43 +5,50 @@ require 'hashie'
5
5
  module Skittles
6
6
  module Request
7
7
  # Perform an HTTP GET request
8
- def get(path, options = {}, raw = false)
9
- request(:get, path, options, raw)
8
+ def get(path, options = {}, headers = {}, raw = false)
9
+ request(:get, path, options, headers, raw)
10
10
  end
11
11
 
12
12
  # Performs an HTTP POST request
13
- def post(path, options = {}, raw = false)
14
- request(:post, path, options, raw)
13
+ def post(path, options = {}, headers = {}, raw = false)
14
+ request(:post, path, options, headers, raw)
15
15
  end
16
16
 
17
17
  # Performs an HTTP PUT request
18
- def put(path, options = {}, raw = false)
19
- request(:put, path, options, raw)
18
+ def put(path, options = {}, headers = {}, raw = false)
19
+ request(:put, path, options, headers, raw)
20
20
  end
21
21
 
22
22
  # Performs an HTTP DELETE request
23
- def delete(path, options = {}, raw = false)
24
- request(:delete, path, options, raw)
23
+ def delete(path, options = {}, headers = {}, raw = false)
24
+ request(:delete, path, options, headers, raw)
25
25
  end
26
26
 
27
27
  private
28
28
  #Perform an HTTP request
29
- def request(method, path, options, raw)
30
- headers = {
29
+ def request(method, path, options, headers, raw)
30
+ headers.merge!({
31
31
  'User-Agent' => user_agent
32
- }
32
+ })
33
33
 
34
34
  options.merge!({
35
35
  :client_id => client_id,
36
36
  :client_secret => client_secret
37
37
  })
38
- response = connection.request(method, paramify(path, options), headers)
38
+
39
+ begin
40
+ response = connection.request(method, paramify(path, options), headers)
41
+ rescue OAuth2::AccessDenied || OAuth2::HTTPError => e
42
+ Skittles::Utils.handle_foursquare_error(e.response)
43
+ else
44
+ Skittles::Error
45
+ end
39
46
 
40
47
  unless raw
41
- result = Yajl::Parser.new.parse(response)
48
+ result = Skittles::Utils.parse_json(response)
42
49
  end
43
50
 
44
- raw ? response : Hashie::Mash.new(result).response
51
+ raw ? response : result.response
45
52
  end
46
53
 
47
54
  # Encode path and turn params into HTTP query.
@@ -0,0 +1,38 @@
1
+ module Skittles
2
+ # @private
3
+ module Utils
4
+ private
5
+ def self.handle_foursquare_error(response)
6
+ info = parse_json(response.body).meta
7
+ case info.code.to_i
8
+ when 400
9
+ error = Skittles::BadRequest.new
10
+ when 401
11
+ error = Skittles::Unauthorized.new
12
+ when 404
13
+ error = Skittles::NotFound.new
14
+ when 405
15
+ error = Skittles::MethodNotAllowed.new
16
+ when 500
17
+ error = Skittles::InternalServerError.new
18
+ when 502
19
+ error = Skittles::BadGateway.new
20
+ when 503
21
+ error = Skittles::ServiceUnavailable.new
22
+ when 504
23
+ error = Skittles::GatewayTimeout.new
24
+ else
25
+ error = Skittles::Error.new
26
+ end
27
+ error.code = info.code.to_i
28
+ error.type = info.errorType
29
+ error.detail = info.errorDetail
30
+ raise error
31
+ end
32
+
33
+ # Parses JSON and returns a Hashie::Mash
34
+ def self.parse_json(json)
35
+ Hashie::Mash.new(Yajl::Parser.new.parse(json))
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Skittles
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: skittles
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Anthony Smith
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-22 00:00:00 -05:00
13
+ date: 2011-03-03 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -211,7 +211,9 @@ files:
211
211
  - lib/skittles/client/venue.rb
212
212
  - lib/skittles/configuration.rb
213
213
  - lib/skittles/connection.rb
214
+ - lib/skittles/error.rb
214
215
  - lib/skittles/request.rb
216
+ - lib/skittles/utils.rb
215
217
  - lib/skittles/version.rb
216
218
  - LICENSE.txt
217
219
  - README.rdoc
@@ -231,7 +233,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
231
233
  requirements:
232
234
  - - ">="
233
235
  - !ruby/object:Gem::Version
234
- hash: 3194088974280100972
236
+ hash: -3126880121428763984
235
237
  segments:
236
238
  - 0
237
239
  version: "0"
@@ -244,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
246
  requirements: []
245
247
 
246
248
  rubyforge_project:
247
- rubygems_version: 1.5.2
249
+ rubygems_version: 1.5.3
248
250
  signing_key:
249
251
  specification_version: 3
250
252
  summary: Foursquare v2 REST API client library for Ruby