Class: Wikihow::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/wikihow/category.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category_hash) ⇒ Category

Returns a new instance of Category.



4
5
6
7
8
9
# File 'lib/wikihow/category.rb', line 4

def initialize(category_hash)
  self.title = category_hash[:title]
  self.url = category_hash[:url]
  self.topics = []
  self.class.all << self
end

Instance Attribute Details

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/wikihow/category.rb', line 2

def title
  @title
end

#topicsObject

Returns the value of attribute topics.



2
3
4
# File 'lib/wikihow/category.rb', line 2

def topics
  @topics
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/wikihow/category.rb', line 2

def url
  @url
end

Class Method Details

.allObject



15
16
17
# File 'lib/wikihow/category.rb', line 15

def self.all
  @@all
end

.get_or_create_categoriesObject



19
20
21
22
23
24
25
# File 'lib/wikihow/category.rb', line 19

def self.get_or_create_categories
  if self.all == []
    self.scrape_for_categories.each{|category| self.new(category)}
  end
  #binding.pry
  self.all
end

.scrape_for_categoriesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/wikihow/category.rb', line 27

def self.scrape_for_categories
  doc = Nokogiri::HTML(open("https://www.wikihow.com/Main-Page"))
  categories_array = []
  doc.search("#hp_categories a").each do |category|
    title = category.text
    url = category.attr("href")
    categories_array << {:title => title,:url => url}
  end
  categories_array
end

Instance Method Details

#add_topic(topic) ⇒ Object



11
12
13
# File 'lib/wikihow/category.rb', line 11

def add_topic(topic)
  self.topics << topic
end