Class: FGraph::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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={})
  @options = 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)
      options = args[1] || {}
      options[:type] = names[0]
      self.search(args[0], options)
    else
      super
  end
end

Instance Attribute Details

#client_idObject (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_secretObject (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_clientObject (readonly)

Returns the value of attribute oauth_client.



6
7
8
# File 'lib/fgraph/client.rb', line 6

def oauth_client
  @oauth_client
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/fgraph/client.rb', line 6

def options
  @options
end

Class Method Details

.instanceObject

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(options={})
  unless self.options[:app_access_token]
    self.options[:app_access_token] = self.oauth_app_access_token
  end
  FGraph.insights(self.options[:client_id], self.options[:app_access_token]['access_token'], options)
end

#me(*args) ⇒ Object



76
77
78
79
80
# File 'lib/fgraph/client.rb', line 76

def me(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args << {:access_token => self.options[:access_token]}.merge(options)
  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.options[:client_id], self.options[:client_secret],
    :redirect_uri => redirect_uri, :code => code)
end

#oauth_app_access_tokenObject



62
63
64
# File 'lib/fgraph/client.rb', line 62

def oauth_app_access_token
  FGraph.oauth_app_access_token(self.options[:client_id], self.options[:client_secret])
end

#oauth_authorize_url(redirect_uri, options = {}) ⇒ Object



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

def oauth_authorize_url(redirect_uri, options={})
  FGraph.oauth_authorize_url(self.options[:client_id], redirect_uri, options)
end

#object(id, options = {}) ⇒ Object



66
67
68
# File 'lib/fgraph/client.rb', line 66

def object(id, options={})
  FGraph.object(id, {:access_token => self.options[:access_token]}.merge(options || {}))
end

#objects(*args) ⇒ Object



70
71
72
73
74
# File 'lib/fgraph/client.rb', line 70

def objects(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args << {:access_token => self.options[:access_token]}.merge(options)
  FGraph.objects(*args)
end

#publish(id, options = {}) ⇒ Object



82
83
84
85
86
# File 'lib/fgraph/client.rb', line 82

def publish(id, options={})
  FGraph.publish(id, {
    :access_token => self.options[:access_token]
  }.merge(options || {}))
end

#remove(id, options = {}) ⇒ Object



88
89
90
91
92
93
# File 'lib/fgraph/client.rb', line 88

def remove(id, options={})
  FGraph.remove(id, {
    :access_token => self.options[:access_token]
  }.merge(options || {}))
  
end

#search(query, options = {}) ⇒ Object



95
96
97
# File 'lib/fgraph/client.rb', line 95

def search(query, options={})
  FGraph.search(query, options)
end

#update_options(options = {}) ⇒ Object



49
50
51
# File 'lib/fgraph/client.rb', line 49

def update_options(options={})
  @options.merge!(options)
end