Class: NHKore::News

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

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.0

Direct Known Subclasses

FutsuuNews, YasashiiNews

Constant Summary collapse

DEFAULT_DIR =

Since:

  • 0.2.0

Util::CORE_DIR
FAVORED_URL =

Since:

  • 0.2.0

/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.

Since:

  • 0.2.0



32
33
34
35
36
37
# File 'lib/nhkore/news.rb', line 32

def initialize
  super()

  @articles = {}
  @sha256s = {}
end

Instance Attribute Details

#articlesObject (readonly)

Since:

  • 0.2.0



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

def articles
  @articles
end

#sha256sObject (readonly)

Since:

  • 0.2.0



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

def sha256s
  @sha256s
end

Class Method Details

.build_file(filename) ⇒ Object

Since:

  • 0.2.0



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

Since:

  • 0.2.0



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

Since:

  • 0.2.0



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

Since:

  • 0.2.0



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

Returns:

  • (Boolean)

Since:

  • 0.2.0



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

Since:

  • 0.2.0



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

Since:

  • 0.2.0



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

Returns:

  • (Boolean)

Since:

  • 0.2.0



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

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

#to_sObject

Since:

  • 0.2.0



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

Since:

  • 0.2.0



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