-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.rb
74 lines (65 loc) · 1.78 KB
/
generate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/usr/env ruby
require 'nokogiri'
require 'fileutils'
require 'json'
require 'time'
require 'pp'
module View
class Entry < Struct.new(:entry_url, :title, :abstract_html, :icon_url, :published_at)
def initialize(entry_url, title, abstract_html, icon_url, published_at)
if entry_url.nil?
STDERR.puts 'entry_urlがありません:'
raise ArgumentError
end
if title.nil?
STDERR.puts 'titleがありません:'
raise ArgumentError
end
if published_at.nil?
STDERR.puts 'published_atがありません:'
raise ArgumentError
end
super(entry_url, title, abstract_html, icon_url, Time.parse(published_at))
end
def abstract
unless self.abstract_html.nil?
Nokogiri::HTML(self.abstract_html).text
end
end
alias :old_published_at :published_at
def published_at
old_published_at.strftime('%Y-%m-%d %H:%M')
end
alias :old_to_h :to_h
def to_h
old_to_h.merge(abstract: self.abstract, published_at: self.published_at)
end
end
class SourceFeed
attr_reader :feed_id, :title, :feed_url, :icon_url, :blog_url
def initialize(feed_id, title, feed_url, icon_url, blog_url)
@feed_id = feed_id
@title = title
@feed_url = feed_url
@icon_url = icon_url
@blog_url = blog_url
end
end
end
ENTRY_COUNT = 50.freeze
entries_json_string = STDIN.read
entries = JSON.parse(entries_json_string)['entries'].map do |e|
begin
View::Entry.new(
e['entry_url'],
e['title'],
e['abstract'],
e['icon_url'],
e['published_at']
)
rescue ArgumentError
STDERR.puts "#{e.pretty_inspect}"
exit(status=1)
end
end.sort_by(&:published_at).reverse.first(ENTRY_COUNT)
puts JSON.dump(entries.map(&:to_h))