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
Constructor Details
#initialize(data) ⇒ PageData
Returns a new instance of PageData.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hacker_term/page_data.rb', line 14 def initialize(data) @data = JSON.parse(data)['items'] add_missing_keys! format_numbers! format_urls! 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.
12 13 14 |
# File 'lib/hacker_term/page_data.rb', line 12 def data @data end |
#line_pos ⇒ Object (readonly)
Returns the value of attribute line_pos.
12 13 14 |
# File 'lib/hacker_term/page_data.rb', line 12 def line_pos @line_pos end |
#mean_score ⇒ Object (readonly)
Returns the value of attribute mean_score.
12 13 14 |
# File 'lib/hacker_term/page_data.rb', line 12 def mean_score @mean_score end |
#median_score ⇒ Object (readonly)
Returns the value of attribute median_score.
12 13 14 |
# File 'lib/hacker_term/page_data.rb', line 12 def median_score @median_score end |
#mode_score ⇒ Object (readonly)
Returns the value of attribute mode_score.
12 13 14 |
# File 'lib/hacker_term/page_data.rb', line 12 def mode_score @mode_score end |
#sorted_by ⇒ Object (readonly)
Returns the value of attribute sorted_by.
12 13 14 |
# File 'lib/hacker_term/page_data.rb', line 12 def sorted_by @sorted_by end |
Instance Method Details
#change_line_pos(direction) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/hacker_term/page_data.rb', line 46 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
60 61 62 |
# File 'lib/hacker_term/page_data.rb', line 60 def selected_comments_url "http://news.ycombinator.com/item?id=" + @data[@line_pos - 1]['item_id'] end |
#selected_url ⇒ Object
56 57 58 |
# File 'lib/hacker_term/page_data.rb', line 56 def selected_url @data[@line_pos - 1]['url'] end |
#sort_on!(mode) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hacker_term/page_data.rb', line 29 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 |