Giter VIP home page Giter VIP logo

ruby-hackernews's Introduction

ruby-hackernews

An API over Hacker News

Requirements

mechanize (>= 1.0.0)
require_all (>= 1.1.0)

Installation

gem install ruby-hackernews

then, in your script:

require 'ruby-hackernews'

before using it.

Entries

You can get entries on the main page with:

Entry.all       # returns the main page's entries as an array

You can provide a number of pages:

Entry.all(3)    # will return the entries on the first 3 pages

There are methods for getting specific entry types:

Entry.questions # gets the first page of questions (ask NH)
Entry.newest    # gets the first page of new links (new)
Entry.jobs      # gets the first page of job offerts (jobs)

Each Entry instance has the following data:

entry = Entry.all.first # gets the top entry on the mainpage

entry.number       # the entry's position on HN

entry.link.title   # the link's name on HN
entry.link.href    # the actual link
entry.link.site    # the referring site, if any

entry.voting.score # the entry's score on HN

entry.user.name    # the submitter's user name

entry.time         # the elapsed time from submission

After you’ve logged in (see below) you can do the following

entry.upvote                     # votes the entry
entry.write_comment("mycomment") # adds a comment to the entry
Entry.submit("mytitle", "myurl") # submit a new link
Entry.submit("myquestion")       # submit a new question (ask HN)

Comments

You get an entry’s comments with:

entry.comments

You can also get the newest comments on HN with:

Comments.newest
Comments.newest(3)  # gets the first 3 pages of new comments

Each Comment instance has the following data:

comment = Entry.all.first.comments.first # gets the first comment of the first entry on HN's main page

comment.text           # comment's body
comment.user.name      # poster's user name on HN
comment.voting.score   # comment's score

Comments are enumerable and threaded, so you can do like:

comment[2][0]          # gets the third reply to this comment, then the first reply to the reply
comment.first          # gets the first reply to this comment
comment.select do |c|  # gets all the comment replies which text contains "test"
  text ~= /test/
end
comment.parent         # gets the comment's parent (nil if no parent)

Once you’re logged in (see below), you can do the following:

comment.upvote
comment.downvote
comment.reply("mycomment")

Logging in

You define a user with:

user = User.new("username")

Then, you log in with:

user.login("password")

You can log out with:

user.logout

You have to log out before logging in with a different user.

TO DO

Get user info (submission, comments...)
Create account
Change user info/settings

THANKS TO

ruby-hackernews's People

Contributors

marckohlbrugge avatar zachaysan avatar boctor avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.