Module: HackerNews::StoryList
- Defined in:
- lib/hnruby/storylist.rb
Overview
Represents an opaque list of all stories on Hacker News.
Each story is cached the first time it’s accessed (or the first time the page it’s on is accessed), so they may become out of date quickly. If you need to refresh constantly, call StoryList.update before retrieving any stories.
story = HackerNews::StoryList[1]
sleep 300
story == HackerNews::StoryList[1] #=> true
HackerNews::StoryList.update
story == HackerNews::StoryList[1] #=> false
Constant Summary collapse
- @@items =
{}
- @@page_urls =
sketchiness ensues
{}
Class Method Summary collapse
-
.[](i) ⇒ Object
If
index
is a number, returns a NewsItem representing the story currently atindex
. -
.at(i) ⇒ Object
Returns a NewsItem representing the story currently at
index
. -
.front_page ⇒ Object
Returns an array of the stories currently on the front page.
-
.get_by(count = 1) ⇒ Object
Returns (up to) the first
count
NewsItems that returntrue
in the accompanying block— if no argument is given, returns the first such NewsItem. -
.page(index) ⇒ Object
Returns an array of all the stories on the
index
th page. -
.stories(range) ⇒ Object
Returns all the stories with ranks in
range
. -
.update ⇒ Object
Clear story cache— any actual NewsItem objects will still need to be updated, though.
Class Method Details
.[](i) ⇒ Object
If index
is a number, returns a NewsItem representing the story currently at index
.
If index
is a range, return all stories with ranks in index
.
s = HackerNews::StoryList[1] \
#=> "DMCA takedown on GPL-copyrighted code" <article: 6623329>
s = HackerNews::StoryList[2..3] \
#=> ["C--" <article: 6621679>, "Profitless Prosperity" <article: 6622394>]
:arg: index
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/hnruby/storylist.rb', line 58 def self.[](i) case i when Numeric at(i.to_i) when Enumerable stories(i) else raise TypeError, "#{i.class} objects cannot be used to access stories" end end |
.at(i) ⇒ Object
Returns a NewsItem representing the story currently at index
. Equivalent to HackerNews::StoryList[index]
. :arg: index
72 73 74 75 76 |
# File 'lib/hnruby/storylist.rb', line 72 def self.at(i) update_page ((i - 1) / 30) + 1 unless @@items[i] @@items[i] end |
.front_page ⇒ Object
Returns an array of the stories currently on the front page.
84 85 86 |
# File 'lib/hnruby/storylist.rb', line 84 def self.front_page (1..30).map{ |i| at i } end |
.get_by(count = 1) ⇒ Object
Returns (up to) the first count
NewsItems that return true
in the accompanying block— if no argument is given, returns the first such NewsItem.
May return fewer than count
items, if a smaller number than are requested can be found.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hnruby/storylist.rb', line 100 def self.get_by(count = 1) i = 0 items = [] until items.count == count i += 1 items << at(i) if yield at(i) end items rescue OpenURI::HTTPError #NOTE: May be unnecessary since page-loading's fixed items end |
.page(index) ⇒ Object
Returns an array of all the stories on the index
th page.
79 80 81 |
# File 'lib/hnruby/storylist.rb', line 79 def self.page(index) ((((index - 1) * 30) + 1)..(index * 30)).map{ |i| at i } end |
.stories(range) ⇒ Object
Returns all the stories with ranks in range
. Equivalent to HackerNews::StoryList[range]
.
90 91 92 |
# File 'lib/hnruby/storylist.rb', line 90 def self.stories(range) range.map{ |i| at i } end |
.update ⇒ Object
Clear story cache— any actual NewsItem objects will still need to be updated, though.
41 42 43 44 |
# File 'lib/hnruby/storylist.rb', line 41 def self.update @@items.clear @@page_urls.clear end |