ruby-hackernews 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -15,7 +15,13 @@ then, in your script:
15
15
 
16
16
  require 'ruby-hackernews'
17
17
 
18
- before using it.
18
+ before using it. If you want to include the namespace (RubyHackernews), add
19
+
20
+ include RubyHackernews
21
+
22
+ otherwise, you'll have to access the gem's classes adding the module, like this:
23
+
24
+ RubyHackernews::Entry.all
19
25
 
20
26
  == Entries
21
27
 
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'rake/testtask'
8
8
 
9
9
  spec = Gem::Specification.new do |s|
10
10
  s.name = 'ruby-hackernews'
11
- s.version = '1.3.0'
11
+ s.version = '1.3.1'
12
12
  s.add_dependency('require_all', '>= 1.1.0')
13
13
  s.add_dependency('mechanize', '>= 1.0.0')
14
14
  s.has_rdoc = false
@@ -14,11 +14,19 @@ module RubyHackernews
14
14
  @user = user
15
15
  @time = time
16
16
  @comments_info = comments
17
+ @cache = PageFetcher.new(@comments_info.page)
18
+ end
19
+
20
+ def text
21
+ unless @text
22
+ @text = TextService.new.get_text(@cache.page)
23
+ end
24
+ return @text
17
25
  end
18
26
 
19
27
  def comments
20
28
  unless @comments
21
- @comments = CommentService.new.get_comments(@comments_info.page)
29
+ @comments = CommentService.new.get_comments(@cache.page)
22
30
  end
23
31
  return @comments
24
32
  end
@@ -13,10 +13,6 @@ module RubyHackernews
13
13
  return LoginService.new.login(@name, password)
14
14
  end
15
15
 
16
- def logout
17
- return LoginService.new.logout
18
- end
19
-
20
16
  def signup(password)
21
17
  return SignupService.new.signup(@name, password)
22
18
  end
@@ -28,7 +24,11 @@ module RubyHackernews
28
24
  def saved(pages = 1)
29
25
  return UserInfoService.new.saved(@name, pages)
30
26
  end
31
-
27
+
28
+ def logout
29
+ return LoginService.new.logout
30
+ end
31
+
32
32
  def comments(pages = 1)
33
33
  return UserInfoService.new.comments(@name, pages)
34
34
  end
@@ -3,24 +3,33 @@ module RubyHackernews
3
3
  class CommentService
4
4
  include MechanizeContext
5
5
 
6
- def get_comments(page_url)
6
+ def get_comments(page)
7
+ table = page.search("table")[3]
8
+ return get_comments_entities(table)
9
+ end
10
+
11
+ def get_user_comments(user)
12
+ page = agent.get(ConfigurationService.base_url + "threads?id=#{user.name}")
13
+ table = page.search("table")[2]
14
+ return get_comments_entities(table)
15
+ end
16
+
17
+ def get_comments_entities(table)
7
18
  comments = []
8
- last = comments
9
- current_level = -1
10
- page = agent.get(page_url)
11
- page.search("//table")[3].search("table/tr").select do |tr|
19
+ target = comments
20
+ current_level = 0
21
+ table.search("table/tr").select do |tr|
12
22
  tr.search("span.comment").inner_html != "[deleted]"
13
23
  end.each do |tr|
14
24
  comment = parse_comment(tr)
15
25
  level = tr.search("img[@src='http://ycombinator.com/images/s.gif']").first['width'].to_i / 40
16
26
  difference = current_level - level
17
- target = last
18
27
  (difference + 1).times do
19
- target = target.parent || comments
28
+ target = target.kind_of?(Comment) && target.parent ? target.parent : comments
20
29
  end
21
- target << comment
22
- last = comment
23
30
  current_level = level
31
+ target << comment
32
+ target = comment
24
33
  end
25
34
  return comments
26
35
  end
@@ -59,7 +68,6 @@ module RubyHackernews
59
68
  form.submit
60
69
  return true
61
70
  end
62
-
63
- end
64
71
 
72
+ end
65
73
  end
@@ -0,0 +1,20 @@
1
+
2
+ module RubyHackernews
3
+
4
+ #acts as a cache
5
+ class PageFetcher
6
+ include MechanizeContext
7
+
8
+ def initialize(page_url)
9
+ @url = page_url
10
+ end
11
+
12
+ def page
13
+ unless @page
14
+ @page = agent.get(@url)
15
+ end
16
+ return @page
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+
2
+ module RubyHackernews
3
+
4
+ class TextParser
5
+
6
+ def initialize(table)
7
+ @target_line = table.search("tr")[3]
8
+ end
9
+
10
+ def parse
11
+ return @target_line.search("td")[1].inner_text
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module RubyHackernews
2
+
3
+ class TextService
4
+ include MechanizeContext
5
+
6
+ def get_text(page)
7
+ return TextParser.new(page.search("table")[2]).parse
8
+ end
9
+
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-hackernews
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
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-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: require_all
16
- requirement: &16407680 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 1.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *16407680
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: mechanize
27
- requirement: &16407120 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: 1.0.0
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *16407120
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
36
46
  description: An interface to Hacker News
37
47
  email: andrea@andreadallera.com
38
48
  executables: []
@@ -41,31 +51,34 @@ extra_rdoc_files: []
41
51
  files:
42
52
  - README.rdoc
43
53
  - Rakefile
44
- - lib/ruby-hackernews/services/not_authenticated_error.rb
45
- - lib/ruby-hackernews/services/parsers/comments_info_parser.rb
46
- - lib/ruby-hackernews/services/parsers/link_info_parser.rb
54
+ - lib/ruby-hackernews.rb
55
+ - lib/ruby-hackernews/services/comment_service.rb
56
+ - lib/ruby-hackernews/services/entry_service.rb
57
+ - lib/ruby-hackernews/services/voting_service.rb
58
+ - lib/ruby-hackernews/services/text_service.rb
47
59
  - lib/ruby-hackernews/services/parsers/entry_page_parser.rb
48
- - lib/ruby-hackernews/services/parsers/user_info_parser.rb
49
- - lib/ruby-hackernews/services/parsers/time_info_parser.rb
60
+ - lib/ruby-hackernews/services/parsers/comments_info_parser.rb
50
61
  - lib/ruby-hackernews/services/parsers/voting_info_parser.rb
51
62
  - lib/ruby-hackernews/services/parsers/entry_parser.rb
52
- - lib/ruby-hackernews/services/user_info_service.rb
53
- - lib/ruby-hackernews/services/signup_service.rb
54
- - lib/ruby-hackernews/services/entry_service.rb
63
+ - lib/ruby-hackernews/services/parsers/user_info_parser.rb
64
+ - lib/ruby-hackernews/services/parsers/link_info_parser.rb
65
+ - lib/ruby-hackernews/services/parsers/text_parser.rb
66
+ - lib/ruby-hackernews/services/parsers/time_info_parser.rb
55
67
  - lib/ruby-hackernews/services/login_service.rb
56
- - lib/ruby-hackernews/services/configuration_service.rb
68
+ - lib/ruby-hackernews/services/signup_service.rb
69
+ - lib/ruby-hackernews/services/not_authenticated_error.rb
70
+ - lib/ruby-hackernews/services/user_info_service.rb
57
71
  - lib/ruby-hackernews/services/mechanize_context.rb
58
- - lib/ruby-hackernews/services/comment_service.rb
59
- - lib/ruby-hackernews/services/voting_service.rb
60
- - lib/ruby-hackernews/domain/entry/link_info.rb
72
+ - lib/ruby-hackernews/services/page_fetcher.rb
73
+ - lib/ruby-hackernews/services/configuration_service.rb
74
+ - lib/ruby-hackernews/domain/user.rb
61
75
  - lib/ruby-hackernews/domain/entry/time_info.rb
62
- - lib/ruby-hackernews/domain/entry/entry.rb
76
+ - lib/ruby-hackernews/domain/entry/link_info.rb
63
77
  - lib/ruby-hackernews/domain/entry/user_info.rb
64
- - lib/ruby-hackernews/domain/entry/comments_info.rb
65
78
  - lib/ruby-hackernews/domain/entry/voting_info.rb
66
- - lib/ruby-hackernews/domain/user.rb
79
+ - lib/ruby-hackernews/domain/entry/comments_info.rb
80
+ - lib/ruby-hackernews/domain/entry/entry.rb
67
81
  - lib/ruby-hackernews/domain/comment/comment.rb
68
- - lib/ruby-hackernews.rb
69
82
  homepage: http://github.com/bolthar/ruby-hackernews
70
83
  licenses: []
71
84
  post_install_message:
@@ -86,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
99
  version: '0'
87
100
  requirements: []
88
101
  rubyforge_project:
89
- rubygems_version: 1.8.17
102
+ rubygems_version: 1.8.24
90
103
  signing_key:
91
104
  specification_version: 3
92
105
  summary: An interface to Hacker News