TinyHNews
A Ruby wrapper for the Hacker News API that fetches and displays top stories with comments and article content.
Features
- Fetch top stories, new stories, and best stories from Hacker News
- Extract story details including title, author, score, comments
- Scrape and convert article content to Markdown
- Fetch nested comments with configurable depth and limits
- Command-line interface for easy terminal usage
- Support for concise output mode
Installation
Add this line to your application's Gemfile:
gem 'tiny_hnews'And then execute:
$ bundle installOr install it yourself as:
$ gem install tiny_hnewsUsage
Command Line Interface
The gem provides a tinyhn command for terminal usage:
# Fetch 5 top stories with default settings
$ tinyhn
# Fetch 10 stories with deeper comments
$ tinyhn -n 10 -d 3 -l 20
# Show concise output (titles and metadata only)
$ tinyhn -c
# Show only first 100 lines of each article
$ tinyhn --lines 100Command Line Options
-
-n, --number NUMBER: Number of top stories to fetch (default: 5) -
-d, --depth DEPTH: Depth of comments to fetch (default: 2) -
-l, --limit LIMIT: Limit number of comments per story (default: 10) -
-c, --concise: Only show titles and metadata -
--lines NUMBER: Number of lines of the article to show (default: 50) -
-v, --version: Show version -
-h, --help: Show help
Ruby API
Fetch Top Stories
require 'tiny_hnews'
# Create a TopStories instance with custom options
top_stories = TinyHNews::TopStories.new(
number_of_stories: 10,
comments_depth: 2,
comments_limit: 20
)
# Fetch the stories
top_stories.fetch
# Access the stories
top_stories.stories.each do |story|
puts "Title: #{story.title}"
puts "Author: #{story.by}"
puts "Score: #{story.score}"
puts "URL: #{story.url}"
puts "Comments: #{story.comments.size}"
puts "Article preview: #{story.article&.split("\n")&.first}"
puts "---"
endFetch Individual Stories
# Fetch a specific story by ID
story =
TinyHNews::Story.new(
id: 12345,
comments_depth: 3,
comments_limit: 50
).wrap
puts story.title
puts story.article
story.comments.each { |comment| puts "- #{comment}" }Story Object Properties
Each Story object provides access to the following properties:
-
id: Story ID -
title: Story title -
by: Author username -
url: Story URL -
text: Story text (for Ask HN, etc.) -
score: Story score -
time: Unix timestamp -
descendants: Number of descendants (comments) -
type: Item type (story, comment, etc.) -
kids: Array of comment IDs -
comments: Array of formatted comment texts -
article: Scraped article content in Markdown format
Dependencies
-
faraday: HTTP client for API requests -
json: JSON parsing -
reverse_markdown: HTML to Markdown conversion
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Testing
Run the test suite with:
$ bundle exec rake testContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/fguillen/TinyHNews.
License
The gem is available as open source under the terms of the MIT License.