Permalink
Browse files

Add API version param to all API requests

  • Loading branch information...
1 parent 7b50c4b commit 633ca842e7cbedb053cedca3b0c3135a2e2f1660 Lawson Kurtz committed Jan 29, 2014
Showing with 23 additions and 17 deletions.
  1. +23 −17 lib/foursquare/base.rb
View
@@ -2,13 +2,13 @@ module Foursquare
class Base
API = "https://api.foursquare.com/v2/"
- def initialize(*args)
- case args.size
- when 1
- @access_token = args.first
- when 2
- @client_id, @client_secret = args
- else
+ def initialize(args = {})
+ @access_token = args.fetch(:access_token, nil)
+ @client_id = args.fetch(:client_id, nil)
+ @client_secret = args.fetch(:client_secret, nil)
+ @api_version = args.fetch(:api_version, Date.new.strftime('%Y%m%d'))
+
+ unless @access_token || (@client_id && @client_secret)
raise ArgumentError, "You need to pass either an access_token or client_id and client_secret"
end
end
@@ -34,6 +34,7 @@ def get(path, params={})
Foursquare.log("GET #{API + path}")
Foursquare.log("PARAMS: #{params.inspect}")
merge_auth_params(params)
+ merge_version_params(params)
response = JSON.parse(Typhoeus::Request.get(API + path, :params => params).body)
Foursquare.log(response.inspect)
error(response) || response["response"]
@@ -44,56 +45,57 @@ def post(path, params={})
Foursquare.log("POST #{API + path}")
Foursquare.log("PARAMS: #{params.inspect}")
merge_auth_params(params)
+ merge_version_params(params)
response = JSON.parse(Typhoeus::Request.post(API + path, :params => params).body)
Foursquare.log(response.inspect)
error(response) || response["response"]
end
-
+
def authorize_url(redirect_uri)
# http://developer.foursquare.com/docs/oauth.html
-
+
# check params
raise "you need to define a client id before" if @client_id.blank?
raise "no callback url provided" if redirect_uri.blank?
-
+
# params
params = {}
params["client_id"] = @client_id
params["response_type"] = "code"
params["redirect_uri"] = redirect_uri
-
+
# url
oauth2_url('authenticate', params)
end
-
+
def access_token(code, redirect_uri)
# http://developer.foursquare.com/docs/oauth.html
-
+
# check params
raise "you need to define a client id before" if @client_id.blank?
raise "you need to define a client secret before" if @client_secret.blank?
raise "no code provided" if code.blank?
raise "no redirect_uri provided" if redirect_uri.blank?
-
+
# params
params = {}
params["client_id"] = @client_id
params["client_secret"] = @client_secret
params["grant_type"] = "authorization_code"
params["redirect_uri"] = redirect_uri
params["code"] = code
-
+
# url
url = oauth2_url('access_token', params)
-
+
# response
# http://developer.foursquare.com/docs/oauth.html
response = JSON.parse(Typhoeus::Request.get(url).body)
response["access_token"]
end
private
-
+
def oauth2_url(method_name, params)
"https://foursquare.com/oauth2/#{method_name}?#{params.to_query}"
end
@@ -132,5 +134,9 @@ def merge_auth_params(params)
params.merge!(:client_id => @client_id, :client_secret => @client_secret)
end
end
+
+ def merge_version_params(params)
+ params.merge!(:v => @api_version)
+ end
end
end

0 comments on commit 633ca84

Please sign in to comment.