tiny_hnews 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3e075115863db50ddcfca86659c7a2e52e28de6d4e2260d49ec20b1800f04101
4
+ data.tar.gz: 8eaad66ab94ebd85eab634333314a3c341a353c3bd4ea91bbcb529b2cb4d64b0
5
+ SHA512:
6
+ metadata.gz: 361da4be16c1c17c110d0a4ec84f0b98878c74a0e0745fabcb54b95d14f98c12dfbc4de6821ffe2eda48879057c1f1e4bc14a5e3f2519eedd95f530af4602005
7
+ data.tar.gz: e49627fbaae8f30620c2619a315bdc674ba4a61226f76460198f81429bc2cd0969e2226d05e4bb01f6ea66e0610e8451fc788f68c01198d51c8b0e087eb22df6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-09-25
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Fernando Guillén Suárez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # TinyHNews
2
+
3
+ API wraper for: https://github.com/HackerNews/API
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,13 @@
1
+ URL = "https://en.wikipedia.org/wiki/Ortega_hypothesis"
2
+
3
+ require "nokogiri"
4
+ require "html2markdown"
5
+ require "faraday"
6
+ require "reverse_markdown"
7
+
8
+ html = Faraday.get(URL).body
9
+ doc = Nokogiri::HTML(html)
10
+ body = doc.at("body").to_html
11
+
12
+ rm = ReverseMarkdown.convert(body, unknown_tags: :bypass)
13
+ h2m = HTMLPage.new(contents: body).markdown
@@ -0,0 +1,76 @@
1
+ require "faraday"
2
+
3
+ module TinyHNews
4
+ class API
5
+ def topstories
6
+ log("Fetching top stories")
7
+ JSON.parse(client.get("topstories.json").body)
8
+ end
9
+
10
+ def newstories
11
+ log("Fetching new stories")
12
+ JSON.parse(client.get("newstories.json").body)
13
+ end
14
+
15
+ def beststories
16
+ log("Fetching best stories")
17
+ JSON.parse(client.get("beststories.json").body)
18
+ end
19
+
20
+ def story(story_id)
21
+ log("Fetching story #{story_id}")
22
+ item(story_id)
23
+ end
24
+
25
+ def comment(comment_id)
26
+ log("Fetching comment #{comment_id}")
27
+ item(comment_id)
28
+ end
29
+
30
+ def item(item_id)
31
+ log("Fetching item #{item_id}")
32
+ JSON.parse(client.get("item/#{item_id}.json").body)
33
+ end
34
+
35
+ private
36
+
37
+ def client
38
+ @client ||=
39
+ Faraday.new(
40
+ url: "https://hacker-news.firebaseio.com/v0",
41
+ headers: { "Content-Type" => "application/json" }
42
+ )
43
+ end
44
+
45
+ def log(message)
46
+ puts "[TinyHNews::API] #{message}"
47
+ end
48
+
49
+ # Class methods that delegate to instance methods
50
+ class << self
51
+ def topstories
52
+ new.topstories
53
+ end
54
+
55
+ def newstories
56
+ new.newstories
57
+ end
58
+
59
+ def beststories
60
+ new.beststories
61
+ end
62
+
63
+ def story(story_id)
64
+ new.story(story_id)
65
+ end
66
+
67
+ def comment(comment_id)
68
+ new.comment(comment_id)
69
+ end
70
+
71
+ def item(item_id)
72
+ new.item(item_id)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,69 @@
1
+ module TinyHNews
2
+ class Story
3
+ attr_reader :id, :by, :descendants, :kids, :score, :time, :title, :type, :url, :text, :comments, :article
4
+
5
+ def initialize(id:, comments_depth: 2, comments_limit: 10)
6
+ @id = id
7
+ @comments_depth = comments_depth
8
+ @comments_limit = comments_limit
9
+ end
10
+
11
+ def wrap
12
+ fetch
13
+ scrap
14
+ load_comments
15
+ self
16
+ end
17
+
18
+ private
19
+
20
+ def fetch
21
+ response = TinyHNews::API.story(@id)
22
+ @title = response["title"]
23
+ @type = response["type"]
24
+ @by = response["by"]
25
+ @url = response["url"]
26
+ @text = response["text"]
27
+ @kids = response["kids"]
28
+ @score = response["score"]
29
+ @time = response["time"]
30
+ @descendants = response["descendants"]
31
+ @article = nil
32
+ end
33
+
34
+ def scrap
35
+ @article = TinyHNews::ToMarkdown.from_url(@url)
36
+ end
37
+
38
+ def load_comments
39
+ return [] unless @kids
40
+
41
+ @comments = fetch_comments(comment_ids: @kids)
42
+ end
43
+
44
+ def fetch_comments(
45
+ comment_ids:,
46
+ aggregated_comments: [],
47
+ current_depth: 0
48
+ )
49
+ comment_ids.each do |comment_id|
50
+ break if aggregated_comments.size >= @comments_limit
51
+
52
+ comment = TinyHNews::API.comment(comment_id)
53
+
54
+ aggregated_comments << ToMarkdown.from_hml(comment["text"]).strip
55
+
56
+ next if current_depth + 1 >= @comments_depth
57
+ next if comment["kids"].nil? || comment["kids"].empty?
58
+
59
+ fetch_comments(
60
+ comment_ids: comment["kids"],
61
+ aggregated_comments:,
62
+ current_depth: current_depth + 1
63
+ )
64
+ end
65
+
66
+ aggregated_comments
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,24 @@
1
+ require "reverse_markdown"
2
+ require "faraday"
3
+
4
+ module TinyHNews
5
+ class ToMarkdown
6
+ def self.from_url(url)
7
+ html = fetch(url)
8
+ doc = Nokogiri::HTML(html)
9
+ body = doc.at("body").to_html
10
+ from_hml(body)
11
+ rescue StandardError => e
12
+ puts "[TinyHNews::ToMarkdown] Error fetching or converting URL #{url}: #{e.message}"
13
+ nil
14
+ end
15
+
16
+ def self.from_hml(html)
17
+ ReverseMarkdown.convert(html, unknown_tags: :bypass)
18
+ end
19
+
20
+ def self.fetch(url)
21
+ Faraday.get(url).body
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ module TinyHNews
2
+ class TopStories
3
+ attr_reader :stories
4
+
5
+ def initialize(
6
+ number_of_stories: 10,
7
+ comments_depth: 2,
8
+ comments_limit: 20
9
+ )
10
+ @number_of_stories = number_of_stories
11
+ @comments_depth = comments_depth
12
+ @comments_limit = comments_limit
13
+ @stories = []
14
+ end
15
+
16
+ def fetch
17
+ top_stories = TinyHNews::API.topstories.first(@number_of_stories)
18
+
19
+ @stories =
20
+ top_stories.map do |story_id|
21
+ TinyHNews::Story.new(
22
+ id: story_id,
23
+ comments_depth: @comments_depth,
24
+ comments_limit: @comments_limit
25
+ ).wrap
26
+ end
27
+
28
+ self
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TinyHNews
4
+ VERSION = "0.1.0"
5
+ end
data/lib/tiny_hnews.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tiny_hnews/version"
4
+ require_relative "tiny_hnews/api"
5
+ require_relative "tiny_hnews/top_stories"
6
+ require_relative "tiny_hnews/story"
7
+ require_relative "tiny_hnews/to_markdown"
8
+
9
+ module TinyHNews
10
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_hnews
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Guillén Suárez
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: reverse_markdown
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: debug
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: minitest
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: mocha
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: rake
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rubocop
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ - !ruby/object:Gem::Dependency
125
+ name: webmock
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ description: A command-line tool to fetch and display top stories from Hacker News,
139
+ including comments and article content.
140
+ email:
141
+ - fguillen.mail@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - CHANGELOG.md
147
+ - LICENSE.txt
148
+ - README.md
149
+ - Rakefile
150
+ - etc/script_html_to_markdown.rb
151
+ - lib/tiny_hnews.rb
152
+ - lib/tiny_hnews/api.rb
153
+ - lib/tiny_hnews/story.rb
154
+ - lib/tiny_hnews/to_markdown.rb
155
+ - lib/tiny_hnews/top_stories.rb
156
+ - lib/tiny_hnews/version.rb
157
+ homepage: https://github.com/fguillen/TinyHNews
158
+ licenses:
159
+ - MIT
160
+ metadata:
161
+ allowed_push_host: https://rubygems.org
162
+ homepage_uri: https://github.com/fguillen/TinyHNews
163
+ source_code_uri: https://github.com/fguillen/TinyHNews
164
+ changelog_uri: https://github.com/fguillen/TinyHNews/blob/main/CHANGELOG.md
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 3.2.0
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubygems_version: 3.6.8
180
+ specification_version: 4
181
+ summary: Fetch and display top stories from Hacker News.
182
+ test_files: []