Class: HackerNews
- Inherits:
-
Object
- Object
- HackerNews
- Defined in:
- lib/hackernews.rb
Defined Under Namespace
Classes: LoginError
Constant Summary collapse
- VERSION =
'0.2.1'
- BASE_URL =
"http://news.ycombinator.com"
- ITEM_URL =
"#{BASE_URL}/item?id=%s"
- USER_URL =
"#{BASE_URL}/user?id=%s"
- LOGIN_SUBMIT_URL =
"#{BASE_URL}/y"
- COMMENT_SUBMIT_URL =
"#{BASE_URL}/r"
Class Method Summary collapse
-
.version ⇒ Object
Returns the version string for the library.
Instance Method Summary collapse
-
#average_karma ⇒ Object
Retrieves the average karma per post for the logged in user (must be logged in).
-
#comment(id, text) ⇒ Object
Post a comment on a posted item or on another comment.
-
#initialize(username = nil, password = nil) ⇒ HackerNews
constructor
Creates a new HackerNews object.
-
#karma(username = nil) ⇒ Object
Retrieves the karma for the logged in user, or for the specified username (if given).
-
#login(username, password) ⇒ Object
Log into Hacker News with the specified username and password.
-
#user_page(username = nil) ⇒ Object
Retrieves the user page html for the specified username (or the current logged in user if none is specified).
-
#vote(id) ⇒ Object
Up-vote a post or a comment by passing in the id number.
Constructor Details
#initialize(username = nil, password = nil) ⇒ HackerNews
Creates a new HackerNews object. If username and password are provided, login is called.
24 25 26 |
# File 'lib/hackernews.rb', line 24 def initialize(username=nil, password=nil) login(username, password) if username and password end |
Class Method Details
.version ⇒ Object
Returns the version string for the library.
10 11 12 |
# File 'lib/hackernews.rb', line 10 def self.version VERSION end |
Instance Method Details
#average_karma ⇒ Object
Retrieves the average karma per post for the logged in user (must be logged in).
47 48 49 50 |
# File 'lib/hackernews.rb', line 47 def average_karma require_login! user_page.match(/<td valign=top>avg:<\/td><td>([\d\.]+)<\/td>/)[1] end |
#comment(id, text) ⇒ Object
Post a comment on a posted item or on another comment.
69 70 71 72 73 |
# File 'lib/hackernews.rb', line 69 def comment(id, text) require_login! fnid = get(ITEM_URL % id).match(/<input type=hidden name="fnid" value="([^"]+)"/)[1] post(COMMENT_SUBMIT_URL, 'fnid' => fnid, 'text' => text) end |
#karma(username = nil) ⇒ Object
Retrieves the karma for the logged in user, or for the specified username (if given).
42 43 44 |
# File 'lib/hackernews.rb', line 42 def karma(username=nil) user_page(username).match(/<td valign=top>karma\:<\/td><td>(\d+)<\/td>/)[1] end |
#login(username, password) ⇒ Object
Log into Hacker News with the specified username and password.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hackernews.rb', line 29 def login(username, password) login_url = get(BASE_URL).match(/href="([^"]+)">login<\/a>/)[1] form_html = get(BASE_URL + login_url) fnid = form_html.match(/<input type=hidden name="fnid" value="([^"]+)"/)[1] response = post(LOGIN_SUBMIT_URL, 'fnid' => fnid, 'u' => username, 'p' => password) @username = username @password = password unless @cookie = response.header['set-cookie'] raise LoginError, "Login credentials did not work." end end |
#user_page(username = nil) ⇒ Object
Retrieves the user page html for the specified username (or the current logged in user if none is specified).
53 54 55 56 57 58 59 |
# File 'lib/hackernews.rb', line 53 def user_page(username=nil) username ||= @username @user_pages ||= {} @user_pages[username] ||= begin get(USER_URL % username) end end |