lib/meal_ticket.rb in meal_ticket-0.1.0 vs lib/meal_ticket.rb in meal_ticket-0.1.1
- old
+ new
@@ -6,16 +6,16 @@
FLICKR_API_BASE_URL = "http://api.flickr.com/services/" # :nodoc:
# [root_url] The base url of your app, eg "http://www.google.com/". If you're running a rails app, you can literally type root_url
# [scope] A comma-separated list of permissions. For a full list of permissions, see https://developers.facebook.com/docs/authentication/permissions/
def facebook_auth_url(root_url, scope)
- "https://graph.facebook.com/oauth/authorize?client_id=#{FACEBOOK_APP_ID}&redirect_uri=#{root_url}meal_ticket/facebook_callback&scope=#{scope}"
+ "https://graph.facebook.com/oauth/authorize?client_id=#{MealTicket::Config.facebook_app_id}&redirect_uri=#{root_url}meal_ticket/facebook_callback&scope=#{scope}"
end
# Generates the URL for the 2nd step of Facebook auth - exchanging the code for user data
def facebook_exchange_url(root_url, code) # :nodoc:
- "https://graph.facebook.com/oauth/access_token?client_id=#{FACEBOOK_APP_ID}&redirect_uri=#{root_url}meal_ticket/facebook_callback&client_secret=#{FACEBOOK_SECRET}&code=#{CGI::escape code}"
+ "https://graph.facebook.com/oauth/access_token?client_id=#{MealTicket::Config.facebook_app_id}&redirect_uri=#{root_url}meal_ticket/facebook_callback&client_secret=#{MealTicket::Config.facebook_secret}&code=#{CGI::escape code}"
end
# [perm] A single permission level. Permissions can be read, write, or delete. Each successive permission implies the ones before it, eg "write" implies "read". For more information, see http://www.flickr.com/services/api/auth.spec.html
def flickr_auth_url(perm)
flickr_url({"perms" => perm}, "auth")
@@ -27,11 +27,11 @@
end
private
def flickr_url(arg_hash, endpoint = "rest")
- arg_hash.merge!({"api_key" => FLICKR_TOKEN})
+ arg_hash.merge!({"api_key" => MealTicket::Config.flickr_token})
arg_list = []
arg_hash.each do |key, value|
arg_list << "#{key}=#{value}"
end
"#{FLICKR_API_BASE_URL}#{endpoint}/?&api_sig=#{flickr_sign(arg_hash)}&#{arg_list.join('&')}"
@@ -41,20 +41,26 @@
arg_list = []
arg_hash.keys.sort.each do |key|
arg_list << key
arg_list << arg_hash[key]
end
- Digest::MD5.hexdigest("#{FLICKR_SECRET}#{arg_list.join()}")
+ Digest::MD5.hexdigest("#{MealTicket::Config.flickr_secret}#{arg_list.join()}")
end
end
require 'net/http'
require 'net/https'
require 'crack'
require 'json'
class MealTicket
+
+ class Config
+ cattr_accessor :facebook_app_id, :facebook_secret, :facebook_callback
+ cattr_accessor :flickr_token, :flickr_secret, :flickr_callback
+ end
+
include MealTicketRoutes
def initialize(app)
@app = app
end
@@ -69,25 +75,29 @@
@app.call(env)
end
end
def facebook_callback
- response = get facebook_exchange_url(get_root_url, get_query_string_parameter("code"))
+ if get_query_string_parameter("code")
+ response = get facebook_exchange_url(get_root_url, get_query_string_parameter("code"))
- if response.body.include? "access_token"
- response.body =~ /access_token=([^&]+)(?:&expires=(.*))?/ # TODO: genericize get_query_string_parameter to handle this?
- token = $1 || ""
- expires = $2 || ""
- query_string = "facebook[token]=#{CGI.escape(token)}&facebook[expires]=#{CGI.escape(expires)}"
+ if response.body.include? "access_token"
+ response.body =~ /access_token=([^&]+)(?:&expires=(.*))?/ # TODO: genericize get_query_string_parameter to handle this?
+ token = $1 || ""
+ expires = $2 || ""
+ query_string = "facebook[token]=#{CGI.escape(token)}&facebook[expires]=#{CGI.escape(expires)}"
+ else
+ parsed = JSON.parse(response.body)
+ query_string = to_params({:facebook => parsed})
+ end
else
- parsed = JSON.parse(response.body)
- query_string = to_params({:facebook => parsed})
+ query_string = "facebook[error]=#{get_query_string_parameter("error")}"
end
body = %(<html><body>You are being redirected.</body></html>)
headers = {
- 'Location' => "#{get_root_url}#{FACEBOOK_CALLBACK}?#{query_string}",
+ 'Location' => "#{get_root_url}#{MealTicket::Config.facebook_callback}?#{query_string}",
'Content-Type' => 'text/html',
'Content-Length' => body.length.to_s
}
[302, headers, [body]]
end
@@ -107,10 +117,10 @@
query_string = "flickr[error][code]=#{CGI.escape code}&flickr[error][msg]=#{CGI.escape msg}"
end
body = %(<html><body>You are being redirected.</body></html>)
headers = {
- 'Location' => "#{get_root_url}#{FLICKR_CALLBACK}?#{query_string}",
+ 'Location' => "#{get_root_url}#{MealTicket::Config.flickr_callback}?#{query_string}",
'Content-Type' => 'text/html',
'Content-Length' => body.length.to_s
}
[302, headers, [body]]
end