Class: RFb::User

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

Constant Summary collapse

FIELDS =
[:first_name, :name, :current_location, :has_added_app, :locale, :pic, :profile_url, :status, :timezone, :username]
EXCLUDED_FIELDS =
[:current_location]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ User

Returns a new instance of User.



14
15
16
17
18
19
20
21
22
# File 'lib/r_fb/user.rb', line 14

def initialize(attrs={})
  attrs.map do |attr_name, attr_value|
    instance_variable_set("@#{attr_name}", attr_value)
  end
  @user = attrs[:user]
  @fb_id = attrs[:fb_id]
  @session = attrs[:session]
  Session.session_key = @session unless @session.nil?
end

Instance Attribute Details

#fb_idObject

Returns the value of attribute fb_id.



7
8
9
# File 'lib/r_fb/user.rb', line 7

def fb_id
  @fb_id
end

#sessionObject

Returns the value of attribute session.



9
10
11
# File 'lib/r_fb/user.rb', line 9

def session
  @session
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/r_fb/user.rb', line 8

def user
  @user
end

Class Method Details

.find_or_create(attrs = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/r_fb/user.rb', line 31

def self.find_or_create(attrs={})
  # calls DataMapper model class User
  user = ::User.first(:fb_id => attrs[:fb_id])
  user = ::User.create(:fb_id => attrs[:fb_id]) if user.nil?
  u = User.new(attrs.merge(:user => user)).select_all
end

.first(attrs = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/r_fb/user.rb', line 24

def self.first(attrs={})
  raise "RFb::User.first called!"
  user = ::User.first(:fb_id => attrs[:fb_id])
  u = User.new(attrs.merge(:user => user))
  u.select_all
end

Instance Method Details

#friendsObject

use FIELDS.join(“, ”) instead of uid to select other datas



39
40
41
# File 'lib/r_fb/user.rb', line 39

def friends
  Fql.query("SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=#{self.fb_id}) AND has_added_app = 1")
end

#friends_nonapp(limit = true) ⇒ Object



43
44
45
# File 'lib/r_fb/user.rb', line 43

def friends_nonapp(limit=true)
  Fql.query("SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=#{self.fb_id}) AND has_added_app = 0#{"  LIMIT 30" if limit}")
end

#is_app_user?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/r_fb/user.rb', line 47

def is_app_user?
  has_added_app
end

#select_allObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/r_fb/user.rb', line 51

def select_all
  fb_uids = self.fb_id.to_i
  raise "Facebook user id is 0/nil for user: #{self.inspect}" if fb_uids == 0
  parsed_response = RFb.request({
                      method: "facebook.Users.getInfo", 
                      uids: [fb_uids].to_s, 
                      fields: FIELDS.map(&:to_s).to_s
                    })
  # puts "parsed response: [uids: #{[self.fb_id.to_i]} ] "
  # puts parsed_response.inspect
  FIELDS.map do |field|
    value = parsed_response[field.to_s]
    instance_variable_set("@#{field}", value)
    user.send("#{field}=",  value) unless EXCLUDED_FIELDS.include?(field)
  end
  #user.update_attributes(name: "Francesco")

  unless user.save
    raise "RFb::User#select_all failed, failed to #user.save, user: #{user.errors.inspect}"
  end
  
  self
end