<%= link_to 'Search New York Times articles', root_path %>

<%= form_tag search_path, method: 'get', role: 'search' do %>
<%= text_field_tag :q, params[:q], class: 'form-control', placeholder: 'Search...' %>
<% params.slice(:a, :c, :s).each do |name, value| %> <%= hidden_field_tag name, value %> <% end %>

Displaying <%= (params[:page] || 1).to_i.ordinalize %> page with <%= @articles.size %> articles of total <%= @articles.total %>

<% end %>
<% if @articles.size < 1 && (suggestions = @articles.response.response['suggest']) && suggestions.present? %>

No documents have been found. Maybe you mean <%= suggestions.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq.map do |term| link_to term, search_path(params.merge q: term) end.to_sentence(last_word_connector: ' or ').html_safe %>?

<% end %>
<% unless @articles.size < 1 %>

<%= link_to 'All Sections →'.html_safe, search_path(params.merge(c: nil))%>

<% @articles.response.response['facets']['categories']['terms'].each do |c| %> <%= link_to search_path(params.merge(c: c['term'])), class: "list-group-item#{' active' if params[:c] == c['term']}" do c['term'].titleize.html_safe + content_tag(:small, c['count'], class: 'badge').html_safe end %> <% end %>

<%= link_to 'All Authors →'.html_safe, search_path(params.merge(a: nil))%>

<% @articles.response.response['facets']['authors']['terms'].each do |a| %> <%= link_to search_path(params.merge(a: a['term'])), class: "list-group-item#{' active' if params[:a] == a['term']}" do a['term'].titleize.html_safe + content_tag(:small, a['count'], class: 'badge').html_safe end %> <% end %>

<%= link_to 'Any Date →'.html_safe, search_path(params.merge(w: nil))%>

<% @articles.response.response['facets']['published']['entries'].each do |w| %> <%= __start = Time.at(w['time']/1000) __end = __start.end_of_week __date = __start.to_date.to_s(:iso) link_to search_path(params.merge(w: __date)), class: "list-group-item#{' active' if params[:w] == __date}" do "#{__start.to_date.to_s(:short)} — #{__end.to_date.to_s(:short)}".html_safe + \ content_tag(:small, w['count'], class: 'badge').html_safe end %> <% end %>
<% end %>
<% @articles.each do |article| %>

<%= (article.try(:highlight).try(:title) ? article.highlight.title.join.html_safe : article.title) %> <%= article.categories.to_sentence %>

<% if article.try(:highlight).try(:abstract) %> <%= article.highlight.abstract.join.html_safe %> <% else %> <%= article.try(:highlight).try(:content) ? article.highlight.content.join('…').html_safe : article.abstract %> <% end %>

<% if comments = article.try(:highlight) && article.highlight['comments.body'] %>

Comments: <%= comments.join('…').html_safe %>

<% end %>

Authors: <%= article.authors.map(&:full_name).to_sentence %> | Published: <%= article.published_on %> | Score: <%= article._score %>

<% end %>