Class: Foursquare::Base

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

Constant Summary collapse

BASE_URL =
'http://api.foursquare.com/v1'
FORMAT =
'json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth) ⇒ Base

Returns a new instance of Base.



65
66
67
# File 'lib/foursquare.rb', line 65

def initialize(oauth)
  @oauth = oauth
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, params = {}) ⇒ Object

Foursquare API: groups.google.com/group/foursquare-api/web/api-documentation

.test # api test method

=> {'response': 'ok'}

.checkin = => ‘At home. Writing code’ # post new check in

=> {...checkin hash...}

.history # authenticated user’s checkin history

> [hashie…, checkin hashie…]

.send(‘venue.flagclosed=’, => 12345) # flag venue 12345 as closed

> ‘ok’

.venue_flagclosed = => 12345

> ‘ok’

Assignment methods(POSTs) always return a hash. Annoyingly Ruby always returns what’s on the right side of the assignment operator. So there are some wrapper methods below for POSTs that make sure it gets turned into a hashie



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/foursquare.rb', line 87

def method_missing(method_symbol, params = {})
  method_name = method_symbol.to_s.split(/\.|_/).join('/')
  
  if (method_name[-1,1]) == '='
    method = method_name[0..-2]
    result = post(api_url(method), params)
    params.replace(result[method] || result)
  else
    result = get(api_url(method_name, params))
    result[method_name] || result
  end
end

Instance Attribute Details

#oauthObject

Returns the value of attribute oauth.



63
64
65
# File 'lib/foursquare.rb', line 63

def oauth
  @oauth
end

Instance Method Details

#addtip(params = {}) ⇒ Object



152
153
154
# File 'lib/foursquare.rb', line 152

def addtip(params = {})
  api(:addtip=, params).tip
end

#addvenue(params = {}) ⇒ Object



140
141
142
# File 'lib/foursquare.rb', line 140

def addvenue(params = {})
  api(:addvenue=, params).venue
end

#api(method_symbol, params = {}) ⇒ Object



100
101
102
# File 'lib/foursquare.rb', line 100

def api(method_symbol, params = {})
  Hashie::Mash.new(method_missing(method_symbol, params))
end

#api_url(method_name, options = nil) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/foursquare.rb', line 104

def api_url(method_name, options = nil)
  params = options.is_a?(Hash) ? to_query_params(options) : options
  params = nil if params and params.blank?
  url = BASE_URL + '/' + method_name.split('.').join('/')
  url += ".#{FORMAT}"
  url += "?#{params}" if params
  url
end

#checkin(params = {}) ⇒ Object

API method wrappers



132
133
134
# File 'lib/foursquare.rb', line 132

def checkin(params = {})
  api(:checkin=, params).checkin
end

#findfriends_byname(params = {}) ⇒ Object



180
181
182
# File 'lib/foursquare.rb', line 180

def findfriends_byname(params = {})
  api(:findfriends_byname, params).users
end

#findfriends_byphone(params = {}) ⇒ Object



184
185
186
# File 'lib/foursquare.rb', line 184

def findfriends_byphone(params = {})
  api(:findfriends_byphone, params).users
end

#findfriends_bytwitter(params = {}) ⇒ Object



188
189
190
# File 'lib/foursquare.rb', line 188

def findfriends_bytwitter(params = {})
  api(:findfriends_bytwitter, params).users
end

#friend_approve(params = {}) ⇒ Object



168
169
170
# File 'lib/foursquare.rb', line 168

def friend_approve(params = {})
  api(:friend_approve=, params).user
end

#friend_deny(params = {}) ⇒ Object



172
173
174
# File 'lib/foursquare.rb', line 172

def friend_deny(params = {})
  api(:friend_deny=, params).user
end

#friend_requestsObject



164
165
166
# File 'lib/foursquare.rb', line 164

def friend_requests
  api(:friend_requests).requests
end

#friend_sendrequest(params = {}) ⇒ Object



176
177
178
# File 'lib/foursquare.rb', line 176

def friend_sendrequest(params = {})
  api(:friend_sendrequest=, params).user
end

#get(url) ⇒ Object



122
123
124
# File 'lib/foursquare.rb', line 122

def get(url)
  parse_response(@oauth.access_token.get(url))
end

#history(params = {}) ⇒ Object



136
137
138
# File 'lib/foursquare.rb', line 136

def history(params = {})
  api(:history, params).checkins
end

#parse_response(response) ⇒ Object



113
114
115
116
# File 'lib/foursquare.rb', line 113

def parse_response(response)
  raise_errors(response)
  Crack::JSON.parse(response.body)
end

#post(url, body) ⇒ Object



126
127
128
# File 'lib/foursquare.rb', line 126

def post(url, body)
  parse_response(@oauth.access_token.post(url, body))
end

#settings_setpings(params = {}) ⇒ Object



192
193
194
# File 'lib/foursquare.rb', line 192

def settings_setpings(params = {})
  api(:settings_setpings=, params).settings
end

#tip_markdone(params = {}) ⇒ Object



160
161
162
# File 'lib/foursquare.rb', line 160

def tip_markdone(params = {})
  api(:tip_markdone=, params).tip
end

#tip_marktodo(params = {}) ⇒ Object



156
157
158
# File 'lib/foursquare.rb', line 156

def tip_marktodo(params = {})
  api(:tip_marktodo=, params).tip
end

#to_query_params(options) ⇒ Object



118
119
120
# File 'lib/foursquare.rb', line 118

def to_query_params(options)
  options.collect { |key, value| "#{key}=#{value}" }.join('&')
end

#venue_flagclosed(params = {}) ⇒ Object



148
149
150
# File 'lib/foursquare.rb', line 148

def venue_flagclosed(params = {})
  api(:venue_flagclosed=, params)
end

#venue_proposeedit(params = {}) ⇒ Object



144
145
146
# File 'lib/foursquare.rb', line 144

def venue_proposeedit(params = {})
  api(:venue_proposeedit=, params)
end