/hackernews_ruby

API Wrapper for Hacker News

Primary LanguageRubyMIT LicenseMIT

HackernewsRuby

Coverage Status Build Status Gem Version

A wrapper for the new Hacker News API.

Installation

Add this line to your application's Gemfile:

gem 'hackernews_ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hackernews_ruby

Usage

Instantiate a client like so:

client = HackernewsRuby::Client.new

Items

Items have the following fields:

Outlined here form HN

Field Description
id The item's unique id. Required.
deleted true if the item is deleted.
type The type of item. One of "job", "story", "comment", "poll", or "pollopt".
by The username of the item's author.
time Creation date of the item, in Unix Time.
text The comment, Ask HN, or poll text. HTML.
dead true if the item is dead.
parent The item's parent. For comments, either another comment or the relevant story. For pollopts, the relevant poll.
kids The ids of the item's comments, in ranked display order.
url The URL of the story.
score The story's score, or the votes for a pollopt.
title The title of the story or poll.
parts A list of related pollopts, in display order.

To get an item simply do:

client.get_item(834129)

This will get any item available on the API by ID such as stories, comments, polls and jobs.

Say you wanted the title of a story:

story = client.get_item(8863) #story_id
story.title
=> "My YC app: Dropbox - Throw away your USB drive"

Users

Users have the following fields:

Outlined here from HN

Field Description
id The user's unique username. Case-sensitive. Required.
delay Delay in minutes between a comment's creation and its visibility to other users.
created Creation date of the user, in Unix Time.
karma The user's karma.
about The user's optional self-description. HTML.
submitted List of the user's stories, polls and comments.

Say you wanted to fetch a particular #user:

user = client.get_user('jl') #userid is case sensitive
user.about
=> "This is a test"

Live Data

To fetch the top 100 stories use #top_stories:

stories = client.top_stories

This will return an array of ID's. To get each story after that, just use the #get_item method like this:

stories = client.top_stories
stories.each do |story|
  resp = client.get_item(story)
  puts resp.title
  puts resp.score
  puts resp.url
end

To get the most updated Profiles & Items you can use #updated:

updates = client.updated
updates.items
=> [1212, 1214]
updates.profiles
=> [5653, 25456]

To get the latest item created you need to call #max_item:

latest = client.max_item
=> 12343

Or to get contents of the item, just method chain:

latest = client.max_item
client.get_item(latest)

Contributing

  1. Fork it ( https://github.com/[my-github-username]/hackernews_ruby/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request