hnruby 0.01.2 → 0.02

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b6d68737114cfb703d4d7007b6719583bc99331
4
- data.tar.gz: 4f93b627eb99063c28fc9a0e870e87d399663e7d
3
+ metadata.gz: f1bb2f5a4c8b8fa3b1b78dc9438863f0506be8a3
4
+ data.tar.gz: dc9ab3e324b16719efd2f5e77734606d40ee76ad
5
5
  SHA512:
6
- metadata.gz: f2404fb955d07e5aa329766ba34d4561e5e04d20aa49930d759a787152e05dfb62401157bd7b15ea3438076fc04c78bf2999f6cf38453f4826b1c0be61ce40d5
7
- data.tar.gz: 3ffe6c85c9e4df51dabf563a270a8c863f26b8238d0ec79179ba5b9e9bda930a032c4843dc912fd265c6da67d4226a4d1d5bf7c09e88f3ebbafab6d1910c81ad
6
+ metadata.gz: 14030d713a3aeb6a065d6515913ae299f70f751ad1ea8d48ce841c4a0bc59fb01203c880c381b41cbd84b667b2624a8ec5d40659f0d05266bd8f2e0c4fa8220e
7
+ data.tar.gz: 7904c487d791b218a9306f8405e1587779d5409c885fd53a6eb4a9025c4460bdb0cb38520e9ba496b1ba6de610df98c950844b43eaa02ddfa0c1d3b287537387
@@ -43,7 +43,7 @@ require 'hnruby/storylist.rb'
43
43
  # From a CommentPage, specific comments can be accessed via index:
44
44
  # comment = cpage[1] #=> <Comment> by user 1 day ago
45
45
  module HackerNews
46
- #Because clearly this is in danger of changing at any moment.
47
- HN_URL = "https://news.ycombinator.com/"
46
+ #Because clearly this is in danger of changing at any moment.
47
+ HN_URL = "https://news.ycombinator.com/"
48
48
 
49
49
  end
@@ -82,12 +82,16 @@ class Comment
82
82
  end
83
83
 
84
84
  # Represents the list of comments pertaining to a particluar HN story.
85
+ #---
86
+ #TODO: Support self-post content, polls, etc.
85
87
  class CommentPage
86
88
  include Enumerable
87
89
  # Returns this comment page's title.
88
90
  attr_reader :title
89
91
  # Returns the URL for this comment page on Hacker News.
90
92
  attr_reader :url
93
+ # Returns the URL for the story corresponding to this comment page.
94
+ attr_reader :story_url
91
95
 
92
96
  # Returns a new CommentPage corresponding to <tt>url</tt>.
93
97
  # cpage = HackerNews::CommentPage.new\
@@ -97,13 +101,14 @@ class CommentPage
97
101
  # the story whose ID is +url+.
98
102
  # cpage = HackerNews::CommentPage.new 6621679 #=> "C--" <12 Comments>
99
103
  def initialize(url)
100
- url = "#{HN_URL}/item?id=#{url.to_i}" if url.is_a? Numeric
104
+ @url = url.is_a?(Numeric) ? "#{HN_URL}item?id=#{url.to_i}" : url
101
105
 
102
- html = Nokogiri::HTML(open(url), nil, "UTF-8")
106
+ html = Nokogiri::HTML(open(@url), nil, "UTF-8")
103
107
  @title = html.title.chomp " | Hacker News"
104
- @url = url
108
+ @story_url = ((html / "tr")[4] / "td")[1].at("a").attribute("href").value
105
109
 
106
- html = (html / "tr")[8..-3]
110
+ html = (html / "tr")[(@story_url.start_with?("item?id=") ? 10 : 8)..-3].
111
+ drop_while { |i| i.children.length != 1 } # Fixes polls and such
107
112
  @comments = []
108
113
  lineage = []
109
114
  (0...html.length).select{ |i| i.even? }.each do |i|
@@ -120,11 +125,6 @@ class CommentPage
120
125
  self
121
126
  end
122
127
 
123
- # Returns array containing every Comment object within the page.
124
- def to_a
125
- @comments
126
- end
127
-
128
128
  # Returns the Comment object at <tt>index</tt>.
129
129
  def [](index)
130
130
  @comments[index]
@@ -136,6 +136,11 @@ class CommentPage
136
136
  @comments[index]
137
137
  end
138
138
 
139
+ # Returns the number of comments in +self+.
140
+ def length
141
+ @comments.length
142
+ end
143
+
139
144
  # :nodoc:
140
145
  def inspect
141
146
  "\"#{@title}\" <#{@comments.length} comments>"
@@ -38,38 +38,32 @@ class NewsItem
38
38
 
39
39
  @number = article.first.content.to_i
40
40
  @url = article[2].at("a").attributes["href"].value
41
- @url = HN_URL + @url if url.start_with? "item?id="
41
+ @url.prepend(HN_URL) if url.start_with? "item?id="
42
42
  @title = article[2].children.first.content
43
43
 
44
44
  if meta.children.length == 5
45
45
  #This is a normal article
46
- @category = :article
47
-
48
- @points = meta[1].children.first.content.to_i
49
- @submitter = meta[1].children[2].content
50
- @time = meta[1].children[3].content.delete("|").strip
51
- @comment_url = meta[1].children[4].attributes["href"].value
52
- @comment_page = nil #Don't needlessly download until asked
53
- @comments = meta[1].children[4].content.to_i
46
+ @points = meta[1].children.first.content.to_i
47
+ @submitter = meta[1].children[2].content
48
+ @time = meta[1].children[3].content.delete("|").strip
49
+ @comment_url = meta[1].children[4].attributes["href"].value
50
+ @comments = meta[1].children[4].content.to_i
54
51
  else
55
52
  #This, by contrast, is a job offer, which lacks all meta-info aside
56
53
  #from submission time
57
- @category = :joboffer
58
-
59
54
  @time = meta[1].content
60
- @points, @submitter, @comment_url, @comment_page, @comments = nil
61
55
  end
62
56
  end
63
57
  # :startdoc:
64
58
 
65
59
  # Returns true if <tt>self</tt> is an article (with points, comments, etc.).
66
60
  def article?
67
- @category == :article
61
+ !@points.nil?
68
62
  end
69
63
  # Returns true if <tt>self</tt> is a job offer (without points, comments,
70
64
  # etc.).
71
65
  def joboffer?
72
- @category == :joboffer
66
+ @points.nil?
73
67
  end
74
68
 
75
69
  # Returns the URL to the HN profile of the submitter.
@@ -85,12 +79,13 @@ class NewsItem
85
79
  def comment_url
86
80
  "#{HN_URL}#{@comment_url}" if @comment_url
87
81
  end
88
- # Returns the CommentPage corresponding to <tt>self</tt>
82
+ # Returns the CommentPage corresponding to <tt>self</tt>.
83
+ #
84
+ # If +self+ is a job offer, returns +nil+.
89
85
  # news_item.comment_page\
90
86
  # #=> "Amazon and the "profitless business model" fallacy" <21 comments>
91
87
  def comment_page
92
- return @comment_page if @comment_page
93
- @comment_page = CommentPage.new comment_url if @comment_url
88
+ @comment_page ||= CommentPage.new comment_url if @comment_url
94
89
  end
95
90
 
96
91
  # :nodoc:
@@ -16,13 +16,20 @@ module HackerNews
16
16
  # story == HackerNews::StoryList[1] #=> false
17
17
  module StoryList
18
18
  @@items = {}
19
+ @@page_urls = {} #sketchiness ensues
19
20
 
20
21
  private
22
+ def self.get_page_url(index)
23
+ update_page(index - 1) unless @@page_urls[index]
24
+ HN_URL + @@page_urls[index]
25
+ end
26
+
21
27
  def self.update_page(index)
22
- url = HN_URL + "#{"news#{index}" if index > 1}"
23
- html = (Nokogiri::HTML(open(url), nil, "UTF-8") / "tr").to_a[4..-6]
28
+ url = (index > 1) ? get_page_url(index) : HN_URL
29
+ html = Nokogiri::HTML(open(url), nil, "UTF-8") / "tr"
30
+ @@page_urls[index+1] = html[-3].at("a").attribute("href").content.sub "/",""
24
31
 
25
- (0...html.length).step(3) do |i|
32
+ (4...(html.length - 6)).step(3) do |i|
26
33
  item = NewsItem.new(html[i], html[i + 1])
27
34
  @@items[item.number] = item
28
35
  end
@@ -33,6 +40,7 @@ public
33
40
  #updated, though.
34
41
  def self.update
35
42
  @@items.clear
43
+ @@page_urls.clear
36
44
  end
37
45
 
38
46
  # If <tt>index</tt> is a number, returns a NewsItem representing the story
@@ -48,10 +56,11 @@ public
48
56
  # #=> ["C--" <article: 6621679>, "Profitless Prosperity" <article: 6622394>]
49
57
  # :arg: index
50
58
  def self.[](i)
51
- if i.is_a? Numeric
52
- self.at i.to_i
53
- elsif i.is_a? Enumerable
54
- self.stories i
59
+ case i
60
+ when Numeric
61
+ at(i.to_i)
62
+ when Enumerable
63
+ stories(i)
55
64
  else
56
65
  raise TypeError, "#{i.class} objects cannot be used to access stories"
57
66
  end
@@ -81,6 +90,23 @@ public
81
90
  def self.stories(range)
82
91
  range.map{ |i| at i }
83
92
  end
93
+
94
+ # Returns (up to) the first +count+ NewsItems that return +true+ in the
95
+ # accompanying block--- if no argument is given, returns the first such
96
+ # NewsItem.
97
+ #
98
+ # May return fewer than +count+ items, if a smaller number than are requested
99
+ # can be found.
100
+ def self.get_by(count = 1)
101
+ i = 0
102
+ items = []
103
+ until items.count == count
104
+ i += 1
105
+ items << at(i) if yield at(i)
106
+ end
107
+ rescue OpenURI::HTTPError #NOTE: May be unnecessary since page-loading's fixed
108
+ items
109
+ end
84
110
  end
85
111
 
86
112
  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.2
4
+ version: '0.02'
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-11-02 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-terminfo