Class: Fobos::GraphAPI::Pages

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/fobos/graph_api/pages.rb

Constant Summary collapse

FB_URI =

Store Facebook default URL

'https://www.facebook.com'
GRAPH_URI =

Store Facebook Graph API URL

'https://graph.facebook.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Pages

Need access token for making calls.



18
19
20
# File 'lib/fobos/graph_api/pages.rb', line 18

def initialize(access_token)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

You can get access token with Fobos::GraphAPI::Oauth.



10
11
12
# File 'lib/fobos/graph_api/pages.rb', line 10

def access_token
  @access_token
end

Instance Method Details

#get_accountsObject

Return list of user’s accounts.

Use access_token from Fobos::GraphAPI::Pages.new



25
26
27
28
# File 'lib/fobos/graph_api/pages.rb', line 25

def get_accounts
  user = Fobos::GraphAPI::Users.new(@access_token)
  user.get_request(fields: 'accounts')
end

#get_feed(page_id, options = {}) ⇒ Object

Return feed list of page.

Use access_token from Fobos::GraphAPI::Pages.new



51
52
53
54
55
56
57
58
# File 'lib/fobos/graph_api/pages.rb', line 51

def get_feed(page_id, options = {})
  options_part = ''
  options_part = options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?

  query = GRAPH_URI.to_s + "/#{page_id}" + "/feed?#{options_part}&access_token=#{@access_token}"

  self.class.get(query)
end

#post_to_page(page_id, page_access_token, options = {}) ⇒ Object

Publishing post to the page wall.

If you call it with USER_ACCESS_TOKEN it will publish post from user!

If you want publish something from Page you need call it with PAGE_ACCESS_TOKEN. You can get it from list of user’s account returned by Fobos::GraphAPI::Pages.get_accounts.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fobos/graph_api/pages.rb', line 35

def post_to_page(page_id, page_access_token, options = {})
  options_part = ''
  options_part = options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?

  query = GRAPH_URI.to_s + "/#{page_id}" + "/feed?#{options_part}&access_token=#{page_access_token}"

  encoded_url = URI.encode(query)

  puts encoded_url

  self.class.post(URI.parse(encoded_url))
end