hnruby 0.01 → 0.01.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hnruby/comment.rb +37 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dddb492033269661ba8dc31860d12e871be3ab48
4
- data.tar.gz: 24ff007b41e12b0314e27486b244199f6a883280
3
+ metadata.gz: 6f1b2d89d4f5fc0c1382e12b0f61a013a44e82a8
4
+ data.tar.gz: dd86d0841ee82738e9a979d4ae1f087243c6efd7
5
5
  SHA512:
6
- metadata.gz: 95d298a93c691ec7495bb8a4034890561406ef0c54a24184ce5673f157cc129a4f27641fab8725fd1c09e2b4ba38967ec0f06bbc5110e132fd5cdd89e3bd0773
7
- data.tar.gz: 636cf406397b229a70d82a5e92a76d0004ff56f289608700c5481d634c4b8fd36201be719a06209db8e96528f17de77a70511e19229ce7db94d4470597e09c35
6
+ metadata.gz: 80972f3f6816814943d8e1ac34a94a471a530ac5381b7df20bb1d17fa9facf26f4b2d895d1fe94b94a5f43e921e3cee6e666aa8b537540b2298e0836d35f2ec9
7
+ data.tar.gz: f5c38130b920c884734d4bbc7f1855d13aea976b32173b658e4acd9263569359fe0b4fda73a6feb5ffbb2ccc067930c4289b298d7e5f2f7e6f7a5b437645478f
@@ -15,7 +15,7 @@ class Comment
15
15
  # Returns the username of the comment's author.
16
16
  attr_reader :submitter
17
17
  # Returns (as a human-readable string) how long ago the comment was
18
- # submitted.
18
+ # submitted, as of this object's creation.
19
19
  # comment.time #=> "5 minutes ago"
20
20
  attr_reader :time
21
21
  # Returns the actual content of the comment, as a string (with original html
@@ -42,7 +42,7 @@ class Comment
42
42
  @id = (html / "a")[2].attribute("href").content.delete("item?id=").to_i
43
43
  @submitter = (html / "a")[1].content
44
44
  @submitter_url = (html / "a")[1].attribute("href").content
45
- @time = (html / "span")[1].content.split[1..-3].join " "
45
+ @time = (html / "span")[1].content.split[1..-3].join(" ")
46
46
  @content = (html / "font")[-2].inner_html
47
47
  end
48
48
  # :startdoc:
@@ -69,49 +69,77 @@ class Comment
69
69
  def inspect
70
70
  "<Comment> by #{@submitter} at #{@time}"
71
71
  end
72
+
73
+ def <=>(comment)
74
+ left, right = [self, comment].map do |i|
75
+ i.time.split[0].to_i * {
76
+ "minute" => 60, "minutes" => 60,
77
+ "hour" => 3600, "hours" => 3600,
78
+ "day" => 86400, "days" => 86400
79
+ }[i.time.split[1]]
80
+ end
81
+
82
+ -(right <=> left)
83
+ end
72
84
  end
73
85
 
74
86
  # Represents the list of comments pertaining to a particluar HN story.
75
87
  class CommentPage
88
+ include Enumerable
89
+ # Represents the title of
90
+ attr_reader :title
91
+ attr_reader :url
92
+
76
93
  # Returns a new CommentPage corresponding to <tt>url</tt>.
77
94
  # cpage = HackerNews::CommentPage.new\
78
95
  # "https://news.ycombinator.com/item?id=6621679" #=> "C--" <12 Comments>
79
96
  def initialize(url)
80
97
  html = Nokogiri::HTML open(url)
81
98
  @title = html.title.chomp " | Hacker News"
99
+ @url = url
82
100
 
83
101
  html = (html / "tr")[8..-3]
84
102
  @comments = []
85
103
  lineage = []
86
104
  (0...html.length).select{ |i| i.even? }.each do |i|
87
105
  com = Comment.new html[i]
88
- com.parent = lineage[com.depth - 1]
106
+ com.parent = com.depth > 0 ? lineage[com.depth - 1] : nil
89
107
  lineage[com.depth] = com
90
108
  @comments << com
91
109
  end
92
110
  end
93
111
 
112
+ #Passes all comments in +self+ as a block
113
+ def each
114
+ @comments.each { |i| yield i }
115
+ self
116
+ end
117
+
94
118
  # Returns array containing every Comment object within the page.
95
119
  def to_a
96
120
  @comments
97
121
  end
98
122
 
99
- # :arg: index
100
123
  # Returns the Comment object at <tt>index</tt>.
101
- def [](i)
102
- @comments[i]
124
+ def [](index)
125
+ @comments[index]
103
126
  end
104
- # :arg: index
127
+
105
128
  # Returns the Comment object at <tt>index</tt>.
106
129
  # Equivalent to <tt>self[index]</tt>.
107
- def at(i)
108
- @comments[i]
130
+ def at(index)
131
+ @comments[index]
109
132
  end
110
133
 
111
134
  # :nodoc:
112
135
  def inspect
113
136
  "\"#{@title}\" <#{@comments.length} comments>"
114
137
  end
138
+
139
+ # :nodoc:
140
+ def to_s
141
+ inspect
142
+ end
115
143
  end
116
144
 
117
145
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hnruby
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.01'
4
+ version: 0.01.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jem Orgun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-31 00:00:00.000000000 Z
11
+ date: 2013-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-terminfo