Class: CatGenerator::Photo
- Inherits:
-
Object
- Object
- CatGenerator::Photo
- Defined in:
- lib/cat_generator/photo.rb
Constant Summary collapse
- CAT_PHOTOS_API =
'http://thecatapi.com/api/images/get'
Class Method Summary collapse
-
.open_in_browser ⇒ Object
opens the photo in the default browser.
-
.url ⇒ Object
returns the next photo url from thecatapi.com.
-
.write_to_desktop ⇒ Object
downloads the next photo and saves to Desktop.
Class Method Details
.open_in_browser ⇒ Object
opens the photo in the default browser
29 30 31 32 |
# File 'lib/cat_generator/photo.rb', line 29 def self.open_in_browser photo_url = url `open #{photo_url}` if photo_url end |
.url ⇒ Object
returns the next photo url from thecatapi.com
6 7 8 9 10 11 12 |
# File 'lib/cat_generator/photo.rb', line 6 def self.url response = Excon.get(CAT_PHOTOS_API) if response.status == 302 response.headers["location"] end end |
.write_to_desktop ⇒ Object
downloads the next photo and saves to Desktop
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cat_generator/photo.rb', line 15 def self.write_to_desktop photo_url = url if photo_url filename = photo_url.split('/').last target = File.("~/Desktop/#{filename}") File.open(target, 'w') do |file| file.write(Excon.get(photo_url).body) end end end |