lib/foursquare.rb in foursquare-oauth-0.2.2 vs lib/foursquare.rb in foursquare-oauth-0.2.3

- old
+ new

@@ -1,14 +1,10 @@ require 'rubygems' require 'httparty' require 'hashie' require 'oauth' -Hash.send :include, Hashie::HashExtensions - - - module Foursquare class OAuth def initialize(ctoken, csecret, options={}) @consumer_token, @consumer_secret = ctoken, csecret end @@ -82,27 +78,27 @@ # # 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 # - 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 - - def api(method_symbol, params = {}) - Hashie::Mash.new(method_missing(method_symbol, params)) - end - + # 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 + # + # def api(method_symbol, params = {}) + # Hashie::Mash.new(method_missing(method_symbol, params)) + # end + # 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}" @@ -110,92 +106,134 @@ url end def parse_response(response) raise_errors(response) - Crack::JSON.parse(response.body) + + Hashie::Mash.new(Yajl::Parser.parse(response.body)) end def to_query_params(options) - options.collect { |key, value| "#{key}=#{value}" }.join('&') + options.map{|key, value| "#{key}=#{value}"}.join('&') end - def get(url) - parse_response(@oauth.access_token.get(url)) + def get(url, params) + parse_response(@oauth.access_token.get(api_url(url, params))) end def post(url, body) - parse_response(@oauth.access_token.post(url, body)) + parse_response(@oauth.access_token.post(api_url(url), body)) end # API method wrappers + # Check in methods + + def checkins(params = {}) + get('/checkins', params).checkins + end + def checkin(params = {}) - api(:checkin=, params).checkin + post('/checkin', params).checkin end def history(params = {}) - api(:history, params).checkins + get('/history', params).checkins end - def addvenue(params = {}) - api(:addvenue=, params).venue + # User methods + + def user(params = {}) + get('/user', params).user end - def venue_proposeedit(params = {}) - api(:venue_proposeedit=, params) + def friends(params = {}) + get('/friends', params).friends end - def venue_flagclosed(params = {}) - api(:venue_flagclosed=, params) + # Venue methods + + def venues(params = {}) + get('/venues', params).venues end - def addtip(params = {}) - api(:addtip=, params).tip + def venue(params = P) + get('/venue', params).venue end - def tip_marktodo(params = {}) - api(:tip_marktodo=, params).tip + def categories + get('/categories', params).categories end + + def add_venue(params = {}) + post('/addvenue', params).venue + end - def tip_markdone(params = {}) - api(:tip_markdone=, params).tip + def venue_propose_edit(params = {}) + post('/venue/proposeedit', params) end + def venue_flag_closed(params = {}) + post('/venue/flagclosed', params) + end + + # Tip methods + + def tips(params = {}) + get('/tips', params).tips + end + + def add_tip(params = {}) + post('/addtip', params).tip + end + + def tip_mark_todo(params = {}) + post('/tip/marktodo', params).tip + end + + def tip_mark_done(params = {}) + post('/tip/markdone', params).tip + end + + # Friend methods + def friend_requests - api(:friend_requests).requests + get('/friend/requests').requests end def friend_approve(params = {}) - api(:friend_approve=, params).user + post('/friend/approve', params).user end def friend_deny(params = {}) - api(:friend_deny=, params).user + post('/friend/deny', params).user end - def friend_sendrequest(params = {}) - api(:friend_sendrequest=, params).user + def friend_send_request(params = {}) + post('/friend/sendrequest', params).user end - def findfriends_byname(params = {}) - api(:findfriends_byname, params).users + def friends_by_name(params = {}) + post('/findfriends/byname', params).users end - def findfriends_byphone(params = {}) - api(:findfriends_byphone, params).users + def friends_by_phone(params = {}) + get('/findfriends/byphone', params).users end - def findfriends_bytwitter(params = {}) - api(:findfriends_bytwitter, params).users + def friends_by_twitter(params = {}) + get('/findfriends/bytwitter', params).users end - def settings_setpings(params = {}) - api(:settings_setpings=, params).settings + def settings_set_pings(params = {}) + post('/settings/setpings', params).settings end - private + def test(params = {}) + get('/test', params) + end + private def raise_errors(response) message = "(#{response.code}): #{response.message} - #{response.inspect} - #{response.body}" case response.code.to_i \ No newline at end of file