Class: Fleakr::Api::FileParameter
- 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
Instance Method Summary collapse
-
#initialize(name, filename) ⇒ FileParameter
constructor
Create a parameter with name and specified filename.
-
#mime_type ⇒ Object
Discover MIME type by file extension using MIME_TYPES constant.
-
#to_form ⇒ Object
Generate a form representation of this file for upload (as multipart/form-data).
-
#value ⇒ Object
File data (from @filename) to pass to the Flickr API.
Methods inherited from Parameter
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_type ⇒ Object
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_form ⇒ Object
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 |
#value ⇒ Object
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 |