Class: Facebook::GraphClient
- Inherits:
-
Object
- Object
- Facebook::GraphClient
- Defined in:
- lib/facebook-graphclient.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #api(action, method, query_params = nil) ⇒ Object
- #fql(query) ⇒ Object
- #get_user_cookie(cookies) ⇒ Object
-
#initialize(facebook_settings = {}) ⇒ GraphClient
constructor
A new instance of GraphClient.
- #params ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(facebook_settings = {}) ⇒ GraphClient
Returns a new instance of GraphClient.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/facebook-graphclient.rb', line 18 def initialize facebook_settings = {} @app_id = facebook_settings[:app_id] @api_key = facebook_settings[:api_key] @secret = facebook_settings[:secret] @cookie = facebook_settings[:cookies] @access_token = facebook_settings[:access_token] || @cookie['access_token'] @session = Patron::Session.new end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
16 17 18 |
# File 'lib/facebook-graphclient.rb', line 16 def access_token @access_token end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
15 16 17 |
# File 'lib/facebook-graphclient.rb', line 15 def api_key @api_key end |
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
15 16 17 |
# File 'lib/facebook-graphclient.rb', line 15 def app_id @app_id end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
15 16 17 |
# File 'lib/facebook-graphclient.rb', line 15 def secret @secret end |
Instance Method Details
#[](k) ⇒ Object
124 125 126 |
# File 'lib/facebook-graphclient.rb', line 124 def [] k params[k] end |
#api(action, method, query_params = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/facebook-graphclient.rb', line 46 def api action, method, query_params = nil query_params[:access_token] ||= @access_token query_string = '?' + query_params.map { |k,v| "#{k}=#{v}" }.join("&") unless query_params.empty? tries = 0 begin raw_response = @session.send(action, GRAPH_URL + method + query_string) rescue Patron::HostResolutionError, Patron::ConnectionFailed retry if tries < 5 tries += 1 end # TODO: Handle photo requests, which return photo data and not JSON if raw_response.headers['Content-Type'] =~ /text\/javascript/ # We have JSON json = ["false", '', nil].include?(raw_response.body) ? '{}' : raw_response.body response = Yajl::Parser.parse(json) if e = response['error'] error = FacebookError.new(e['message']) error.data = e raise error else response end end end |
#fql(query) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/facebook-graphclient.rb', line 76 def fql query query_params = { :access_token => @access_token, :format => 'json', :query => Rack::Utils.escape(query) } query_string = '?' + query_params.map { |k,v| "#{k}=#{v}" }.join("&") unless query_params.empty? tries = 0 begin raw_response = @session.get("https://api.facebook.com/method/fql.query" + query_string) rescue Patron::HostResolutionError, Patron::ConnectionFailed retry if tries < 5 tries += 1 end json = ["false", '', nil].include?(raw_response.body) ? '{}' : raw_response.body response = Yajl::Parser.parse(json) if e = response.first['error'] error = FacebookError.new(e['message']) error.data = e raise error else response end end |
#get_user_cookie(cookies) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/facebook-graphclient.rb', line 30 def if and = ["fbs_#{@app_id}"] Rack::Utils.parse_nested_query() else {} end end |
#params ⇒ Object
128 129 130 |
# File 'lib/facebook-graphclient.rb', line 128 def params valid? ? @params : {} end |
#valid? ⇒ Boolean
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/facebook-graphclient.rb', line 105 def valid? return false if @cookie.nil? unless @is_valid vars = @cookie.dup good_sig = vars.delete 'sig' sig = Digest::MD5.hexdigest(vars.sort.map { |k,v| "#{k}=#{v}" }.compact.join + @secret) if @is_valid = (sig == good_sig) @params = vars else @params = {} end end @is_valid end |