Class: CatGenerator::Photo

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

Constant Summary collapse

CAT_PHOTOS_API =
'http://thecatapi.com/api/images/get'

Class Method Summary collapse

Class Method Details

.open_in_browserObject

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

.urlObject

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_desktopObject

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.expand_path("~/Desktop/#{filename}")

    File.open(target, 'w') do |file|
      file.write(Excon.get(photo_url).body)
    end
  end
end