Class: Fleakr::Objects::Image

Inherits:
Object
  • Object
show all
Includes:
Support::Object
Defined in:
lib/fleakr/objects/image.rb

Overview

Image

This class wraps the functionality for saving remote images to disk. It’s called by the Fleakr::Objects::Photo class to save an image with a specific size and would typically never be called directly.

Example:

user = Fleakr.user('brownout')
user.photos.first.small.save_to('/tmp')

Attributes

size

The name of this image’s size (e.g. Square, Large, etc…)

width

The width of this image

height

The height of this image

url

The direct URL for this image

page

The page on Flickr that represents this photo

Instance Method Summary collapse

Methods included from Support::Object

included

Instance Method Details

#filenameObject

The filename portion of the image (without the full URL)



35
36
37
# File 'lib/fleakr/objects/image.rb', line 35

def filename
  self.url.match(/([^\/]+)$/)[1]
end

#save_to(target, prefix = nil) ⇒ Object

Save this image to the specified directory or file. If the target is a directory, the file will be created with the original filename from Flickr.

If the target is a file, it will be saved with the specified name. In the case that the target file already exists, this method will overwrite it.



43
44
45
46
# File 'lib/fleakr/objects/image.rb', line 43

def save_to(target, prefix = nil)
  destination = File.directory?(target) ? "#{target}/#{prefix}#{self.filename}" : "#{target}"
  File.open(destination, 'w') {|f| f << Net::HTTP.get(URI.parse(self.url)) }
end