Class: FGraph::Client
- Inherits:
-
Object
- Object
- FGraph::Client
- Defined in:
- lib/fgraph/client.rb
Overview
Facebook proxy class to call Facebook Graph API methods with default options. Please refer to FGraph method documentation for more information.
Constant Summary collapse
- @@instance =
nil
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#oauth_client ⇒ Object
readonly
Returns the value of attribute oauth_client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.instance ⇒ Object
Return static instance of FGraph::Client with default options set in FGraph.config.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Client
constructor
Initialize Client with default options, so options are not required to be passed when calling respective Facebook Graph API methods.
- #insights(options = {}) ⇒ Object
- #me(*args) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #oauth_access_token(redirect_uri, code) ⇒ Object
- #oauth_app_access_token ⇒ Object
- #oauth_authorize_url(redirect_uri, options = {}) ⇒ Object
- #object(id, options = {}) ⇒ Object
- #objects(*args) ⇒ Object
- #publish(id, options = {}) ⇒ Object
- #remove(id, options = {}) ⇒ Object
- #search(query, options = {}) ⇒ Object
- #update_options(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Initialize Client with default options, so options are not required to be passed when calling respective Facebook Graph API methods.
Options
-
client_id - Application ID
-
client_secret - Application Secret
-
access_token - Access token, required to publish to Facebook Graph or access current user profile.
-
app_access_token - Application access token, required to access Facebook insights. Auto generated if client_id and client_secret option are provided.
Initialize with default options
fg_client = FGraph::Client.new(:client_id => '...', :client_secret => '...') fg_client.oauth_authorize_url('[redirect uri]', :scope => 'publish_stream') fg_client.oauth_access_token('[redirect uri]', '[authorization code]')
Intialize with access token
fg_client = FGraph::Client.new(:access_token => '...') fg_client.me fg.client.publish_feed('herryanto', :message => 'Cool!')
45 46 47 |
# File 'lib/fgraph/client.rb', line 45 def initialize(={}) @options = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/fgraph/client.rb', line 106 def method_missing(name, *args, &block) names = name.to_s.split('_') super unless names.length > 1 case names.shift when 'object' # object_photos self.object("#{args[0]}/#{names[0]}", args[1]) when 'me' # me_photos self.me(names[0], args[0]) when 'publish' # publish_feed(id) self.publish("#{args[0]}/#{names[0]}", args[1]) when 'remove' # remove_feed(id) self.remove("#{args[0]}/#{names[0]}", args[1]) when 'search' # search_user(query) = args[1] || {} [:type] = names[0] self.search(args[0], ) else super end end |
Instance Attribute Details
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
6 7 8 |
# File 'lib/fgraph/client.rb', line 6 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
6 7 8 |
# File 'lib/fgraph/client.rb', line 6 def client_secret @client_secret end |
#oauth_client ⇒ Object (readonly)
Returns the value of attribute oauth_client.
6 7 8 |
# File 'lib/fgraph/client.rb', line 6 def oauth_client @oauth_client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/fgraph/client.rb', line 6 def @options end |
Class Method Details
.instance ⇒ Object
Return static instance of FGraph::Client with default options set in FGraph.config.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fgraph/client.rb', line 12 def self.instance return @@instance if @@instance if FGraph.config @@instance = FGraph::Client.new( :client_id => FGraph.config['app_id'], :client_secret => FGraph.config['app_secret'] ) else @@instance = FGraph::Client.new end end |
Instance Method Details
#insights(options = {}) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/fgraph/client.rb', line 99 def insights(={}) unless self.[:app_access_token] self.[:app_access_token] = self.oauth_app_access_token end FGraph.insights(self.[:client_id], self.[:app_access_token]['access_token'], ) end |
#me(*args) ⇒ Object
76 77 78 79 80 |
# File 'lib/fgraph/client.rb', line 76 def me(*args) = args.last.is_a?(Hash) ? args.pop : {} args << {:access_token => self.[:access_token]}.merge() FGraph.me(*args) end |
#oauth_access_token(redirect_uri, code) ⇒ Object
57 58 59 60 |
# File 'lib/fgraph/client.rb', line 57 def oauth_access_token(redirect_uri, code) FGraph.oauth_access_token(self.[:client_id], self.[:client_secret], :redirect_uri => redirect_uri, :code => code) end |
#oauth_app_access_token ⇒ Object
62 63 64 |
# File 'lib/fgraph/client.rb', line 62 def oauth_app_access_token FGraph.oauth_app_access_token(self.[:client_id], self.[:client_secret]) end |
#oauth_authorize_url(redirect_uri, options = {}) ⇒ Object
53 54 55 |
# File 'lib/fgraph/client.rb', line 53 def (redirect_uri, ={}) FGraph.(self.[:client_id], redirect_uri, ) end |
#object(id, options = {}) ⇒ Object
66 67 68 |
# File 'lib/fgraph/client.rb', line 66 def object(id, ={}) FGraph.object(id, {:access_token => self.[:access_token]}.merge( || {})) end |
#objects(*args) ⇒ Object
70 71 72 73 74 |
# File 'lib/fgraph/client.rb', line 70 def objects(*args) = args.last.is_a?(Hash) ? args.pop : {} args << {:access_token => self.[:access_token]}.merge() FGraph.objects(*args) end |
#publish(id, options = {}) ⇒ Object
82 83 84 85 86 |
# File 'lib/fgraph/client.rb', line 82 def publish(id, ={}) FGraph.publish(id, { :access_token => self.[:access_token] }.merge( || {})) end |
#remove(id, options = {}) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/fgraph/client.rb', line 88 def remove(id, ={}) FGraph.remove(id, { :access_token => self.[:access_token] }.merge( || {})) end |
#search(query, options = {}) ⇒ Object
95 96 97 |
# File 'lib/fgraph/client.rb', line 95 def search(query, ={}) FGraph.search(query, ) end |
#update_options(options = {}) ⇒ Object
49 50 51 |
# File 'lib/fgraph/client.rb', line 49 def (={}) @options.merge!() end |