lib/koala.rb in koala-1.0.0.rc vs lib/koala.rb in koala-1.0.0
- old
+ new
@@ -19,29 +19,13 @@
module Koala
module Facebook
# Ruby client library for the Facebook Platform.
- # Copyright 2010 Facebook
- # Adapted from the Python library by Alex Koppel, Rafi Jacoby, and the team at Context Optional
- #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may
- # not use this file except in compliance with the License. You may obtain
- # a copy of the License at
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- # License for the specific language governing permissions and limitations
- # under the License.
- #
- # This client library is designed to support the Graph API and the official
- # Facebook JavaScript SDK, which is the canonical way to implement
- # Facebook authentication. Read more about the Graph API at
- # http://developers.facebook.com/docs/api. You can download the Facebook
- # JavaScript SDK at http://github.com/facebook/connect-js/.
+ # Copyright 2010-2011 Alex Koppel
+ # Contributors: Alex Koppel, Chris Baclig, Rafi Jacoby, and the team at Context Optional
+ # http://github.com/arsduo/koala
class API
# initialize with an access token
def initialize(access_token = nil)
@access_token = access_token
@@ -177,30 +161,30 @@
callback = options[:callback] || @oauth_callback_url
raise ArgumentError, "url_for_access_token must get a callback either from the OAuth object or in the parameters!" unless callback
"https://#{GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@app_secret}&code=#{code}"
end
- def get_access_token_info(code)
+ def get_access_token_info(code, options = {})
# convenience method to get a parsed token from Facebook for a given code
# should this require an OAuth callback URL?
- get_token_from_server(:code => code, :redirect_uri => @oauth_callback_url)
+ get_token_from_server({:code => code, :redirect_uri => @oauth_callback_url}, false, options)
end
- def get_access_token(code)
+ def get_access_token(code, options = {})
# upstream methods will throw errors if needed
- if info = get_access_token_info(code)
+ if info = get_access_token_info(code, options)
string = info["access_token"]
end
end
- def get_app_access_token_info
+ def get_app_access_token_info(options = {})
# convenience method to get a the application's sessionless access token
- get_token_from_server({:type => 'client_cred'}, true)
+ get_token_from_server({:type => 'client_cred'}, true, options)
end
- def get_app_access_token
- if info = get_app_access_token_info
+ def get_app_access_token(options = {})
+ if info = get_app_access_token_info(options)
string = info["access_token"]
end
end
# Originally provided directly by Facebook, however this has changed
@@ -221,16 +205,16 @@
return envelope
end
# from session keys
- def get_token_info_from_session_keys(sessions)
+ def get_token_info_from_session_keys(sessions, options = {})
# fetch the OAuth tokens from Facebook
response = fetch_token_string({
:type => 'client_cred',
:sessions => sessions.join(",")
- }, true, "exchange_sessions")
+ }, true, "exchange_sessions", options)
# Facebook returns an empty body in certain error conditions
if response == ""
raise APIError.new({
"type" => "ArgumentError",
@@ -239,28 +223,28 @@
end
JSON.parse(response)
end
- def get_tokens_from_session_keys(sessions)
+ def get_tokens_from_session_keys(sessions, options = {})
# get the original hash results
- results = get_token_info_from_session_keys(sessions)
+ results = get_token_info_from_session_keys(sessions, options)
# now recollect them as just the access tokens
results.collect { |r| r ? r["access_token"] : nil }
end
- def get_token_from_session_key(session)
+ def get_token_from_session_key(session, options = {})
# convenience method for a single key
# gets the overlaoded strings automatically
- get_tokens_from_session_keys([session])[0]
+ get_tokens_from_session_keys([session], options)[0]
end
protected
- def get_token_from_server(args, post = false)
+ def get_token_from_server(args, post = false, options = {})
# fetch the result from Facebook's servers
- result = fetch_token_string(args, post)
+ result = fetch_token_string(args, post, "access_token", options)
# if we have an error, parse the error JSON and raise an error
raise APIError.new((JSON.parse(result)["error"] rescue nil) || {}) if result =~ /error/
# otherwise, parse the access token
@@ -273,14 +257,14 @@
hash.merge!(key => value)
end
components
end
- def fetch_token_string(args, post = false, endpoint = "access_token")
+ def fetch_token_string(args, post = false, endpoint = "access_token", options = {})
Koala.make_request("/oauth/#{endpoint}", {
:client_id => @app_id,
:client_secret => @app_secret
- }.merge!(args), post ? "post" : "get", :use_ssl => true).body
+ }.merge!(args), post ? "post" : "get", {:use_ssl => true}.merge!(options)).body
end
# base 64
# directly from https://github.com/facebook/crypto-request-examples/raw/master/sample.rb
def base64_url_decode(str)