Module: FacebookApi

Defined in:
lib/facebook_api.rb,
lib/facebook_api/session.rb

Defined Under Namespace

Classes: Configuration, Error, Session

Constant Summary collapse

API_VERSION =

:nodoc:

'1.0'
REST_URL =

:nodoc:

'http://api.facebook.com/restserver.php'

Class Method Summary collapse

Class Method Details

.api_keyObject

Returns the api key. set this with #configure.



31
32
33
# File 'lib/facebook_api.rb', line 31

def self.api_key
  config.api_key
end

.calculate_signature(params) ⇒ Object

Calculates a signature, as described in the API docs here.



94
95
96
97
# File 'lib/facebook_api.rb', line 94

def self.calculate_signature(params)
  params_string = params.sort.inject('') { |str, pair| str << pair[0] << '=' << pair[1] }
  Digest::MD5.hexdigest(params_string + secret_key)
end

.configObject

Returns the current Facebook configuration. This gets set with #configure.



53
54
55
# File 'lib/facebook_api.rb', line 53

def self.config
  @config
end

.configure {|@config| ... } ⇒ Object

Allows you to set your Facebook configuration for accessing the REST API:

FacebookApi.configure do |config|
  config.api_key = 'YOUR_API_KEY'
  config.secret_key = 'YOUR_SECRET_KEY'
  config.canvas_page_name = 'YOUR_CANVAS_PAGE_NAME'
  config.callback_url = 'YOUR_CALLBACK_URL'
end

Yields:



48
49
50
# File 'lib/facebook_api.rb', line 48

def self.configure(&block)
  yield @config
end

.convert_time(time) ⇒ Object

Helper to convert ActiveSupport::TimeWithZone from local time to UTC. Use this when sending date/times to Facebook as Facebook expects times to be sent as UTC converted to a Unix timestamp.



102
103
104
105
106
107
108
109
# File 'lib/facebook_api.rb', line 102

def self.convert_time(time)
  if time.is_a?(ActiveSupport::TimeWithZone)
    pacific_zone = ActiveSupport::TimeZone["UTC"]
    pacific_zone.parse(time.strftime("%Y-%m-%d %H:%M:%S"))
  else
    time
  end
end

.loggerObject

Returns the logger for Facebook calls. By default, this outputs to STDOUT.



22
23
24
25
26
27
28
# File 'lib/facebook_api.rb', line 22

def self.logger
  unless @logger
    @logger = ::Logger.new($stdout)
    @logger.level = Logger::INFO
  end
  @logger
end

.secret_keyObject

Returns the secret key. set this with #configure.



36
37
38
# File 'lib/facebook_api.rb', line 36

def self.secret_key
  config.secret_key    
end

.verify_connect_cookies_signature(args) ⇒ Object

Verifies the signature in the cookies set by Facebook Connect checks out. Returns true if the signature is valid, false otherwise. See the API docs here for more details on how this is calculated.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/facebook_api.rb', line 79

def self.verify_connect_cookies_signature(args)
  signature = args.delete(api_key)
  return false if signature.nil?

  signed_args = Hash.new
  args.each do |k, v|
    if k =~ /^#{api_key}_(.*)/
      signed_args[$1] = v
    end
  end
  
  signature == calculate_signature(signed_args)
end

.verify_facebook_params_signature(args) ⇒ Object

Verifies the signature of parmaters sent by Facebook. Returns true if the signature is valid, false otherwise See the API docs here for more details on how this is calculated.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/facebook_api.rb', line 61

def self.verify_facebook_params_signature(args)
  signature = args.delete('fb_sig')
  return false if signature.nil?

  signed_args = Hash.new
  args.each do |k, v|
    if k =~ /^fb_sig_(.*)/
      signed_args[$1] = v
    end
  end

  signature == calculate_signature(signed_args)
end