Class: SocialProfile::People::Facebook

Inherits:
SocialProfile::Person show all
Defined in:
lib/social_profile/people/facebook.rb

Constant Summary collapse

FRIENDS_FQL =
"SELECT friend_count FROM user WHERE uid=me()"
FIRST_POST_FQL =
"SELECT created_time FROM stream WHERE source_id = me() AND created_time < {date} limit 1"
LAST_POSTS_FIELDS =
[
  "comments.fields(created_time).limit(1).summary(true)", 
  "likes.limit(1).fields(id).summary(true)",
  "created_time",
  "shares"
]

Instance Attribute Summary

Attributes inherited from SocialProfile::Person

#access_token, #options, #uid

Instance Method Summary collapse

Methods inherited from SocialProfile::Person

#find_album, #find_or_create_album, get, #initialize, #share_photo!, #tag_object!

Constructor Details

This class inherits a constructor from SocialProfile::Person

Instance Method Details

#album!(options = {}) ⇒ Object

Create new album id



21
22
23
# File 'lib/social_profile/people/facebook.rb', line 21

def album!(options = {})
  user.album!(options)
end

#fetch_album(album_id) ⇒ Object

Find album by id



16
17
18
# File 'lib/social_profile/people/facebook.rb', line 16

def fetch_album(album_id)
  ::FbGraph::Album.fetch(album_id, :access_token => access_token)
end

#fetch_friends_countObject



37
38
39
40
41
42
43
44
# File 'lib/social_profile/people/facebook.rb', line 37

def fetch_friends_count
  response = FbGraph::Query.new(FRIENDS_FQL).fetch(:access_token => access_token)

  response = response.first if response.is_a?(Array)
  return nil if response.is_a?(Hash) && response["friend_count"].blank?

  response["friend_count"].to_i
end

#first_post_exists?(year) ⇒ Boolean

Check if exists any post before current year

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/social_profile/people/facebook.rb', line 48

def first_post_exists?(year)
  timestamp = Time.new(year, 1, 1).utc.to_i

  _sql = FIRST_POST_FQL.gsub('{date}', timestamp.to_s)
  response = FbGraph::Query.new(_sql).fetch(:access_token => access_token)

  response = response.first if response.is_a?(Array)
  return nil if response.nil? || (response.is_a?(Hash) && response["created_time"].blank?)

  response["created_time"].to_i
end

#followers(options = {}) ⇒ Object

Get all followers list



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/social_profile/people/facebook.rb', line 100

def followers(options={})
  limit = options[:limit] || 5000
  fetch_all = options[:fetch_all] || false
  iteration = 0

  _followers = collection = user.subscribers(:limit => limit)
  max_iteration = _followers.total_count / limit + 1

  if fetch_all
    while iteration < max_iteration
      iteration += 1
      collection = collection.next
      _followers += collection
    end
  end

  _followers
end

#followers_countObject

Get followers count



33
34
35
# File 'lib/social_profile/people/facebook.rb', line 33

def followers_count
  @followers_count ||= followers(:limit => 1).total_count
end

#friends(options = {}) ⇒ Object

Get all friends list



93
94
95
96
# File 'lib/social_profile/people/facebook.rb', line 93

def friends(options={})
  limit = options[:limit] || 5000
  user.friends(:limit => limit)
end

#friends_countObject

Get friends count



27
28
29
# File 'lib/social_profile/people/facebook.rb', line 27

def friends_count
  @friends_count ||= fetch_friends_count
end

#last_post_by_days(days, options = {}) ⇒ Object

Get last post by days from feed with comments, shares and likes counters



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/social_profile/people/facebook.rb', line 70

def last_post_by_days(days, options={})
  date = (options[:date_end] || Time.now) - days.days
  limit = options[:limit] || 100
  max_iteration = options[:max_iteration] || 100
  iteration = 0

  posts = collection = last_posts(limit, options)
  return [] if posts.blank?
  
  last_created_time = posts.last.created_time

  while last_created_time > date && !last_created_time.blank? && iteration < max_iteration
    iteration += 1
    collection = collection.next
    posts += collection
    last_created_time = posts.last.created_time
  end

  posts.select { |p| p.created_time > date }
end

#last_posts(limit, options = {}) ⇒ Object

Get last limited posts from feed with comments, shares and likes counters



62
63
64
65
66
# File 'lib/social_profile/people/facebook.rb', line 62

def last_posts(limit, options = {})
  fields = options[:fields] || LAST_POSTS_FIELDS

  user.feed(:fields => fields.join(","), :limit => limit)        
end