hn 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
7
7
  spec.pattern = 'spec/**/*_spec.rb'
8
8
  end
9
9
 
10
- task :default => :spec
10
+ task :default => :spec
data/hn.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency 'rake'
21
21
  gem.add_development_dependency 'simplecov'
22
22
  gem.add_development_dependency 'chronic'
23
+ gem.add_development_dependency 'fakeweb'
23
24
 
24
25
  gem.add_runtime_dependency 'nokogiri'
25
26
  end
@@ -1,6 +1,6 @@
1
1
  module HackerNews
2
2
  class Entry
3
- attr_accessor :id, :title, :link, :site, :points, :num_comments, :time_string, :user
3
+ attr_accessor :id, :title, :link, :site, :points, :num_comments, :time_string, :username
4
4
 
5
5
  def initialize
6
6
  yield self if block_given?
@@ -4,15 +4,19 @@ require "open-uri"
4
4
  module HackerNews
5
5
  class EntryParser
6
6
  def homepage
7
- parse_entries 'http://news.ycombinator.com/'
7
+ parse_entries site_name
8
8
  end
9
9
 
10
10
  def newest
11
- parse_entries 'http://news.ycombinator.com/newest'
11
+ parse_entries "#{site_name}newest"
12
12
  end
13
13
 
14
14
  private
15
15
 
16
+ def site_name
17
+ 'http://news.ycombinator.com/'
18
+ end
19
+
16
20
  def parse_entries(url)
17
21
  # doc = Nokogiri::HTML(open('spec/fixtures/home.html'))
18
22
  doc = Nokogiri::HTML(open(url))
@@ -23,9 +27,10 @@ module HackerNews
23
27
  Entry.new do |entry|
24
28
  entry.title = trs[i*3].at_css('td.title a').text
25
29
  entry.link = trs[i*3].at_css('td.title a')['href']
30
+ entry.link = site_name + entry.link unless entry.link =~ /^http/
26
31
  entry.site = trs[i*3].at_css('td.title span.comhead').text.match(/\((.+)\)/)[1] rescue nil
27
32
  entry.points = trs[i*3+1].at_css('td.subtext span').text.to_i rescue 0
28
- entry.user = trs[i*3+1].at_css('td.subtext a').text
33
+ entry.username = trs[i*3+1].at_css('td.subtext a').text
29
34
  entry.time_string = trs[i*3+1].at_css('td.subtext a').next.text.sub('|', '').strip
30
35
  entry.num_comments = trs[i*3+1].css('td.subtext a')[1].text.to_i
31
36
  entry.id = trs[i*3+1].css('td.subtext a')[1]['href'].match(/\d+/)[0].to_i
data/lib/hn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module HackerNews
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,6 +1,9 @@
1
1
  require "spec_helper"
2
2
  require "chronic"
3
3
 
4
+ FakeWeb.register_uri :get, 'http://news.ycombinator.com/', :body => fixture('home.html')
5
+ FakeWeb.register_uri :get, 'http://news.ycombinator.com/newest', :body => fixture('newest.html')
6
+
4
7
  module HackerNews
5
8
  describe EntryParser do
6
9
  subject { EntryParser.new }
@@ -9,13 +12,12 @@ module HackerNews
9
12
 
10
13
  context "homepage" do
11
14
  it "should parse them right" do
12
- parser.stub(:open).and_return(open('spec/fixtures/home.html'))
13
15
  entries = parser.homepage
14
16
  entries.count.should == 30
15
17
  entries.first.should be_an Entry
16
18
  entries.each do |entry|
17
19
  entry.id.should > 4000000
18
- entry.user.should =~ /\w+/
20
+ entry.username.should =~ /\w+/
19
21
  entry.link.should =~ /^http/ unless entry.site.nil?
20
22
  entry.title.should_not be_empty
21
23
  entry.num_comments.should > 0
@@ -28,14 +30,18 @@ module HackerNews
28
30
 
29
31
  context "newest" do
30
32
  it "should parse them right" do
31
- parser.stub(:open).and_return(open('spec/fixtures/newest.html'))
32
33
  entries = parser.newest
33
34
  entries.count.should == 30
34
35
  entries.first.should be_an Entry
35
36
  entries.each do |entry|
36
37
  entry.id.should > 4000000
37
- entry.user.should =~ /\w+/
38
- entry.link.should =~ /^http/ unless entry.site.nil?
38
+ entry.username.should =~ /\w+/
39
+ entry.link.should =~ /^http/
40
+
41
+ if entry.site.nil? # ASK HN
42
+ entry.id.should == entry.link.match(/^http:\/\/news\.ycombinator\.com\/item\?id=(\d+)$/)[1].to_i
43
+ end
44
+
39
45
  entry.title.should_not be_empty
40
46
  entry.num_comments.should >= 0
41
47
  entry.site.should_not =~ /^http/
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,12 @@ SimpleCov.start do
3
3
  add_filter 'spec'
4
4
  end
5
5
 
6
- require "open-uri"
7
6
  require 'hn'
8
7
 
9
- def fixture(filename)
10
- open("spec/fixtures/#{filename}") { |file| file.read }
11
- end
8
+ require "fakeweb"
9
+ FakeWeb.allow_net_connect = false
10
+
11
+ require "open-uri"
12
+ def fixture(fixture_name)
13
+ open("spec/fixtures/#{fixture_name}") { |p| p.read }
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-28 00:00:00.000000000 Z
12
+ date: 2012-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70199363409420 !ruby/object:Gem::Requirement
16
+ requirement: &70126472762360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70199363409420
24
+ version_requirements: *70126472762360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70199363409000 !ruby/object:Gem::Requirement
27
+ requirement: &70126472761940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70199363409000
35
+ version_requirements: *70126472761940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: simplecov
38
- requirement: &70199363408580 !ruby/object:Gem::Requirement
38
+ requirement: &70126472761520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70199363408580
46
+ version_requirements: *70126472761520
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: chronic
49
- requirement: &70199363408160 !ruby/object:Gem::Requirement
49
+ requirement: &70126472761100 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,21 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70199363408160
57
+ version_requirements: *70126472761100
58
+ - !ruby/object:Gem::Dependency
59
+ name: fakeweb
60
+ requirement: &70126472760680 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70126472760680
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: nokogiri
60
- requirement: &70199363407740 !ruby/object:Gem::Requirement
71
+ requirement: &70126472760260 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,7 +76,7 @@ dependencies:
65
76
  version: '0'
66
77
  type: :runtime
67
78
  prerelease: false
68
- version_requirements: *70199363407740
79
+ version_requirements: *70126472760260
69
80
  description: A tiny gem to fetch Hacker News entries
70
81
  email:
71
82
  - afu@forresty.com
@@ -105,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
116
  version: '0'
106
117
  segments:
107
118
  - 0
108
- hash: 3803145340244818705
119
+ hash: 4450533506662142412
109
120
  required_rubygems_version: !ruby/object:Gem::Requirement
110
121
  none: false
111
122
  requirements:
@@ -114,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
125
  version: '0'
115
126
  segments:
116
127
  - 0
117
- hash: 3803145340244818705
128
+ hash: 4450533506662142412
118
129
  requirements: []
119
130
  rubyforge_project:
120
131
  rubygems_version: 1.8.11