Class: Fleakr::Api::FileParameter

Inherits:
Parameter
  • Object
show all
Defined in:
lib/fleakr/api/file_parameter.rb

Overview

FileParameter

Parameter class to encapsulate file data sent to the Flickr upload API

Constant Summary collapse

MIME_TYPES =
{
  '.jpg' => 'image/jpeg',
  '.png' => 'image/png',
  '.gif' => 'image/gif'
}

Instance Attribute Summary

Attributes inherited from Parameter

#name

Instance Method Summary collapse

Methods inherited from Parameter

#<=>, #include_in_signature?

Constructor Details

#initialize(name, filename) ⇒ FileParameter

Create a parameter with name and specified filename



18
19
20
21
# File 'lib/fleakr/api/file_parameter.rb', line 18

def initialize(name, filename)
  @filename = filename
  super(name, false)
end

Instance Method Details

#mime_typeObject

Discover MIME type by file extension using MIME_TYPES constant



25
26
27
# File 'lib/fleakr/api/file_parameter.rb', line 25

def mime_type
  MIME_TYPES[File.extname(@filename)]
end

#to_formObject

Generate a form representation of this file for upload (as multipart/form-data)



37
38
39
40
41
42
# File 'lib/fleakr/api/file_parameter.rb', line 37

def to_form
  "Content-Disposition: form-data; name=\"#{self.name}\"; filename=\"#{@filename}\"\r\n" +
  "Content-Type: #{self.mime_type}\r\n" +
  "\r\n" +
  "#{self.value}\r\n"
end

#valueObject

File data (from @filename) to pass to the Flickr API



31
32
33
# File 'lib/fleakr/api/file_parameter.rb', line 31

def value
  @value ||= File.read(@filename)
end