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

Returns a new instance of News.



28
29
30
31
32
33
# File 'lib/nhkore/news.rb', line 28

def initialize
  super

  @articles = {}
  @sha256s = {}
end

Instance Attribute Details

#articlesObject (readonly)

Returns the value of attribute articles.



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

def articles
  @articles
end

#sha256sObject (readonly)

Returns the value of attribute sha256s.



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

def sha256s
  @sha256s
end

Class Method Details

.build_file(filename) ⇒ Object



52
53
54
# File 'lib/nhkore/news.rb', line 52

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



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

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



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

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



90
91
92
93
94
# File 'lib/nhkore/news.rb', line 90

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

  return @articles[key]
end

#article?(key) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/nhkore/news.rb', line 110

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

  return @articles.key?(key)
end

#article_with_sha256(sha256) ⇒ Object



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

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



56
57
58
59
60
61
# File 'lib/nhkore/news.rb', line 56

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

  coder[:articles] = @articles
end

#sha256?(sha256) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



120
121
122
123
# File 'lib/nhkore/news.rb', line 120

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



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

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