Class: HackerTerm::PageData

Inherits:
Object
  • Object
show all
Defined in:
lib/hacker_term/page_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/hacker_term/page_data.rb', line 12

def data
  @data
end

#line_posObject (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_scoreObject (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_scoreObject (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_scoreObject (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_byObject (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_urlObject



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_urlObject



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