class RFacebook::FacebookSession

Deprecated Methods

↑ top

Error classes

↑ top

Properties

↑ top

Attributes

logger[RW]

Can be set to any valid logger (for example, RAIL_DEFAULT_LOGGER)

session_expires[R]

The expiration time of this session, as given from Facebook API login.

session_key[R]

The key for this session. You will need to save this for infinite sessions.

session_user_id[R]

The user id of the user associated with this sesssion.

Public Interface

↑ top

Public Class Methods

new(api_key, api_secret, quiet = false) click to toggle source

Constructs a FacebookSession.

api_key

your API key

api_secret

your API secret

quiet

boolean, set to true if you don't want exceptions to be thrown (defaults to false)

# File lib/facebook_session.rb, line 95
def initialize(api_key, api_secret, quiet = false)
  # required parameters
  @api_key = api_key
  @api_secret = api_secret
    
  # optional parameters
  @quiet = quiet

  # initialize internal state
  @last_error_message = nil # DEPRECATED
  @last_error_code = nil # DEPRECATED
  @expired = false
end

Public Instance Methods

expired?() click to toggle source

Returns true if the session is expired (will often mean that the session is not ready as well)

# File lib/facebook_session.rb, line 115
def expired?
  return @expired
end
quiet=(val) click to toggle source

Sets whether or not we suppress exceptions from being thrown

# File lib/facebook_session.rb, line 125
def quiet=(val)
  @quiet = val
end
quiet?() click to toggle source

Returns true if exceptions are being suppressed in favor of log messages

# File lib/facebook_session.rb, line 120
def quiet?
  return @quiet
end
ready?() click to toggle source

Template method. Returns true when the session is definitely prepared to make API calls.

# File lib/facebook_session.rb, line 110
def ready?
  raise NotImplementedError
end
signature(params) click to toggle source

Template method. Used for signing a set of parameters in the way that Facebook specifies: <developers.facebook.com/documentation.php?v=1.0&doc=auth>

params

a Hash containing the parameters to sign

# File lib/facebook_session.rb, line 133
def signature(params)
  raise NotImplementedError
end

Serialization

↑ top

Utility methods

↑ top

Private Instance Methods

handle_xml_response(rawXML) click to toggle source

Wraps an XML response in a Facepricot XML document, and checks for an error response (raising or logging errors as needed)

NOTE: Facepricot chaining may be deprecated in the 1.0 release

# File lib/facebook_session.rb, line 208
def handle_xml_response(rawXML)
  facepricotXML = Facepricot.new(rawXML)

  # error checking    
  if facepricotXML.at("error_response")
  
    # get the error code
    errorCode = facepricotXML.at("error_code").inner_html.to_i
    errorMessage = facepricotXML.at("error_msg").inner_html
    log_debug "** RFACEBOOK(GEM) - RFacebook::FacebookSession\#remote_call - remote call failed (#{errorCode}: #{errorMessage})"
  
    # TODO: remove these 2 lines
    @last_error_message = "ERROR #{errorCode}: #{errorMessage}" # DEPRECATED
    @last_error_code = errorCode # DEPRECATED
  
    # check to see if this error was an expired session error
    case errorCode

    # the remote method did not exist, convert that to a standard Ruby no-method error
    when 3
      raise NoMethodError, errorMessage unless quiet? == true
    
    # the parameters were wrong, or not enough parameters...convert that to a standard Ruby argument error
    when 100,606
      raise ArgumentError, errorMessage unless quiet? == true
    
    # when the session expires, we need to record that internally
    when 102
      @expired = true
      raise ExpiredSessionStandardError.new(errorMessage, errorCode) unless quiet? == true
  
    # otherwise, just raise a regular remote error with the error code
    else
      raise RemoteStandardError.new(errorMessage, errorCode) unless quiet? == true
    end
  
    # since the quiet flag may have been activated, we may not have thrown
    # an actual exception, so we still need to return nil here
    return nil
  end

  # everything was just fine, return the Facepricot XML response
  return facepricotXML
end
method_missing(methodSymbol, *params) click to toggle source

This allows any Facebook method to be called, using the Ruby mechanism for responding to unimplemented methods. Basically, this converts a call to “auth_getSession” to “auth.getSession” and does the proper API call using the parameter hash given.

This allows you to call an API method such as facebook.users.getInfo by calling “fbsession.users_getInfo”

# File lib/facebook_session.rb, line 150
def method_missing(methodSymbol, *params)
  # get the remote method name
  remoteMethod = methodSymbol.to_s.gsub("_", ".")
  if methodSymbol.to_s.match(/cached_(.*)/)
    log_debug "** RFACEBOOK(GEM) - DEPRECATION NOTICE - cached methods are deprecated, making a raw call without caching."
    tokens.shift
  end

  # there can only be one parameter, a Hash, for remote methods
  unless (params.size == 1 and params.first.is_a?(Hash))
    log_debug "** RFACEBOOK(GEM) - when you call a remote Facebook method"
  end

  # make the remote method call
  return remote_call(remoteMethod, params.first)  
end
post_request(params, useSSL=false) click to toggle source

Posts a request to the remote Facebook API servers, and returns the raw text body of the result

params

a Hash of the post parameters to send to the REST API

useSSL

defaults to false, set to true if you want to use SSL for the POST

# File lib/facebook_session.rb, line 258
def post_request(params, useSSL=false)
  # get a server handle
  port = (useSSL == true) ? 443 : 80
  http_server = Net::HTTP.new(API_HOST, port)
  http_server.use_ssl = useSSL

  # build a request
  http_request = Net::HTTP::Post.new(API_PATH_REST)
  http_request.form_data = params

  # get the response XML
  return http_server.start{|http| http.request(http_request)}.body
end