Class: Facebooker::User

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/facebooker/models/user.rb,
lib/facebooker/adapters/bebo_adapter.rb

Overview

Holds attributes and behavior for a Facebook User

Defined Under Namespace

Classes: Status

Constant Summary collapse

FIELDS =
[:status, :political, :pic_small, :name, :quotes, :is_app_user, :tv, :profile_update_time, :meeting_sex, :hs_info, :timezone, :relationship_status, :hometown_location, :about_me, :wall_count, :significant_other_id, :pic_big, :music, :uid, :work_history, :sex, :religion, :notes_count, :activities, :pic_square, :movies, :has_added_app, :education_history, :birthday, :first_name, :meeting_for, :last_name, :interests, :current_location, :pic, :books, :affiliations]
STANDARD_FIELDS =
[:uid, :first_name, :last_name, :name, :timezone, :birthday, :sex, :affiliations, :locale, :profile_url]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#anon=, included, #populate_from_hash!, #populated?

Constructor Details

#initialize(*args) ⇒ User

Can pass in these two forms: id, session, (optional) attribute_hash attribute_hash



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/facebooker/models/user.rb', line 29

def initialize(*args)
  if (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args.size==1
    @id=Integer(args.shift)
    @session = Session.current
  elsif (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args[1].kind_of?(Session)
    @id = Integer(args.shift)
    @session = args.shift
  end
  if args.last.kind_of?(Hash)
    populate_from_hash!(args.pop)
  end     
end

Instance Attribute Details

#affiliationsObject (readonly)

Returns the value of attribute affiliations.



17
18
19
# File 'lib/facebooker/models/user.rb', line 17

def affiliations
  @affiliations
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/facebooker/models/user.rb', line 15

def id
  @id
end

#sessionObject

Returns the value of attribute session.



15
16
17
# File 'lib/facebooker/models/user.rb', line 15

def session
  @session
end

Class Method Details

.cast_to_facebook_id(object) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/facebooker/models/user.rb', line 325

def self.cast_to_facebook_id(object)
  if object.respond_to?(:facebook_id)
    object.facebook_id
  else
    object
  end
end

.hash_email(email) ⇒ Object



318
319
320
321
322
323
# File 'lib/facebooker/models/user.rb', line 318

def self.hash_email(email)
  email = email.downcase.strip
  crc=Zlib.crc32(email)
  md5=Digest::MD5.hexdigest(email)
  "#{crc}_#{md5}"
end

.register(users) ⇒ Object

register a user with Facebook users should be a hast with at least an :email field you can optionally provide an :account_id field as well



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/facebooker/models/user.rb', line 294

def self.register(users)
  user_map={}
  users=users.map do |h|
    returning h.dup do |d|
      if email=d.delete(:email)
        hash = hash_email(email)
        user_map[hash]=h
        d[:email_hash]=hash
      end
    end
  end
  Facebooker::Session.create.post("facebook.connect.registerUsers",:accounts=>users.to_json) do |ret|
    ret.each do |hash|
      user_map.delete(hash)
    end
    unless user_map.empty?
      e=Facebooker::Session::UserRegistrationFailed.new
      e.failed_users = user_map.values
      raise e
    end
    ret
  end
end

.standard_fields(fields = []) ⇒ Object



341
342
343
# File 'lib/facebooker/models/user.rb', line 341

def self.standard_fields(fields = [])
  valid_fields(fields,STANDARD_FIELDS)
end

.user_fields(fields = []) ⇒ Object



337
338
339
# File 'lib/facebooker/models/user.rb', line 337

def self.user_fields(fields = [])
  valid_fields(fields)
end

Instance Method Details

#==(other_user) ⇒ Object

Two Facebooker::User objects should be considered equal if their Facebook ids are equal



285
286
287
# File 'lib/facebooker/models/user.rb', line 285

def ==(other_user)
  id == other_user.id
end

#albumsObject



156
157
158
159
160
161
162
# File 'lib/facebooker/models/user.rb', line 156

def albums
  @albums ||= session.post('facebook.photos.getAlbums', :uid => self.id) do |response|
    response.map do |hash|
      Album.from_hash(hash)
    end
  end
end

#cast_to_friend_list_id(flid) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/facebooker/models/user.rb', line 69

def cast_to_friend_list_id(flid)
  case flid
  when String
    list=friend_lists.detect {|f| f.name==flid}
    raise Facebooker::Session::InvalidFriendList unless list
    list.flid
  when FriendList
    flid.flid
  else
    flid
  end
end

#create_album(params) ⇒ Object



164
165
166
# File 'lib/facebooker/models/user.rb', line 164

def create_album(params)
  @album = session.post('facebook.photos.createAlbum', params) {|response| Album.from_hash(response)}
end

#events(params = {}) ⇒ Object

Returns a user’s events, params correspond to API call parameters (except UID): wiki.developers.facebook.com/index.php/Events.get E.g:

@user.events(:start_time => Time.now, :end_time => 1.month.from_now)
# => Returns events betwen now and a month from now


47
48
49
50
51
52
53
54
55
56
# File 'lib/facebooker/models/user.rb', line 47

def events(params={})
  @events ||= {}
  [:start_time,:end_time].compact.each do |key|
    params[key] = params[key].to_i
  end
#      puts @events[params.to_s].nil?
  @events[params.to_s] ||= @session.post('facebook.events.get', {:uid => self.id}.merge(params)).map do |event|
    Event.from_hash(event)
  end
end

#facebook_idObject



333
334
335
# File 'lib/facebooker/models/user.rb', line 333

def facebook_id
  @id
end

#friend_listsObject



95
96
97
98
99
100
101
# File 'lib/facebooker/models/user.rb', line 95

def friend_lists    
  @friend_lists ||= @session.post('facebook.friends.getLists').map do |hash|
    friend_list = FriendList.from_hash(hash)                               
    friend_list.session = session                                          
    friend_list                                                            
  end                                                                      
end

#friends(flid = nil) ⇒ Object

Retrieve friends



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/facebooker/models/user.rb', line 83

def friends(flid = nil)
 	@friends_hash ||= {}
 	flid=cast_to_friend_list_id(flid)
  
 	#use __blank instead of nil so that this is cached
 	cache_key = flid||"__blank"
 	@friends_hash[cache_key] ||= @session.post('facebook.friends.get', (flid.nil? ? {} : {:flid => flid})).map do |uid|
      User.new(uid, @session)
  end
  @friends_hash[cache_key]
end

#friends!(*fields) ⇒ Object

Retrieve friends with user info populated Subsequent calls will be retrieved from memory. Optional: list of fields to retrieve as symbols



106
107
108
109
110
# File 'lib/facebooker/models/user.rb', line 106

def friends!(*fields)
  @friends ||= session.post('facebook.users.getInfo', :fields => collect(fields), :uids => friends.map{|f| f.id}.join(',')).map do |hash|  
    User.new(hash['uid'], session, hash)
  end
end

#friends=(list_of_friends, flid = nil) ⇒ Object

Set the list of friends, given an array of User objects. If the list has been retrieved previously, will not set



60
61
62
63
64
65
66
67
# File 'lib/facebooker/models/user.rb', line 60

def friends=(list_of_friends,flid=nil)
  @friends_hash ||= {}
 	flid=cast_to_friend_list_id(flid)
 	#use __blank instead of nil so that this is cached
 	cache_key = flid||"__blank"
 	
  @friends_hash[cache_key] ||= list_of_friends
end

#friends_with?(user_or_id) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/facebooker/models/user.rb', line 121

def friends_with?(user_or_id)
  friends.map{|f| f.to_i}.include?(user_or_id.to_i)  
end

#friends_with_this_appObject



125
126
127
128
129
# File 'lib/facebooker/models/user.rb', line 125

def friends_with_this_app
  @friends_with_this_app ||= session.post('facebook.friends.getAppUsers').map do |uid|
    User.new(uid, session)
  end
end

#get_cookies(name = nil) ⇒ Object

Convenience method to get cookies for the current user



269
270
271
# File 'lib/facebooker/models/user.rb', line 269

def get_cookies(name=nil)
  session.data.get_cookies(@id, name)
end

#get_profile_infoObject



221
222
223
# File 'lib/facebooker/models/user.rb', line 221

def get_profile_info
  session.post('facebook.profile.getInfo', :uid => @id)
end

#groups(gids = []) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/facebooker/models/user.rb', line 131

def groups(gids = [])
  args = gids.empty? ? {} : {:gids => gids}
  @groups ||= session.post('facebook.groups.get', args).map do |hash|
    group = Group.from_hash(hash)
    group.session = session
    group
  end
end

#has_permission?(ext_perm) ⇒ Boolean

Checks to see if the user has enabled the given extended permission

Returns:

  • (Boolean)


251
252
253
# File 'lib/facebooker/models/user.rb', line 251

def has_permission?(ext_perm) # ext_perm = email, offline_access, status_update, photo_upload, create_listing, create_event, rsvp_event, sms
  session.post('facebook.users.hasAppPermission',:ext_perm=>ext_perm) == "1"
end

#mobile_fbml=(markup) ⇒ Object

Set the mobile profile FBML



190
191
192
# File 'lib/facebooker/models/user.rb', line 190

def mobile_fbml=(markup)
  set_profile_fbml(nil, markup, nil)
end

#notificationsObject



140
141
142
# File 'lib/facebooker/models/user.rb', line 140

def notifications
  @notifications ||= Notifications.from_hash(session.post('facebook.notifications.get'))
end

#populate(*fields) ⇒ Object

Retrieve profile data for logged in user Optional: list of fields to retrieve as symbols



115
116
117
118
119
# File 'lib/facebooker/models/user.rb', line 115

def populate(*fields)
  session.post('facebook.users.getInfo', :fields => collect(fields), :uids => id) do |response|
    populate_from_hash!(response.first)
  end
end

#profile_action=(markup) ⇒ Object



194
195
196
# File 'lib/facebooker/models/user.rb', line 194

def profile_action=(markup)
  set_profile_fbml(nil, nil, markup)
end

#profile_fbmlObject



176
177
178
# File 'lib/facebooker/models/user.rb', line 176

def profile_fbml
  session.post('facebook.profile.getFBML', :uid => @id)  
end

#profile_fbml=(markup) ⇒ Object

Set the profile FBML for this user

This does not set profile actions, that should be done with profile_action=



184
185
186
# File 'lib/facebooker/models/user.rb', line 184

def profile_fbml=(markup)
  set_profile_fbml(markup, nil, nil)
end

#profile_main=(markup) ⇒ Object



198
199
200
# File 'lib/facebooker/models/user.rb', line 198

def profile_main=(markup)
 set_profile_fbml(nil,nil,nil,markup)
end

#profile_photosObject



168
169
170
# File 'lib/facebooker/models/user.rb', line 168

def profile_photos
  session.get_photos(nil, nil, profile_pic_album_id)
end

#publish_action(action) ⇒ Object



148
149
150
# File 'lib/facebooker/models/user.rb', line 148

def publish_action(action)
  publish(action)
end

#publish_story(story) ⇒ Object



144
145
146
# File 'lib/facebooker/models/user.rb', line 144

def publish_story(story)
  publish(story)
end

#publish_templatized_action(action) ⇒ Object



152
153
154
# File 'lib/facebooker/models/user.rb', line 152

def publish_templatized_action(action)
  publish(action)
end

#send_email(subject, text = nil, fbml = nil) ⇒ Object

Convenience method to send email to the current user



257
258
259
# File 'lib/facebooker/models/user.rb', line 257

def send_email(subject, text=nil, fbml=nil)
  session.send_email([@id], subject, text, fbml)
end

Convenience method to set cookie for the current user



263
264
265
# File 'lib/facebooker/models/user.rb', line 263

def set_cookie(name, value, expires=nil, path=nil)
  session.data.set_cookie(@id, name, value, expires, path)
end

#set_profile_fbml_with_bebo_adapter(profile_fbml, mobile_fbml, profile_action_fbml, profile_main = nil) ⇒ Object Also known as: set_profile_fbml



39
40
41
42
43
44
45
# File 'lib/facebooker/adapters/bebo_adapter.rb', line 39

def set_profile_fbml_with_bebo_adapter(profile_fbml, mobile_fbml, profile_action_fbml, profile_main = nil)
  if(Facebooker.is_for?(:bebo))
    self.session.post('facebook.profile.setFBML', :uid => @id, :markup => profile_fbml)
  else
    set_profile_fbml_without_bebo_adapter(profile_fbml,mobile_fbml, profile_action_fbml, profile_main)
  end
end

#set_profile_info(title, info_fields, format = :text) ⇒ Object

** NEW PROFILE DESIGN *** Set a info section for this user

Note: using set_profile_info as I feel using user.set_info could be confused with the user.getInfo facebook method.

Also, I feel it fits in line with user.set_profile_fbml.


216
217
218
219
# File 'lib/facebooker/models/user.rb', line 216

def set_profile_info(title, info_fields, format = :text)
  session.post('facebook.profile.setInfo', :title => title, :uid => @id, 
    :type => format.to_s == "text" ? 1 : 5, :info_fields => info_fields.to_json)
end

#set_status(message) ⇒ Object

Set the status for a user DOES NOT prepend “is” to the message

requires extended permission.



242
243
244
245
246
247
# File 'lib/facebooker/models/user.rb', line 242

def set_status(message)
  self.status=message
  session.post('facebook.users.setStatus',:status=>message,:status_includes_verb=>1) do |ret|
    ret
  end
end

#status=(message) ⇒ Object

This DOES NOT set the status of a user on Facebook Use the set_status method instead



228
229
230
231
232
233
234
235
# File 'lib/facebooker/models/user.rb', line 228

def status=(message)
  case message
  when String,Status
    @status = message
  when Hash
    @status = Status.from_hash(message)
  end
end

#to_iObject

Returns the user’s id as an integer



275
276
277
# File 'lib/facebooker/models/user.rb', line 275

def to_i
  id
end

#to_sObject



279
280
281
# File 'lib/facebooker/models/user.rb', line 279

def to_s
  id.to_s
end

#upload_photo(multipart_post_file) ⇒ Object



172
173
174
# File 'lib/facebooker/models/user.rb', line 172

def upload_photo(multipart_post_file)
  Photo.from_hash(session.post_file('facebook.photos.upload', {nil => multipart_post_file}))
end