Module: G::Commands::Headline

Defined in:
lib/g.rb

Class Method Summary collapse

Class Method Details

.list(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/g.rb', line 21

def self.list(params = {})
	search_params = { 
		q: params[:q],
		fields: ["headline", "shortUrl"], 
		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(/.{#{60}}|.+/).map { |x| x.strip }.join("-\n")
			item.webUrl = item.fields.shortUrl

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

	puts table
end

.pretify(item) ⇒ Object



49
50
51
# 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