Class: Fleakr::Objects::Image
- Inherits:
-
Object
- Object
- Fleakr::Objects::Image
- 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
-
#filename ⇒ Object
The filename portion of the image (without the full URL).
-
#save_to(target, prefix = nil) ⇒ Object
Save this image to the specified directory or file.
Methods included from Support::Object
Instance Method Details
#filename ⇒ Object
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 |