hackernews 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/hackernews.rb +17 -5
  2. data/test/hackernews_test.rb +14 -5
  3. metadata +5 -12
data/lib/hackernews.rb CHANGED
@@ -4,7 +4,7 @@ require 'open-uri'
4
4
 
5
5
  class HackerNews
6
6
 
7
- VERSION = '0.2.0'
7
+ VERSION = '0.2.1'
8
8
 
9
9
  # Returns the version string for the library.
10
10
  def self.version
@@ -20,8 +20,13 @@ class HackerNews
20
20
  class LoginError < RuntimeError; end
21
21
 
22
22
  # Creates a new HackerNews object.
23
- # Specify your username and password.
24
- def initialize(username, password)
23
+ # If username and password are provided, login is called.
24
+ def initialize(username=nil, password=nil)
25
+ login(username, password) if username and password
26
+ end
27
+
28
+ # Log into Hacker News with the specified username and password.
29
+ def login(username, password)
25
30
  login_url = get(BASE_URL).match(/href="([^"]+)">login<\/a>/)[1]
26
31
  form_html = get(BASE_URL + login_url)
27
32
  fnid = form_html.match(/<input type=hidden name="fnid" value="([^"]+)"/)[1]
@@ -38,8 +43,9 @@ class HackerNews
38
43
  user_page(username).match(/<td valign=top>karma\:<\/td><td>(\d+)<\/td>/)[1]
39
44
  end
40
45
 
41
- # Retrieves the average karma per post for the logged in user.
46
+ # Retrieves the average karma per post for the logged in user (must be logged in).
42
47
  def average_karma
48
+ require_login!
43
49
  user_page.match(/<td valign=top>avg:<\/td><td>([\d\.]+)<\/td>/)[1]
44
50
  end
45
51
 
@@ -52,14 +58,16 @@ class HackerNews
52
58
  end
53
59
  end
54
60
 
55
- # Up-vote on a post or on a comment by passing in the id number.
61
+ # Up-vote a post or a comment by passing in the id number.
56
62
  def vote(id)
63
+ require_login!
57
64
  url = get(ITEM_URL % id).match(/<a id=up_\d+ onclick="return vote\(this\)" href="(vote\?[^"]+)">/)[1]
58
65
  get(BASE_URL + '/' + url)
59
66
  end
60
67
 
61
68
  # Post a comment on a posted item or on another comment.
62
69
  def comment(id, text)
70
+ require_login!
63
71
  fnid = get(ITEM_URL % id).match(/<input type=hidden name="fnid" value="([^"]+)"/)[1]
64
72
  post(COMMENT_SUBMIT_URL, 'fnid' => fnid, 'text' => text)
65
73
  end
@@ -96,5 +104,9 @@ class HackerNews
96
104
  def build_header
97
105
  @cookie && {'Cookie' => @cookie}
98
106
  end
107
+
108
+ def require_login!
109
+ raise(LoginError, "You must log in to perform this action.") unless @cookie
110
+ end
99
111
 
100
112
  end
@@ -4,30 +4,39 @@ require File.dirname(__FILE__) + '/../lib/hackernews.rb'
4
4
  class HackerNewsTest < Test::Unit::TestCase
5
5
 
6
6
  def setup
7
- if ENV['HN_USERNAME'] and ENV['HN_PASSWORD']
8
- @hn = HackerNews.new(ENV['HN_USERNAME'], ENV['HN_PASSWORD'])
9
- else
7
+ unless ENV['HN_USERNAME'] and ENV['HN_PASSWORD']
10
8
  puts 'Must set HN_USERNAME and HN_PASSWORD env variables before running.'
11
9
  exit(1)
12
10
  end
11
+ @hn = HackerNews.new
12
+ end
13
+
14
+ def login
15
+ @hn.login(ENV['HN_USERNAME'], ENV['HN_PASSWORD'])
13
16
  end
14
17
 
15
18
  def test_session_cookie
19
+ login
16
20
  assert @hn.instance_eval('@cookie') =~ /user=[a-z0-9]+;/i
17
21
  end
18
22
 
19
23
  def test_login_failure
20
24
  assert_raise HackerNews::LoginError do
21
- HackerNews.new('foobar00000', 'baz')
25
+ @hn.login('foobar00000', 'baz')
22
26
  end
23
27
  end
24
28
 
25
29
  def test_karma
26
- assert @hn.karma.to_s != ''
27
30
  assert @hn.karma('pg').to_i > 59000
31
+ login
32
+ assert @hn.karma.to_s != ''
28
33
  end
29
34
 
30
35
  def test_average_karma
36
+ assert_raise HackerNews::LoginError do
37
+ @hn.average_karma
38
+ end
39
+ login
31
40
  assert @hn.average_karma.to_s != ''
32
41
  end
33
42
 
metadata CHANGED
@@ -1,12 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hackernews
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- - 0
9
- version: 0.2.0
4
+ version: 0.2.1
10
5
  platform: ruby
11
6
  authors:
12
7
  - Tim Morgan
@@ -14,7 +9,7 @@ autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
11
 
17
- date: 2010-03-03 00:00:00 -06:00
12
+ date: 2010-03-04 00:00:00 -06:00
18
13
  default_executable:
19
14
  dependencies: []
20
15
 
@@ -43,20 +38,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
38
  requirements:
44
39
  - - ">="
45
40
  - !ruby/object:Gem::Version
46
- segments:
47
- - 0
48
41
  version: "0"
42
+ version:
49
43
  required_rubygems_version: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - ">="
52
46
  - !ruby/object:Gem::Version
53
- segments:
54
- - 0
55
47
  version: "0"
48
+ version:
56
49
  requirements: []
57
50
 
58
51
  rubyforge_project:
59
- rubygems_version: 1.3.6
52
+ rubygems_version: 1.3.5
60
53
  signing_key:
61
54
  specification_version: 3
62
55
  summary: Ruby gem to login and interact with the Hacker News website.