Class: Fleakr::Api::FileParameter

Inherits:
Object
  • 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 collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, filename) ⇒ FileParameter

Create a parameter with name and specified filename


20
21
22
23
# File 'lib/fleakr/api/file_parameter.rb', line 20

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


16
17
18
# File 'lib/fleakr/api/file_parameter.rb', line 16

def name
  @name
end

Instance Method Details

#mime_typeObject

Discover MIME type by file extension using MIME_TYPES constant


27
28
29
# File 'lib/fleakr/api/file_parameter.rb', line 27

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

#to_formObject

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


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

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


33
34
35
# File 'lib/fleakr/api/file_parameter.rb', line 33

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