Class: Ngphoto

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

Constant Summary collapse

NG_PHOTO_OF_THE_DAY_URL =
"http://photography.nationalgeographic.com/photography/photo-of-the-day/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, url) ⇒ Ngphoto

Returns a new instance of Ngphoto.



10
11
12
13
# File 'lib/ngphoto.rb', line 10

def initialize(title, url)
  @title = Time.now.strftime('%Y-%m-%d-') + title.split('--')[0].strip.gsub(' ', '-').downcase
  @url   = url
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/ngphoto.rb', line 8

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/ngphoto.rb', line 8

def url
  @url
end

Class Method Details

.fetchObject



19
20
21
22
23
24
25
26
# File 'lib/ngphoto.rb', line 19

def self.fetch
  doc = Nokogiri::HTML(open(NG_PHOTO_OF_THE_DAY_URL))
  
  title = doc.css('title').first.text
  url   = doc.css('.download_link a').first[:href]

  Ngphoto.new title, url
end

Instance Method Details

#saveObject



15
16
17
# File 'lib/ngphoto.rb', line 15

def save
  system "curl -o ~/Pictures/ngphoto/#{title}.jpg #{url}"
end