Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello, just to let you know I did some changes to enable userless access, feel free to ignore #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
*.swo
2 changes: 1 addition & 1 deletion lib/foursquare_checkins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def deletecomment(checkin_id, params={})
perform_graph_request("checkins/#{checkin_id}/deletecomment", params, "post")
end
end
end
end
49 changes: 40 additions & 9 deletions lib/foursquare_node.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
module MetaFour

BASE_URL = "https://api.foursquare.com:443/v2/"

attr_accessor :client_id, :client_secret

def base_url
@base_url || BASE_URL
end

def base_url=(value)
@base_url = value if value
end
end

module Foursquare

class Node
extend MetaFour

def initialize(access_token)
@access_token = access_token
@base_url = "https://api.foursquare.com:443/v2/"
@access_token = access_token
end

def perform_graph_request(endpoint, params={}, method="get")
def self.perform_graph_request(endpoint, params={}, method="get")
require 'net/http'

@query_string = "?"
@query_string += "oauth_token=#{CGI.escape(@access_token)}" unless @access_token.empty?
query_string = "?"
if params[:oauth_token]
query_string += "oauth_token=#{params[:oauth_token]}"
elsif params[:client_id] && params[:client_secret]
query_string += "client_id=#{params[:client_id]}&client_secret=#{params[:client_secret]}"
elsif @client_id && @client_secret
query_string += "client_id=#{@client_id}&client_secret=#{@client_secret}"
else
raise "client_id and client_secret or oauth_token are required."
end
params.reject! {|k,v| [:oauth_token, :client_secret, :client_id].include?(k) }

if method=="get"
params.each{|key, val| @query_string += "&#{key}=#{val}"}
url = URI.parse("#{@base_url}#{endpoint}#{@query_string}")
params.each{|key, val| query_string += "&#{key}=#{val}"}
url = URI.parse("#{base_url}#{endpoint}#{query_string}")
request = Net::HTTP::Get.new("#{url.path}?#{url.query}",{"Content-Type"=>"text/json"})
else
url = URI.parse("#{@base_url}#{endpoint}#{@query_string}")
url = URI.parse("#{base_url}#{endpoint}#{query_string}")
request = Net::HTTP::Post.new("#{url.path}?#{url.query}",{"Content-Type"=>"text/json"})
request.set_form_data(params)
end
Expand All @@ -26,8 +52,13 @@ def perform_graph_request(endpoint, params={}, method="get")
response = JSON.parse(http.start {|http| http.request(request)}.body)
response
end

def perform_graph_request(endpoint, params={}, method="get")
h = {:oauth_token => @oauth_token}
self.class.perform_graph_request(endpoint, params.merge(h), method)
end
end

def self.exchange_access_token(code, client_id, client_secret, callback)
require 'net/http'

Expand Down
2 changes: 1 addition & 1 deletion lib/foursquare_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def set(setting_id, params={})
perform_graph_request("settings/#{setting_id}/set", params, "post")
end
end
end
end
2 changes: 1 addition & 1 deletion lib/foursquare_tips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def unmark(tip_id)
perform_graph_request("tips/#{tip_id}/unmark", {}, "post")
end
end
end
end
10 changes: 5 additions & 5 deletions lib/foursquare_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def search(params={})
end

def requests
perform_graph_request("users/requests")
perform_graph_request("users/requests", {})
end

#Aspects
def badges(user_id)
perform_graph_request("users/#{user_id}/badges")
perform_graph_request("users/#{user_id}/badges", {})
end

def checkins(user_id="self", params={})
Expand All @@ -31,7 +31,7 @@ def checkins(user_id="self", params={})
end

def friends(user_id="self")
perform_graph_request("users/#{user_id}/friends")
perform_graph_request("users/#{user_id}/friends", params)
end

def tips(user_id="self", params={})
Expand All @@ -49,7 +49,7 @@ def todos(user_id="self", params={})
end

def venuehistory(user_id="self")
perform_graph_request("users/#{user_id}/venuehistory")
perform_graph_request("users/#{user_id}/venuehistory", params)
end

#Actions
Expand All @@ -75,4 +75,4 @@ def setpings(user_id, params={})
perform_graph_request("users/#{user_id}/setpings", params, "post")
end
end
end
end
19 changes: 13 additions & 6 deletions lib/foursquare_venue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,37 @@ def add(params={})
perform_graph_request("venues/add", params, "post")
end

def categories
perform_graph_request("venues/categories", {})
def self.categories(params={})
perform_graph_request("venues/categories", params)
end

def search(params={})
def categories(params={})
perform_graph_request("venues/categories", params)
end

def self.search(params={})
params = {:ll => "37.792694,-122.409325",
:llAcc => "100",
:alt => "0",
:altAcc=>"100",
:query=>"",
:limit=>"50",
:intent=>"checkin"}.merge!(params)

perform_graph_request("venues/search", params)
end

def search(params={})
self.class.search(params.merge(:oauth_token => @access_token))
end

#Aspects
def herenow(venue_id)
perform_graph_request("venues/#{venue_id}/herenow", {})
end

#sort = recent or popular
def tips(venue_id, params={:sort => "recent"})
perform_graph_request("venues/#{venue_id}/tips", {})
perform_graph_request("venues/#{venue_id}/tips", params)
end

#marktodo
Expand Down Expand Up @@ -68,4 +75,4 @@ def proposeedit(venue_id, params={})
perform_graph_request("venues/#{venue_id}/proposeedit", params, "post")
end
end
end
end