class NHKore::News

@author Jonathan Bradley Whited @since 0.2.0

Constants

DEFAULT_DIR
FAVORED_URL

Attributes

articles[R]
sha256s[R]

Public Class Methods

build_file(filename) click to toggle source
# File lib/nhkore/news.rb, line 56
def self.build_file(filename)
  return File.join(DEFAULT_DIR,filename)
end
load_data(data,article_class: Article,file: nil,news_class: News,overwrite: false,**kargs) click to toggle source
# File lib/nhkore/news.rb, line 67
def self.load_data(data,article_class: Article,file: nil,news_class: News,overwrite: false,**kargs)
  data = Util.load_yaml(data,file: file)

  articles = data[:articles]

  news = news_class.new

  articles&.each() do |key,hash|
    key = key.to_s # Change from a symbol
    news.add_article(article_class.load_data(key,hash),key: key,overwrite: overwrite)
  end

  return news
end
new() click to toggle source
Calls superclass method
# File lib/nhkore/news.rb, line 32
def initialize
  super()

  @articles = {}
  @sha256s = {}
end

Public Instance Methods

add_article(article,key: nil,overwrite: false) click to toggle source
# File lib/nhkore/news.rb, line 39
def add_article(article,key: nil,overwrite: false)
  url = article.url
  url = url.to_s unless url.nil?

  key = key.nil? ? url : key.to_s

  if !overwrite
    raise ArgumentError,"duplicate article[#{key}] in articles" if @articles.key?(key)
    raise ArgumentError,"duplicate sha256[#{article.sha256}] in articles" if @sha256s.key?(article.sha256)
  end

  @articles[key] = article
  @sha256s[article.sha256] = url

  return self
end
article(key) click to toggle source
# File lib/nhkore/news.rb, line 94
def article(key)
  key = key.to_s unless key.nil?

  return @articles[key]
end
article?(key) click to toggle source
# File lib/nhkore/news.rb, line 114
def article?(key)
  key = key.to_s unless key.nil?

  return @articles.key?(key)
end
article_with_sha256(sha256) click to toggle source
# File lib/nhkore/news.rb, line 100
def article_with_sha256(sha256)
  article = nil

  @articles.each_value do |a|
    if a.sha256 == sha256
      article = a

      break
    end
  end

  return article
end
encode_with(coder) click to toggle source
# File lib/nhkore/news.rb, line 60
def encode_with(coder)
  # Order matters.
  # Don't output @sha256s.

  coder[:articles] = @articles
end
sha256?(sha256) click to toggle source
# File lib/nhkore/news.rb, line 120
def sha256?(sha256)
  return @sha256s.key?(sha256)
end
to_s() click to toggle source
# File lib/nhkore/news.rb, line 124
def to_s
  # Put each Word on one line (flow/inline style).
  return Util.dump_yaml(self,flow_level: 8)
end
update_article(article,url) click to toggle source
# File lib/nhkore/news.rb, line 82
def update_article(article,url)
  url = url.to_s unless url.nil?

  # Favor https.
  return if article.url.to_s =~ FAVORED_URL
  return if url !~ FAVORED_URL

  @articles.delete(article.url) # Probably no to_s() here
  @articles[url] = article
  article.url = url
end