Class: FacebookGraphAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_graph_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token, debug = false) ⇒ FacebookGraphAPI

Returns a new instance of FacebookGraphAPI.



10
11
12
13
14
15
16
17
# File 'lib/facebook_graph_api.rb', line 10

def initialize(access_token, debug = false)
  @debug = debug
  @graph_url = 'https://graph.facebook.com'
  @access_token = access_token
  if @access_token.blank? && !Rails.nil?
    Rails.logger.info 'An access token is required for this class to work'
  end
end

Instance Method Details

#delete_status(status_id) ⇒ Object

Delete a status

status_id: the facebook id of the status to delete



77
78
79
# File 'lib/facebook_graph_api.rb', line 77

def delete_status(status_id)
  delete_request('/' + status_id)
end

#get_account_infoObject

Get the basic account information from facebook



20
21
22
# File 'lib/facebook_graph_api.rb', line 20

def 
  @account_info ||= get_request('/me')
end

#get_feedObject

Get the users feed



25
26
27
28
29
30
31
32
33
# File 'lib/facebook_graph_api.rb', line 25

def get_feed
  @feed ||= get_request('/me/feed')
  # I have no idea why sometimes it uses the data index and sometimes it doesn't....
  begin
    return @feed['data']
  rescue
    return @feed
  end
end

#get_fql(fql) ⇒ Object

Get any fql request Fql cannot be cached since it might be used more than once to gather different data without creating a new facebook api class



104
105
106
# File 'lib/facebook_graph_api.rb', line 104

def get_fql(fql)
  get_request('/fql?q=' + CGI.escape(fql))['data']
end

#get_insightsObject

Get the insights of a page (**this doesn’t work for profiles**)



93
94
95
96
97
98
99
100
# File 'lib/facebook_graph_api.rb', line 93

def get_insights
  @insights ||= get_request('/me/insights')
  begin
    return @insights['data']
  rescue
    return @insights
  end
end

#get_pagesObject

Get pages associated with the account



109
110
111
# File 'lib/facebook_graph_api.rb', line 109

def get_pages
  @page ||= get_request('/me/accounts')['data']
end

#get_statusesObject

Get statuses from facebook page/account



82
83
84
85
86
87
88
89
90
# File 'lib/facebook_graph_api.rb', line 82

def get_statuses
  @statuses ||= get_request('/me/statuses')
  # I have no idea why sometimes it uses the data index and sometimes it doesn't....
  begin
    return @statuses['data']
  rescue
    return @statuses
  end
end

#post_link(link, message) ⇒ Object

Post a clickable link to a users feed (works for youtube videos as well)

link: the link (beginning with http:// or https://) that will be displayed in the users feed

message: the message to be posted with the link on the feed page



64
65
66
# File 'lib/facebook_graph_api.rb', line 64

def post_link(link, message)
  post_request('/me/feed', {link: link, message: message})
end

#post_picture(picture_path, message, post_to_feed = true) ⇒ Object

Post a picture to the users wall picture_path: the actual file path to the image (no urls) message: the message to be attached to the image if any. Can be null or empty. post_to_feed: true (by default) if you want the picture to be posted to the users wall, false if you want it hidden. This isn’t 100% tested.



45
46
47
48
49
50
51
52
53
# File 'lib/facebook_graph_api.rb', line 45

def post_picture(picture_path, message, post_to_feed=true)
  File.open(picture_path.to_s.gsub('%20', '\ ').gsub('(', '\(').gsub(')', '\)'), 'rb') do |binary_image|
    if post_to_feed
      post_request('/me/photos', {source: binary_image, message: message})
    else
      post_request('/me/photos', {source: binary_image, message: message, no_story: 'true'})
    end
  end
end

#post_status(message) ⇒ Object

Post a message to the users wall message: text of the message to be posted. Links will not be converted to click able links. Use post_link to post a clickable link (including video)



37
38
39
# File 'lib/facebook_graph_api.rb', line 37

def post_status(message)
  post_request('/me/feed', {message: message})
end

#reply_to_status(status_id, message) ⇒ Object

Post a reply to a status that is already existing on facebook status_id: the facebook id of the status to reply to

message: the message to use in the reply



71
72
73
# File 'lib/facebook_graph_api.rb', line 71

def reply_to_status(status_id, message)
  post_request('/' + status_id + '/comments', {message: message})
end

#set_as_cover_image(picture_id) ⇒ Object

Set an already existing image to be the users cover image

picture_id: the facebook id of the image to use as the users cover image. This currently doesn’t allow for an offset, but this will be available in the next version.



57
58
59
# File 'lib/facebook_graph_api.rb', line 57

def set_as_cover_image(picture_id)
  post_request('/me', {cover: picture_id, no_feed_story: 'true'})
end