Class: Foursquare::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/foursquare/base.rb

Constant Summary collapse

API =
"https://api.foursquare.com/v2/"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
14
# File 'lib/foursquare/base.rb', line 5

def initialize(*args)
  case args.size
  when 1
    @access_token = args.first
  when 2
    @client_id, @client_secret = args
  else
    raise ArgumentError, "You need to pass either an access_token or client_id and client_secret"
  end
end

Instance Method Details

#access_token(code, redirect_uri) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/foursquare/base.rb', line 69

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

#authorize_url(redirect_uri) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/foursquare/base.rb', line 52

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

#checkinsObject



20
21
22
# File 'lib/foursquare/base.rb', line 20

def checkins
  Foursquare::CheckinProxy.new(self)
end

#get(path, params = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/foursquare/base.rb', line 32

def get(path, params={})
  params = camelize(params)
  Foursquare.log("GET #{API + path}")
  Foursquare.log("PARAMS: #{params.inspect}")
  merge_auth_params(params)
  response = JSON.parse(Typhoeus::Request.get(API + path, :params => params).body)
  Foursquare.log(response.inspect)
  error(response) || response["response"]
end

#post(path, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/foursquare/base.rb', line 42

def post(path, params={})
  params = camelize(params)
  Foursquare.log("POST #{API + path}")
  Foursquare.log("PARAMS: #{params.inspect}")
  merge_auth_params(params)
  response = JSON.parse(Typhoeus::Request.post(API + path, :params => params).body)
  Foursquare.log(response.inspect)
  error(response) || response["response"]
end

#settingsObject



28
29
30
# File 'lib/foursquare/base.rb', line 28

def settings
  @settings ||= Foursquare::Settings.new(self)
end

#usersObject



16
17
18
# File 'lib/foursquare/base.rb', line 16

def users
  Foursquare::UserProxy.new(self)
end

#venuesObject



24
25
26
# File 'lib/foursquare/base.rb', line 24

def venues
  Foursquare::VenueProxy.new(self)
end