Class: HackerTerm::PageData
- Inherits:
-
Object
- Object
- HackerTerm::PageData
- Defined in:
- lib/hacker_term/page_data.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#line_pos ⇒ Object
readonly
Returns the value of attribute line_pos.
-
#mean_score ⇒ Object
readonly
Returns the value of attribute mean_score.
-
#median_score ⇒ Object
readonly
Returns the value of attribute median_score.
-
#mode_score ⇒ Object
readonly
Returns the value of attribute mode_score.
-
#sorted_by ⇒ Object
readonly
Returns the value of attribute sorted_by.
Instance Method Summary collapse
- #change_line_pos(direction) ⇒ Object
-
#initialize(data) ⇒ PageData
constructor
A new instance of PageData.
- #selected_comments_url ⇒ Object
- #selected_url ⇒ Object
- #sort_on!(mode) ⇒ Object
- #unescape_titles! ⇒ Object
Constructor Details
#initialize(data) ⇒ PageData
Returns a new instance of PageData.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hacker_term/page_data.rb', line 15 def initialize(data) begin @data = JSON.parse(data)['items'] rescue JSON::ParserError raise "JSON appears to be malformed: #{unescaped}" # Bomb out for now... end add_missing_keys! format_numbers! format_urls! unescape_titles! calculate_mean_score calculate_median_score calculate_mode_score @sorted_by = 'RANK' @line_pos = 1 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/hacker_term/page_data.rb', line 13 def data @data end |
#line_pos ⇒ Object (readonly)
Returns the value of attribute line_pos.
13 14 15 |
# File 'lib/hacker_term/page_data.rb', line 13 def line_pos @line_pos end |
#mean_score ⇒ Object (readonly)
Returns the value of attribute mean_score.
13 14 15 |
# File 'lib/hacker_term/page_data.rb', line 13 def mean_score @mean_score end |
#median_score ⇒ Object (readonly)
Returns the value of attribute median_score.
13 14 15 |
# File 'lib/hacker_term/page_data.rb', line 13 def median_score @median_score end |
#mode_score ⇒ Object (readonly)
Returns the value of attribute mode_score.
13 14 15 |
# File 'lib/hacker_term/page_data.rb', line 13 def mode_score @mode_score end |
#sorted_by ⇒ Object (readonly)
Returns the value of attribute sorted_by.
13 14 15 |
# File 'lib/hacker_term/page_data.rb', line 13 def sorted_by @sorted_by end |
Instance Method Details
#change_line_pos(direction) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/hacker_term/page_data.rb', line 56 def change_line_pos(direction) if direction == :up @line_pos += 1 unless @line_pos == @data.length elsif direction == :down @line_pos -= 1 unless @line_pos == 1 elsif direction == :reset @line_pos = 1 end end |
#selected_comments_url ⇒ Object
70 71 72 |
# File 'lib/hacker_term/page_data.rb', line 70 def selected_comments_url "http://news.ycombinator.com/item?id=" + @data[@line_pos - 1]['item_id'] end |
#selected_url ⇒ Object
66 67 68 |
# File 'lib/hacker_term/page_data.rb', line 66 def selected_url @data[@line_pos - 1]['url'] end |
#sort_on!(mode) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hacker_term/page_data.rb', line 39 def sort_on!(mode) case mode when :score @data = @data.sort_by { |a| -a['score'].to_f } # desc when :comments @data = @data.sort_by { |a| -a['comments'].to_f } # desc when :rank @data = @data.sort_by { |a| a['rank'].to_f } when :title @data = @data.sort_by { |a| a['title'].upcase } else throw "Sorting mode #{mode} not supported!" end @sorted_by = mode.to_s.upcase end |
#unescape_titles! ⇒ Object
35 36 37 |
# File 'lib/hacker_term/page_data.rb', line 35 def unescape_titles! @data.each { |row| row['title'] = CGI.unescapeHTML(row['title']) } end |