class RFacebook::FacebookSession
Deprecated Methods
↑ topError classes
↑ topProperties
↑ topAttributes
Can be set to any valid logger (for example, RAIL_DEFAULT_LOGGER)
The expiration time of this session, as given from Facebook API login.
The key for this session. You will need to save this for infinite sessions.
The user id of the user associated with this sesssion.
Public Interface
↑ topPublic Class Methods
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
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
Sets whether or not we suppress exceptions from being thrown
# File lib/facebook_session.rb, line 125 def quiet=(val) @quiet = val end
Returns true if exceptions are being suppressed in favor of log messages
# File lib/facebook_session.rb, line 120 def quiet? return @quiet end
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
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
↑ topUtility methods
↑ topPrivate Instance Methods
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
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
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