Class: Hackernews::HomeController

Inherits:
ApplicationController show all
Defined in:
app/controllers/home_controller.rb

Constant Summary collapse

FEEDS =
{
  "top" => {title: "Top Stories", screen: :root},
  "new" => {title: "New", screen: :new},
  "best" => {title: "Best", screen: :best},
  "ask" => {title: "Ask HN", screen: :ask},
  "show" => {title: "Show HN", screen: :show},
  "jobs" => {title: "Jobs", screen: :jobs}
}.freeze

Instance Method Summary collapse

Instance Method Details

#askObject



44
45
46
# File 'app/controllers/home_controller.rb', line 44

def ask
  switch_feed("ask")
end

#bestObject



40
41
42
# File 'app/controllers/home_controller.rb', line 40

def best
  switch_feed("best")
end

#close_articleObject



187
188
189
190
191
# File 'app/controllers/home_controller.rb', line 187

def close_article
  home.reading_story_id = nil
  home.article_scroll = 0
  show
end

#current_route?(route) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'app/controllers/home_controller.rb', line 193

def current_route?(route)
  route.name == FEEDS.fetch(home.feed).fetch(:screen)
end

#extract_article_doneObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/home_controller.rb', line 96

def extract_article_done
  value = event.value || {}
  story_id = value[:story_id].to_i
  home.extracting_story_id = nil if home.extracting_story_id.to_i == story_id

  if value[:error]
    home.error = value[:error]
  else
    home.error = ""
    home.articles_by_story_id[story_id] = value.fetch(:article)
    home.reading_story_id = story_id
    home.article_scroll = 0
  end

  show
end

#jobsObject



52
53
54
# File 'app/controllers/home_controller.rb', line 52

def jobs
  switch_feed("jobs")
end

#load_feed_doneObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/home_controller.rb', line 70

def load_feed_done
  value = event.value || {}
  feed = value[:feed] || home.feed
  page = value[:page] || home.page
  key = home.page_key(feed, page)
  current_page = feed == home.feed && page == home.page

  if value[:error]
    home.error = value[:error]
    home.failed_pages[key] = true
  else
    home.error = ""
    home.failed_pages.delete(key)
    home.story_ids_by_feed[value.fetch(:feed)] = value.fetch(:ids)
    home.stories_by_page[key] = value.fetch(:stories)
    home.selected_index = 0 if current_page
  end

  if home.loading_key == key || current_page
    home.loading = false
    home.loading_key = nil
  end

  show
end

#move_downObject



122
123
124
125
126
127
128
129
130
# File 'app/controllers/home_controller.rb', line 122

def move_down
  if home.reading?
    home.article_scroll += 1
  else
    max = [home.current_stories.length - 1, 0].max
    home.selected_index = [home.selected_index + 1, max].min
  end
  show
end

#move_upObject



113
114
115
116
117
118
119
120
# File 'app/controllers/home_controller.rb', line 113

def move_up
  if home.reading?
    home.article_scroll = [home.article_scroll - 1, 0].max
  else
    home.selected_index = [home.selected_index - 1, 0].max
  end
  show
end

#newObject



36
37
38
# File 'app/controllers/home_controller.rb', line 36

def new
  switch_feed("new")
end

#next_pageObject



160
161
162
163
164
165
166
167
# File 'app/controllers/home_controller.rb', line 160

def next_page
  return show if home.reading?
  return show unless more_pages?

  home.page += 1
  home.reset_list_position
  show
end

#open_selected_storyObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/home_controller.rb', line 169

def open_selected_story
  story = home.selected_story
  return show unless story

  if home.articles_by_story_id.key?(story.id)
    home.reading_story_id = story.id
    home.article_scroll = 0
  elsif story.url.to_s.strip.empty?
    home.articles_by_story_id[story.id] = {url: story.article_url, markdown: story.hn_markdown}
    home.reading_story_id = story.id
    home.article_scroll = 0
  else
    start_article_extract(story)
  end

  show
end

#page_downObject



141
142
143
144
145
146
147
148
149
# File 'app/controllers/home_controller.rb', line 141

def page_down
  if home.reading?
    home.article_scroll += 10
  else
    max = [home.current_stories.length - 1, 0].max
    home.selected_index = [home.selected_index + 10, max].min
  end
  show
end

#page_upObject



132
133
134
135
136
137
138
139
# File 'app/controllers/home_controller.rb', line 132

def page_up
  if home.reading?
    home.article_scroll = [home.article_scroll - 10, 0].max
  else
    home.selected_index = [home.selected_index - 10, 0].max
  end
  show
end

#previous_pageObject



151
152
153
154
155
156
157
158
# File 'app/controllers/home_controller.rb', line 151

def previous_page
  return show if home.reading?
  return show if home.page.to_i <= 0

  home.page -= 1
  home.reset_list_position
  show
end

#refreshObject



56
57
58
59
60
61
62
63
# File 'app/controllers/home_controller.rb', line 56

def refresh
  key = home.page_key
  home.stories_by_page.delete(key)
  home.story_ids_by_feed.delete(home.feed)
  home.failed_pages.delete(key)
  start_feed_load(force: true)
  render :show, home: home, palette: command_palette
end

#showObject



31
32
33
34
# File 'app/controllers/home_controller.rb', line 31

def show
  ensure_feed_loaded
  render :show, home: home, palette: command_palette
end

#show_hnObject



48
49
50
# File 'app/controllers/home_controller.rb', line 48

def show_hn
  switch_feed("show")
end

#tick_activityObject



65
66
67
68
# File 'app/controllers/home_controller.rb', line 65

def tick_activity
  home.activity_index += 1 if home.working?
  show
end