Class: Faceoff::User

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

Constant Summary collapse

IM_SERVICES =
['AIM', 'Yahoo', 'Skype', 'Google Talk', 'Windows Live',
'Gadu-Gadu', 'QQ', 'ICQ', 'Yahoo Japan', 'NateOn']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name) ⇒ User

Returns a new instance of User.



150
151
152
153
154
155
156
157
158
159
# File 'lib/faceoff/user.rb', line 150

def initialize id, name
  @fid  = id
  @name = name

  @photo   = nil
  @phones  = {}
  @emails  = []
  @aliases = {}
  @address = {}
end

Instance Attribute Details

#addressObject

Current address.



147
148
149
# File 'lib/faceoff/user.rb', line 147

def address
  @address
end

#aliasesObject

Instant message aliases.



141
142
143
# File 'lib/faceoff/user.rb', line 141

def aliases
  @aliases
end

#birthdayObject

User’s birthday.



132
133
134
# File 'lib/faceoff/user.rb', line 132

def birthday
  @birthday
end

#emailsObject

All emails.



138
139
140
# File 'lib/faceoff/user.rb', line 138

def emails
  @emails
end

#fidObject

Facebook user id.



126
127
128
# File 'lib/faceoff/user.rb', line 126

def fid
  @fid
end

#nameObject

User’s name.



129
130
131
# File 'lib/faceoff/user.rb', line 129

def name
  @name
end

#phonesObject

Phone numbers.



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

def phones
  @phones
end

#photoObject

Facebook profile image.



135
136
137
# File 'lib/faceoff/user.rb', line 135

def photo
  @photo
end

Class Method Details

.fattr(doc, attrib, options = {}) ⇒ Object

Get a facebook attribute from a given Nokogiri::HTML::Document.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/faceoff/user.rb', line 101

def self.fattr doc, attrib, options={}
  resp  = []

  nodes = doc.search("th[@class='label'][text()='#{attrib}:']")

  return resp if nodes.empty?

  nodes.first.next.children.each do |node|
    text = node.name == "img" ? node['src'] : node.text.strip
    next if text.empty?

    if options.empty?
      resp << text
    else
      options.each do |key, matcher|
        resp << text if node[key] =~ matcher
      end
    end
  end

  resp
end

.retrieve(faceoff, user_id) ⇒ Object

Retrieve a single user based on the user’s id or alias:

User.retrieve f, 12345
User.retrieve f, 'bob.smith'


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/faceoff/user.rb', line 36

def self.retrieve faceoff, user_id
  agent = faceoff.agent

  path = if user_id =~ /^\d+$/
           "/profile.php?id=#{user_id}&v=info&ref=sgm"
         else
           "/#{user_id}?v=info"
         end

  page = agent.get path

  pagelets = Pagelet.parse page.body

  name = pagelets[:top_bar].css("h1#profile_name").first.text
  id   = $1 if
    pagelets[:top_bar].css("a#top_bar_pic").first['href'] =~
    %r{/profile\.php\?id=(\d+)}


  user = User.new id, name

  details = pagelets[:tab_content]

  birthday = fattr(details, 'Birthday')[0].strip
  user.birthday = Time.parse birthday if birthday && !birthday.empty?

  user.emails = fattr details, 'Email'

  user.phones['mobile'] =
    fattr(details, 'Mobile Number').first.gsub(/[^\da-z]/i, '') rescue nil

  user.phones['main'] =
    fattr(details, 'Phone').first.gsub(/[^\da-z]/i, '') rescue nil


  user.photo = pagelets[:profile_photo].css("img#profile_pic").first['src']

  user.address = {}
  user.address[:street] = fattr(details, "Address", :href => /&a2=/).first

  user.address[:city], user.address[:state] =
    fattr(details, "Address", :href => /&c2=/).first.to_s.split(", ")

  user.address[:state], user.address[:country] =
    [nil, user.address[:state]] if user.address[:state].to_s.length > 2

  user.address[:zip] = fattr(details, "Address", :href => /&z2=/).first


  IM_SERVICES.each do |im|
    im_alias = fattr(details, im).first
    next unless im_alias
    user.aliases[im] = im_alias
  end

  user.aliases['Facebook'] = $1 if
    fattr(details, 'Facebook Profile').first =~ /([^\/]+)$/

  user
end

.retrieve_all(faceoff, options = {}) ⇒ Object

Retrieve all friends of the given faceoff user.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/faceoff/user.rb', line 12

def self.retrieve_all faceoff, options={}
  agent = faceoff.agent
  limit = options[:limit]
  start = options[:start] || 0

  page = agent.get "/friends/ajax/superfriends.php?"+
    "__a=1&filter=afp&type=afp&value&is_pagelet=false&offset=0"

  user_ids = JSON.parse $1 if page.body =~ %r{"members":(\[[^\]]+\])}
  limit ||= user_ids.length

  user_ids[start, limit].map do |uid|
    user = retrieve faceoff, uid
    yield user if block_given?
    user
  end
end

Instance Method Details

#save!(target = "./Contacts", vcard = nil) ⇒ Object

Saves the user as a vcard to the provided file path.



165
166
167
168
169
170
171
# File 'lib/faceoff/user.rb', line 165

def save! target="./Contacts", vcard=nil
  vcard = to_vcard vcard

  Faceoff.safe_save(File.join(target, "#{@name}.vcf")) do |file|
    file.write vcard
  end
end

#to_vcard(vcard = nil) ⇒ Object

Returns a Vpip::Vcard object.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/faceoff/user.rb', line 177

def to_vcard vcard=nil
  vcard ||= Vpim::Vcard.create

  vcard.make do |maker|

    maker.name{|n| n.fullname = @name }

    maker.add_field Vpim::DirectoryInfo::Field.create('BDAY',
      @birthday.strftime("%Y-%m-%d"))

    maker.add_addr do |addr|
      addr.region   = address[:state]
      addr.locality = address[:city]
      addr.street   = address[:street]
      addr.country  = address[:country]
      addr.postalcode = address[:zip]
    end

    emails.each{|email| maker.add_email email }

    phones.each do |type, number|
      next unless number
      maker.add_tel(number){|tel| tel.location = [type] }
    end

    aliases.each do |name, value|
      maker.add_field Vpim::DirectoryInfo::Field.create("X-#{name}", value)
    end

    maker.add_photo do |photo|
      photo.image = Faceoff.download @photo
      photo.type  = File.extname(@photo)[1..-1]
    end if @photo
  end

  vcard
end