Class: NHKore::News

Inherits:
Object
  • Object
show all
Includes:
Fileable
Defined in:
lib/nhkore/news.rb

Direct Known Subclasses

FutsuuNews, YasashiiNews

Constant Summary collapse

DEFAULT_DIR =
Util::CORE_DIR
FAVORED_URL =
/https:/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fileable

included, #save_file

Constructor Details

#initializeNews



26
27
28
29
30
31
# File 'lib/nhkore/news.rb', line 26

def initialize
  super

  @articles = {}
  @sha256s = {}
end

Instance Attribute Details

#articlesObject (readonly)

Returns the value of attribute articles.



23
24
25
# File 'lib/nhkore/news.rb', line 23

def articles
  @articles
end

#sha256sObject (readonly)

Returns the value of attribute sha256s.



24
25
26
# File 'lib/nhkore/news.rb', line 24

def sha256s
  @sha256s
end

Class Method Details

.build_file(filename) ⇒ Object



50
51
52
# File 'lib/nhkore/news.rb', line 50

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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nhkore/news.rb', line 61

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

Instance Method Details

#add_article(article, key: nil, overwrite: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nhkore/news.rb', line 33

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) ⇒ Object



88
89
90
91
92
# File 'lib/nhkore/news.rb', line 88

def article(key)
  key = key.to_s unless key.nil?

  return @articles[key]
end

#article?(key) ⇒ Boolean



107
108
109
110
111
# File 'lib/nhkore/news.rb', line 107

def article?(key)
  key = key.to_s unless key.nil?

  return @articles.key?(key)
end

#article_with_sha256(sha256) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nhkore/news.rb', line 94

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) ⇒ Object



54
55
56
57
58
59
# File 'lib/nhkore/news.rb', line 54

def encode_with(coder)
  # Order matters.
  # Don't output @sha256s.

  coder[:articles] = @articles
end

#sha256?(sha256) ⇒ Boolean



113
114
115
# File 'lib/nhkore/news.rb', line 113

def sha256?(sha256)
  return @sha256s.key?(sha256)
end

#to_sObject



117
118
119
120
# File 'lib/nhkore/news.rb', line 117

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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nhkore/news.rb', line 76

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