class Hero

Attributes

abilities[RW]
biography[RW]
name[RW]
overview[RW]
path[RW]
quote[RW]
role[RW]
url[RW]

Public Class Methods

all() click to toggle source
# File lib/ow_heroes_roster/hero.rb, line 81
def self.all
  @@all
end
find_by_name(name) click to toggle source
# File lib/ow_heroes_roster/hero.rb, line 17
def self.find_by_name(name)
  self.all.find{|hero| hero.name == name}
end
new(name, path) click to toggle source
# File lib/ow_heroes_roster/hero.rb, line 11
def initialize(name, path)
  @name = name
  @url = "https://playoverwatch.com#{path}"
  @@all << self
end

Public Instance Methods

display_information() click to toggle source

display hero details

# File lib/ow_heroes_roster/hero.rb, line 55
def display_information
  get_details(url)
  puts "\n--------------------------------".colorize(:yellow)
  print "\n**".colorize(:magenta)
  print " #{self.name.upcase}".colorize(:white)
  puts  " **".colorize(:magenta)
  puts "\n--------------------------------".colorize(:yellow)
  puts "\nROLE: #{self.role}".colorize(:yellow)
  puts "--------------------------------".colorize(:magenta)
  puts "OVERVIEW:".colorize(:yellow)
  puts "#{self.overview}".colorize(:white)
  puts "--------------------------------".colorize(:magenta)
  puts "ABILITIES:\n".colorize(:yellow)
  self.abilities.each do |ability|
    puts "*#{ability[:ability]}: #{ability[:description]}".colorize(:white)
  end
  puts "--------------------------------".colorize(:magenta)
  puts "BIOGRAPHY:\n".colorize(:yellow)
  self.biography.each_value {|value| puts "\t*#{value}".colorize(:white) }
  puts "\n--------------------------------".colorize(:magenta)
  if self.quote != ""
    puts "QUOTE: #{self.quote}".colorize(:yellow)
    puts "--------------------------------".colorize(:magenta)
  end
end
get_details(index_url) click to toggle source

get details by scraping hero page

# File lib/ow_heroes_roster/hero.rb, line 22
def get_details(index_url)
  html = open(index_url)
  doc = Nokogiri::HTML(html)

  #role
  @role = doc.css(".hero-detail-role-name").text

  #overview
  @overview = doc.css("#overview .hero-detail-description").text

  #abilities
  @abilities = []
  doc.css(".hero-ability").each do |ability|
    hero_ability = ability.css(".hero-ability-descriptor .h5").text
    hero_description = ability.css(".hero-ability-descriptor p").text
    @abilities << {ability: hero_ability, description: hero_description}
  end

  #biography
  @biography = {}
  doc.css("#story .hero-bio").each do |info|
    hero_real_name = info.css(".name .hero-bio-copy").text
    hero_occupation = info.css(".occupation .hero-bio-copy").text
    hero_base = info.css(".base .hero-bio-copy").text
    hero_affiliation = info.css(".affiliation").text
    @biography = {real_name: hero_real_name, occupation: hero_occupation, base: hero_base, affiliation: hero_affiliation}
  end

  #quote
  @quote = doc.css("#story p.h4").text
end