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.
- data/README.rdoc +4 -0
- data/Rakefile +1 -1
- data/lib/ruby-hackernews/domain/comment/comment.rb +44 -41
- data/lib/ruby-hackernews/domain/entry/comments_info.rb +16 -13
- data/lib/ruby-hackernews/domain/entry/entry.rb +64 -60
- data/lib/ruby-hackernews/domain/entry/link_info.rb +12 -9
- data/lib/ruby-hackernews/domain/entry/time_info.rb +18 -15
- data/lib/ruby-hackernews/domain/entry/user_info.rb +11 -8
- data/lib/ruby-hackernews/domain/entry/voting_info.rb +12 -9
- data/lib/ruby-hackernews/domain/user.rb +31 -27
- data/lib/ruby-hackernews/services/comment_service.rb +54 -51
- data/lib/ruby-hackernews/services/configuration_service.rb +25 -22
- data/lib/ruby-hackernews/services/entry_service.rb +53 -51
- data/lib/ruby-hackernews/services/login_service.rb +23 -20
- data/lib/ruby-hackernews/services/mechanize_context.rb +23 -19
- data/lib/ruby-hackernews/services/not_authenticated_error.rb +8 -5
- data/lib/ruby-hackernews/services/parsers/comments_info_parser.rb +16 -11
- data/lib/ruby-hackernews/services/parsers/entry_page_parser.rb +20 -17
- data/lib/ruby-hackernews/services/parsers/entry_parser.rb +25 -22
- data/lib/ruby-hackernews/services/parsers/link_info_parser.rb +14 -12
- data/lib/ruby-hackernews/services/parsers/time_info_parser.rb +13 -11
- data/lib/ruby-hackernews/services/parsers/user_info_parser.rb +15 -11
- data/lib/ruby-hackernews/services/parsers/voting_info_parser.rb +16 -12
- data/lib/ruby-hackernews/services/signup_service.rb +15 -12
- data/lib/ruby-hackernews/services/user_info_service.rb +21 -18
- data/lib/ruby-hackernews/services/voting_service.rb +10 -7
- 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
@@ -1,54 +1,57 @@
|
|
1
|
+
module RubyHackernews
|
1
2
|
|
2
|
-
class Comment
|
3
|
-
|
3
|
+
class Comment
|
4
|
+
include Enumerable
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
attr_reader :text
|
7
|
+
attr_reader :voting
|
8
|
+
attr_reader :user
|
8
9
|
|
9
|
-
|
10
|
+
attr_accessor :parent
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def <<(comment)
|
21
|
+
comment.parent = self
|
22
|
+
@children << comment
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def each(&block)
|
26
|
+
@children.each(&block)
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def <=>(other_comment)
|
30
|
+
return other_comment.voting.score <=> @voting.score
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def method_missing(method, *args, &block)
|
34
|
+
@children.send(method, *args, &block)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
def self.newest(pages = 1)
|
38
|
+
return CommentService.new.get_new_comments(pages)
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def upvote
|
48
|
+
VotingService.new.vote(@voting.upvote)
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
5
|
-
|
5
|
+
attr_accessor :count
|
6
|
+
attr_reader :page
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
26
|
+
def self.all(pages = 1)
|
27
|
+
return EntryService.new.get_entries(pages)
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
def self.newest(pages = 1)
|
31
|
+
return EntryService.new.get_new_entries(pages)
|
32
|
+
end
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
-
|
34
|
+
def self.questions(pages = 1)
|
35
|
+
return EntryService.new.get_questions(pages)
|
36
|
+
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
def self.jobs(pages = 1)
|
39
|
+
return EntryService.new.get_jobs(pages)
|
40
|
+
end
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
def self.find(id)
|
43
|
+
return EntryService.new.find_by_id(id)
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
def time
|
47
|
+
return @time.time
|
48
|
+
end
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
58
|
+
def comments_count
|
59
|
+
return @comments_info.count unless @comments_info.nil?
|
60
|
+
end
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
-
|
62
|
+
def write_comment(text)
|
63
|
+
return CommentService.new.write_comment(@comments_info.page, text)
|
64
|
+
end
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
66
|
+
def self.submit(title, url)
|
67
|
+
return EntryService.new.submit(title, url)
|
68
|
+
end
|
67
69
|
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
SECOND = 1
|
6
|
+
MINUTE = 60 * SECOND
|
7
|
+
HOUR = 60 * MINUTE
|
8
|
+
DAY = 24 * HOUR
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def time
|
11
|
+
return Time.now - @unit_of_measure * @value
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
5
|
-
|
5
|
+
attr_reader :name
|
6
|
+
attr_reader :page
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
2
|
+
module RubyHackernews
|
3
3
|
|
4
|
-
|
4
|
+
class User
|
5
5
|
|
6
|
-
|
7
|
-
@name = name
|
8
|
-
end
|
6
|
+
attr_reader :name
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
def login(password)
|
13
|
+
return LoginService.new.login(@name, password)
|
14
|
+
end
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|