class APWArticles::Category

Constants

CATEGORIES

Attributes

articles[RW]
name[RW]
url[RW]

Public Class Methods

all() click to toggle source
# File lib/apw_articles/category.rb, line 18
def self.all
  @@all
end
create_from_url() click to toggle source
# File lib/apw_articles/category.rb, line 30
def self.create_from_url
  APWArticles::Scraper.scrape_categories.each do |category|
    self.find_or_create_by_url(category)
  end
end
defaults() click to toggle source

NOTE: the CATEGORIES constant was gathered using the APWArticles::Scraper.scrape_categories method, however as the only way to scrape this information was iterating over 66 articles which is rather time-consuming in the CLI, I have elected to hard code the categories as default categories. New category objects will still be made when they’re encountered in articles.

# File lib/apw_articles/category.rb, line 7
def self.defaults
  CATEGORIES.each {|url| self.find_or_create_by_url(url)}
end
find_or_create_by_url(url) click to toggle source
# File lib/apw_articles/category.rb, line 22
def self.find_or_create_by_url(url)
  if self.all.detect{|category| category.url == url } == nil
    self.new(url)
  else
    self.all.detect{|category| category.url == url }
  end
end
new(url) click to toggle source
# File lib/apw_articles/category.rb, line 11
def initialize(url)
  self.name = url.gsub(/-/, ' ').split.map(&:capitalize).join(' ')
  self.url = url
  self.class.all << self
  self.articles = []
end