Class: OKCupid::Profile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Profile

Returns a new instance of Profile.



89
90
91
92
93
# File 'lib/lonely_coder/profile.rb', line 89

def initialize(attributes)
  attributes.each do |attr,val|
    self.send("#{attr}=", val)
  end
end

Instance Attribute Details

#ageObject

Returns the value of attribute age.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def age
  @age
end

#body_typeObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def body_type
  @body_type
end

#dietObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def diet
  @diet
end

#drinksObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def drinks
  @drinks
end

#drugsObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def drugs
  @drugs
end

#educationObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def education
  @education
end

#enemyObject

Returns the value of attribute enemy.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def enemy
  @enemy
end

#ethnicityObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def ethnicity
  @ethnicity
end

#friendObject

Returns the value of attribute friend.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def friend
  @friend
end

#heightObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def height
  @height
end

#incomeObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def income
  @income
end

#jobObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def job
  @job
end

#last_onlineObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def last_online
  @last_online
end

#locationObject

Returns the value of attribute location.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def location
  @location
end

#matchObject

Returns the value of attribute match.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def match
  @match
end

#offspringObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def offspring
  @offspring
end

#orientationObject

Returns the value of attribute orientation.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def orientation
  @orientation
end

#petsObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def pets
  @pets
end

#profile_thumb_urlsObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def profile_thumb_urls
  @profile_thumb_urls
end

#religionObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def religion
  @religion
end

#sexObject

Returns the value of attribute sex.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def sex
  @sex
end

#signObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def sign
  @sign
end

#singleObject

Returns the value of attribute single.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def single
  @single
end

#small_avatar_urlObject

Returns the value of attribute small_avatar_url.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def small_avatar_url
  @small_avatar_url
end

#smokesObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def smokes
  @smokes
end

#speaksObject

extended profile details



13
14
15
# File 'lib/lonely_coder/profile.rb', line 13

def speaks
  @speaks
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/lonely_coder/profile.rb', line 9

def username
  @username
end

Class Method Details

.by_username(username, browser) ⇒ Object



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
# File 'lib/lonely_coder/profile.rb', line 46

def Profile.by_username(username, browser)
  html = browser.get("http://www.okcupid.com/profile/#{username}")
  
  percents = html.search('#percentages')
  match = percents.search('span.match').text.to_i
  friend = percents.search('span.friend').text.to_i
  enemy = percents.search('span.enemy').text.to_i
  
  basic = html.search('#aso_loc')
  age = basic.search('#ajax_age').text
  sex = basic.search('#ajax_gender').text
  orientation = basic.search('#ajax_orientation').text
  single = basic.search('#ajax_status').text
  location = basic.search('#ajax_location').text
  
  profile_thumb_urls = html.search('#profile_thumbs img').collect {|img| img.attribute('src').value}
  
  attributes = {
    username: username,
    match: match,
    friend: friend,
    enemy: enemy,
    age: age,
    sex: sex,
    orientation: orientation,
    location: location,
    single: single,
    profile_thumb_urls: profile_thumb_urls
  }
  
  details_div = html.search('#profile_details dl')
  
  details_div.each do |node|
    value = OKCupid.strip(node.search('dd').text)
    next if value == ''
    
    attr_name = node.search('dt').text.downcase.gsub(' ','_')
    attributes[attr_name] = value
  end
  
  self.new(attributes)
end

.from_search_result(html) ⇒ Object

Scraping is never pretty.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lonely_coder/profile.rb', line 19

def self.from_search_result(html)
  
  username = html.search('span.username').text
  age, sex, orientation, single = html.search('p.aso').text.split('/')
  
  percents = html.search('div.percentages')
  match = percents.search('p.match .percentage').text.to_i
  friend = percents.search('p.friend .percentage').text.to_i
  enemy = percents.search('p.enemy .percentage').text.to_i
  
  location = html.search('p.location').text
  small_avatar_url = html.search('a.user_image img').attribute('src').value
  
  OKCupid::Profile.new({
    username: username,
    age: OKCupid.strip(age),
    sex: OKCupid.strip(sex),
    orientation: OKCupid.strip(orientation),
    single: OKCupid.strip(single),
    match: match,
    friend: friend,
    enemy: enemy,
    location: location,
    small_avatar_url: small_avatar_url
  })
end

Instance Method Details

#==(other) ⇒ Object



95
96
97
# File 'lib/lonely_coder/profile.rb', line 95

def ==(other)
  self.username == other.username
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/lonely_coder/profile.rb', line 99

def eql?(other)
  self.username == other.username
end

#hashObject



103
104
105
106
107
108
109
# File 'lib/lonely_coder/profile.rb', line 103

def hash
  if self.username
    self.username.hash
  else
    super
  end
end