module G::Commands::Headline

Public Class Methods

list(params = {}) click to toggle source
# File lib/g.rb, line 21
def self.list(params = {})
        search_params = { 
                q: params[:q],
                fields: ["headline"], 
                date: { 
                        from: params[:from] || Time.now, 
                        to: params[:to] || Time.now 
                },
                page: params[:page],
                per_page: params[:per_page]
        }

        items = Theguardian::ContentApi.search(search_params).items
        
        table = Terminal::Table.new(headings: ["Date", "Headline", "Url"]) do |t|
                items.each do |item| 
                        item.pub_date = Time.parse(item.webPublicationDate).strftime("%Y-%m-%d %H:%M")
                        item.headline = item.fields.headline.scan(/.{#{40}}|.+/).map { |x| x.strip }.join("-\n")
                        item.webUrl = item.webUrl.scan(/.{#{60}}|.+/).map { |x| x.strip }.join("\n")

                        t << [item.pub_date, item.headline, item.webUrl]
                        t << :separator
                end
        end

        puts table
end
pretify(item) click to toggle source
# File lib/g.rb, line 49
def self.pretify(item)
        puts "#{Time.parse(item.webPublicationDate).strftime("%Y-%m-%d %H:%M")} - #{item.fields.headline} - #{item.webUrl}"
end