ruby-hackernews 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/README.rdoc +4 -0
  2. data/Rakefile +1 -1
  3. data/lib/ruby-hackernews/domain/comment/comment.rb +44 -41
  4. data/lib/ruby-hackernews/domain/entry/comments_info.rb +16 -13
  5. data/lib/ruby-hackernews/domain/entry/entry.rb +64 -60
  6. data/lib/ruby-hackernews/domain/entry/link_info.rb +12 -9
  7. data/lib/ruby-hackernews/domain/entry/time_info.rb +18 -15
  8. data/lib/ruby-hackernews/domain/entry/user_info.rb +11 -8
  9. data/lib/ruby-hackernews/domain/entry/voting_info.rb +12 -9
  10. data/lib/ruby-hackernews/domain/user.rb +31 -27
  11. data/lib/ruby-hackernews/services/comment_service.rb +54 -51
  12. data/lib/ruby-hackernews/services/configuration_service.rb +25 -22
  13. data/lib/ruby-hackernews/services/entry_service.rb +53 -51
  14. data/lib/ruby-hackernews/services/login_service.rb +23 -20
  15. data/lib/ruby-hackernews/services/mechanize_context.rb +23 -19
  16. data/lib/ruby-hackernews/services/not_authenticated_error.rb +8 -5
  17. data/lib/ruby-hackernews/services/parsers/comments_info_parser.rb +16 -11
  18. data/lib/ruby-hackernews/services/parsers/entry_page_parser.rb +20 -17
  19. data/lib/ruby-hackernews/services/parsers/entry_parser.rb +25 -22
  20. data/lib/ruby-hackernews/services/parsers/link_info_parser.rb +14 -12
  21. data/lib/ruby-hackernews/services/parsers/time_info_parser.rb +13 -11
  22. data/lib/ruby-hackernews/services/parsers/user_info_parser.rb +15 -11
  23. data/lib/ruby-hackernews/services/parsers/voting_info_parser.rb +16 -12
  24. data/lib/ruby-hackernews/services/signup_service.rb +15 -12
  25. data/lib/ruby-hackernews/services/user_info_service.rb +21 -18
  26. data/lib/ruby-hackernews/services/voting_service.rb +10 -7
  27. metadata +24 -24
data/README.rdoc CHANGED
@@ -33,6 +33,10 @@ There are methods for getting specific entry types:
33
33
  Entry.newest # gets the first page of new links (new)
34
34
  Entry.jobs # gets the first page of job offerts (jobs)
35
35
 
36
+ You can also get a single entry by its ID:
37
+
38
+ Entry.find(3102321) # gets that specific entry (warning: number will be 0!)
39
+
36
40
  Each Entry instance has the following data:
37
41
 
38
42
  entry = Entry.all.first # gets the top entry on the mainpage
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.2.1'
11
+ s.version = '1.3.0'
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
@@ -1,54 +1,57 @@
1
+ module RubyHackernews
1
2
 
2
- class Comment
3
- include Enumerable
3
+ class Comment
4
+ include Enumerable
4
5
 
5
- attr_reader :text
6
- attr_reader :voting
7
- attr_reader :user
6
+ attr_reader :text
7
+ attr_reader :voting
8
+ attr_reader :user
8
9
 
9
- attr_accessor :parent
10
+ attr_accessor :parent
10
11
 
11
- def initialize(text, voting, user_info, reply_link)
12
- @text = text
13
- @voting = voting
14
- @user = user_info
15
- @reply_link = reply_link
16
- @children = []
17
- end
12
+ def initialize(text, voting, user_info, reply_link)
13
+ @text = text
14
+ @voting = voting
15
+ @user = user_info
16
+ @reply_link = reply_link
17
+ @children = []
18
+ end
18
19
 
19
- def <<(comment)
20
- comment.parent = self
21
- @children << comment
22
- end
20
+ def <<(comment)
21
+ comment.parent = self
22
+ @children << comment
23
+ end
23
24
 
24
- def each(&block)
25
- @children.each(&block)
26
- end
25
+ def each(&block)
26
+ @children.each(&block)
27
+ end
27
28
 
28
- def <=>(other_comment)
29
- return other_comment.voting.score <=> @voting.score
30
- end
29
+ def <=>(other_comment)
30
+ return other_comment.voting.score <=> @voting.score
31
+ end
31
32
 
32
- def method_missing(method, *args, &block)
33
- @children.send(method, *args, &block)
34
- end
33
+ def method_missing(method, *args, &block)
34
+ @children.send(method, *args, &block)
35
+ end
35
36
 
36
- def self.newest(pages = 1)
37
- return CommentService.new.get_new_comments(pages)
38
- end
37
+ def self.newest(pages = 1)
38
+ return CommentService.new.get_new_comments(pages)
39
+ end
39
40
 
40
- def reply(text)
41
- return false unless @reply_link
42
- CommentService.new.write_comment(@reply_link, text)
43
- return true
44
- end
41
+ def reply(text)
42
+ return false unless @reply_link
43
+ CommentService.new.write_comment(@reply_link, text)
44
+ return true
45
+ end
45
46
 
46
- def upvote
47
- VotingService.new.vote(@voting.upvote)
48
- end
47
+ def upvote
48
+ VotingService.new.vote(@voting.upvote)
49
+ end
49
50
 
50
- def downvote
51
- VotingService.new.vote(@voting.downvote)
52
- end
51
+ def downvote
52
+ VotingService.new.vote(@voting.downvote)
53
+ end
53
54
 
54
- end
55
+ end
56
+
57
+ end
@@ -1,20 +1,23 @@
1
+ module RubyHackernews
1
2
 
2
- class CommentsInfo
3
+ class CommentsInfo
3
4
 
4
- attr_accessor :count
5
- attr_reader :page
5
+ attr_accessor :count
6
+ attr_reader :page
6
7
 
7
- def initialize(count, page)
8
- @count = count
9
- @page = page
10
- end
8
+ def initialize(count, page)
9
+ @count = count
10
+ @page = page
11
+ end
12
+
13
+ def id
14
+ return page[/\d+/]
15
+ end
16
+
17
+ def url
18
+ return page
19
+ end
11
20
 
12
- def id
13
- return page[/\d+/]
14
- end
15
-
16
- def url
17
- return page
18
21
  end
19
22
 
20
23
  end
@@ -1,76 +1,80 @@
1
- class Entry
2
-
3
- attr_reader :number
4
- attr_reader :link
5
- attr_reader :voting
6
- attr_reader :user
7
-
8
- def initialize(number, link, voting, user, comments, time)
9
- @number = number
10
- @link = link
11
- @voting = voting
12
- @user = user
13
- @time = time
14
- @comments_info = comments
15
- end
1
+ module RubyHackernews
2
+
3
+ class Entry
4
+
5
+ attr_reader :number
6
+ attr_reader :link
7
+ attr_reader :voting
8
+ attr_reader :user
9
+
10
+ def initialize(number, link, voting, user, comments, time)
11
+ @number = number
12
+ @link = link
13
+ @voting = voting
14
+ @user = user
15
+ @time = time
16
+ @comments_info = comments
17
+ end
16
18
 
17
- def comments
18
- unless @comments
19
- @comments = CommentService.new.get_comments(@comments_info.page)
19
+ def comments
20
+ unless @comments
21
+ @comments = CommentService.new.get_comments(@comments_info.page)
22
+ end
23
+ return @comments
20
24
  end
21
- return @comments
22
- end
23
25
 
24
- def self.all(pages = 1)
25
- return EntryService.new.get_entries(pages)
26
- end
26
+ def self.all(pages = 1)
27
+ return EntryService.new.get_entries(pages)
28
+ end
27
29
 
28
- def self.newest(pages = 1)
29
- return EntryService.new.get_new_entries(pages)
30
- end
30
+ def self.newest(pages = 1)
31
+ return EntryService.new.get_new_entries(pages)
32
+ end
31
33
 
32
- def self.questions(pages = 1)
33
- return EntryService.new.get_questions(pages)
34
- end
34
+ def self.questions(pages = 1)
35
+ return EntryService.new.get_questions(pages)
36
+ end
35
37
 
36
- def self.jobs(pages = 1)
37
- return EntryService.new.get_jobs(pages)
38
- end
38
+ def self.jobs(pages = 1)
39
+ return EntryService.new.get_jobs(pages)
40
+ end
39
41
 
40
- def self.find(id)
41
- return EntryService.new.find_by_id(id)
42
- end
42
+ def self.find(id)
43
+ return EntryService.new.find_by_id(id)
44
+ end
43
45
 
44
- def time
45
- return @time.time
46
- end
46
+ def time
47
+ return @time.time
48
+ end
47
49
 
48
- def id
49
- return @comments_info ? @comments_info.id : nil
50
- end
51
-
52
- def comments_url
53
- return @comments_info ? ConfigurationService.base_url + @comments_info.url : nil
54
- end
50
+ def id
51
+ return @comments_info ? @comments_info.id : nil
52
+ end
53
+
54
+ def comments_url
55
+ return @comments_info ? ConfigurationService.base_url + @comments_info.url : nil
56
+ end
55
57
 
56
- def comments_count
57
- return @comments_info.count unless @comments_info.nil?
58
- end
58
+ def comments_count
59
+ return @comments_info.count unless @comments_info.nil?
60
+ end
59
61
 
60
- def write_comment(text)
61
- return CommentService.new.write_comment(@comments_info.page, text)
62
- end
62
+ def write_comment(text)
63
+ return CommentService.new.write_comment(@comments_info.page, text)
64
+ end
63
65
 
64
- def self.submit(title, url)
65
- return EntryService.new.submit(title, url)
66
- end
66
+ def self.submit(title, url)
67
+ return EntryService.new.submit(title, url)
68
+ end
67
69
 
68
- def self.ask(title, text)
69
- return EntryService.new.ask(title, text)
70
- end
70
+ def self.ask(title, text)
71
+ return EntryService.new.ask(title, text)
72
+ end
73
+
74
+ def upvote
75
+ return VotingService.new.vote(@voting.upvote)
76
+ end
71
77
 
72
- def upvote
73
- return VotingService.new.vote(@voting.upvote)
74
78
  end
75
79
 
76
- end
80
+ end
@@ -1,14 +1,17 @@
1
+ module RubyHackernews
1
2
 
2
- class LinkInfo
3
+ class LinkInfo
3
4
 
4
- attr_reader :title
5
- attr_reader :href
6
- attr_reader :site
5
+ attr_reader :title
6
+ attr_reader :href
7
+ attr_reader :site
8
+
9
+ def initialize(title, href, site)
10
+ @title = title
11
+ @href = href
12
+ @site = site
13
+ end
7
14
 
8
- def initialize(title, href, site)
9
- @title = title
10
- @href = href
11
- @site = site
12
15
  end
13
16
 
14
- end
17
+ end
@@ -1,22 +1,25 @@
1
+ module RubyHackernews
1
2
 
2
- class TimeInfo
3
+ class TimeInfo
3
4
 
4
- SECOND = 1
5
- MINUTE = 60 * SECOND
6
- HOUR = 60 * MINUTE
7
- DAY = 24 * HOUR
5
+ SECOND = 1
6
+ MINUTE = 60 * SECOND
7
+ HOUR = 60 * MINUTE
8
+ DAY = 24 * HOUR
8
9
 
9
- def time
10
- return Time.now - @unit_of_measure * @value
11
- end
10
+ def time
11
+ return Time.now - @unit_of_measure * @value
12
+ end
12
13
 
13
- def initialize(value, unit_of_measure)
14
- @value = value
15
- if(unit_of_measure)
16
- descriptor = unit_of_measure[unit_of_measure.length - 1].chr == "s" ? unit_of_measure[0..unit_of_measure.length - 2] : unit_of_measure
17
- @unit_of_measure = self.class.const_get(descriptor.upcase)
14
+ def initialize(value, unit_of_measure)
15
+ @value = value
16
+ if(unit_of_measure)
17
+ descriptor = unit_of_measure[unit_of_measure.length - 1].chr == "s" ? unit_of_measure[0..unit_of_measure.length - 2] : unit_of_measure
18
+ @unit_of_measure = self.class.const_get(descriptor.upcase)
19
+ end
18
20
  end
19
- end
20
21
 
21
22
 
22
- end
23
+ end
24
+
25
+ end
@@ -1,12 +1,15 @@
1
+ module RubyHackernews
1
2
 
2
- class UserInfo
3
+ class UserInfo
3
4
 
4
- attr_reader :name
5
- attr_reader :page
5
+ attr_reader :name
6
+ attr_reader :page
6
7
 
7
- def initialize(name, page)
8
- @name = name
9
- @page = page
10
- end
8
+ def initialize(name, page)
9
+ @name = name
10
+ @page = page
11
+ end
11
12
 
12
- end
13
+ end
14
+
15
+ end
@@ -1,14 +1,17 @@
1
+ module RubyHackernews
1
2
 
2
- class VotingInfo
3
+ class VotingInfo
3
4
 
4
- attr_reader :score
5
- attr_reader :upvote
6
- attr_reader :downvote
5
+ attr_reader :score
6
+ attr_reader :upvote
7
+ attr_reader :downvote
8
+
9
+ def initialize(score, upvote, downvote)
10
+ @score = score
11
+ @upvote = upvote
12
+ @downvote = downvote
13
+ end
7
14
 
8
- def initialize(score, upvote, downvote)
9
- @score = score
10
- @upvote = upvote
11
- @downvote = downvote
12
15
  end
13
16
 
14
- end
17
+ end
@@ -1,34 +1,38 @@
1
1
 
2
- class User
2
+ module RubyHackernews
3
3
 
4
- attr_reader :name
4
+ class User
5
5
 
6
- def initialize(name)
7
- @name = name
8
- end
6
+ attr_reader :name
9
7
 
10
- def login(password)
11
- return LoginService.new.login(@name, password)
12
- end
8
+ def initialize(name)
9
+ @name = name
10
+ end
13
11
 
14
- def logout
15
- return LoginService.new.logout
16
- end
12
+ def login(password)
13
+ return LoginService.new.login(@name, password)
14
+ end
17
15
 
18
- def signup(password)
19
- return SignupService.new.signup(@name, password)
20
- end
21
-
22
- def submissions(pages = 1)
23
- return UserInfoService.new.submissions(@name, pages)
24
- end
25
-
26
- def saved(pages = 1)
27
- return UserInfoService.new.saved(@name, pages)
28
- end
29
-
30
- def comments(pages = 1)
31
- return UserInfoService.new.comments(@name, pages)
16
+ def logout
17
+ return LoginService.new.logout
18
+ end
19
+
20
+ def signup(password)
21
+ return SignupService.new.signup(@name, password)
22
+ end
23
+
24
+ def submissions(pages = 1)
25
+ return UserInfoService.new.submissions(@name, pages)
26
+ end
27
+
28
+ def saved(pages = 1)
29
+ return UserInfoService.new.saved(@name, pages)
30
+ end
31
+
32
+ def comments(pages = 1)
33
+ return UserInfoService.new.comments(@name, pages)
34
+ end
35
+
32
36
  end
33
-
34
- end
37
+
38
+ end