Class: NHKore::News
Overview
Direct Known Subclasses
Constant Summary collapse
Instance Attribute Summary collapse
- #articles ⇒ Object readonly
- #sha256s ⇒ Object readonly
Class Method Summary collapse
- .build_file(filename) ⇒ Object
- .load_data(data, article_class: Article, file: nil, news_class: News, overwrite: false, **kargs) ⇒ Object
Instance Method Summary collapse
- #add_article(article, key: nil, overwrite: false) ⇒ Object
- #article(key) ⇒ Object
- #article?(key) ⇒ Boolean
- #article_with_sha256(sha256) ⇒ Object
- #encode_with(coder) ⇒ Object
-
#initialize ⇒ News
constructor
A new instance of News.
- #sha256?(sha256) ⇒ Boolean
- #to_s ⇒ Object
- #update_article(article, url) ⇒ Object
Methods included from Fileable
Constructor Details
#initialize ⇒ News
Returns a new instance of News.
32 33 34 35 36 37 |
# File 'lib/nhkore/news.rb', line 32 def initialize super() @articles = {} @sha256s = {} end |
Instance Attribute Details
#articles ⇒ Object (readonly)
29 30 31 |
# File 'lib/nhkore/news.rb', line 29 def articles @articles end |
#sha256s ⇒ Object (readonly)
30 31 32 |
# File 'lib/nhkore/news.rb', line 30 def sha256s @sha256s end |
Class Method Details
.build_file(filename) ⇒ Object
56 57 58 |
# 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) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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 |
Instance Method Details
#add_article(article, key: nil, overwrite: false) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
94 95 96 97 98 |
# File 'lib/nhkore/news.rb', line 94 def article(key) key = key.to_s unless key.nil? return @articles[key] end |
#article?(key) ⇒ Boolean
114 115 116 117 118 |
# 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) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/nhkore/news.rb', line 60 def encode_with(coder) # Order matters. # Don't output @sha256s. coder[:articles] = @articles end |
#sha256?(sha256) ⇒ Boolean
120 121 122 |
# File 'lib/nhkore/news.rb', line 120 def sha256?(sha256) return @sha256s.key?(sha256) end |
#to_s ⇒ Object
124 125 126 127 |
# 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) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# 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 |