Class: DelegateTracker::Candidate

Inherits:
Object
  • Object
show all
Defined in:
lib/delegate_tracker/candidate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#delegates_earnedObject

Returns the value of attribute delegates_earned.



2
3
4
# File 'lib/delegate_tracker/candidate.rb', line 2

def delegates_earned
  @delegates_earned
end

#delegates_needed_for_nominationObject

Returns the value of attribute delegates_needed_for_nomination.



2
3
4
# File 'lib/delegate_tracker/candidate.rb', line 2

def delegates_needed_for_nomination
  @delegates_needed_for_nomination
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/delegate_tracker/candidate.rb', line 2

def name
  @name
end

#quoteObject

Returns the value of attribute quote.



2
3
4
# File 'lib/delegate_tracker/candidate.rb', line 2

def quote
  @quote
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/delegate_tracker/candidate.rb', line 2

def url
  @url
end

Class Method Details

.candidate_profile(candidate) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/delegate_tracker/candidate.rb', line 16

def self.candidate_profile(candidate)
  pronoun = candidate.name.include?("Hillary") ? "She" : "He"
  puts ""
  puts "#{candidate.name} has won #{candidate.delegates_earned} delegates so far."
  puts ""
  puts "#{pronoun} needs #{candidate.delegates_needed_for_nomination - candidate.delegates_earned} more delegates to win the nomination."
  puts ""
  puts "#{pronoun} is quoted to have said '#{candidate.quote}'"
  puts ""
end

.create_candidate(party, profile_number) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/delegate_tracker/candidate.rb', line 4

def self.create_candidate(party, profile_number)
  candidate = self.new
  candidate.url = Nokogiri::HTML(open(DelegateTracker::CandidateListScraper.new.get_candidate_url(party)[profile_number]))
  candidate.name = candidate.url.css("h1.is-emphasized").text.strip
  candidate.quote = candidate.url.css("p.quote-text").text.strip.tr('"', '')
  candidate.delegates_earned = strip_to_int(candidate.url.css("span.index-text"))
  candidate.delegates_needed_for_nomination = strip_to_int(candidate.url.css("span.needed-text"))
  candidate_profile(candidate)
end

.strip_to_int(data) ⇒ Object



13
14
15
# File 'lib/delegate_tracker/candidate.rb', line 13

def self.strip_to_int(data)
  data.text.strip.downcase.scan(/\d/).join('').to_i
end