Class: Koala::UploadableIO

Inherits:
Object
  • Object
show all
Defined in:
lib/koala/uploadable_io.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_path_or_mixed, content_type = nil, filename = nil) ⇒ UploadableIO

Returns a new instance of UploadableIO.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/koala/uploadable_io.rb', line 7

def initialize(io_or_path_or_mixed, content_type = nil, filename = nil)
  # see if we got the right inputs
  if content_type.nil?
    parse_init_mixed_param io_or_path_or_mixed
  elsif !content_type.nil? && (io_or_path_or_mixed.respond_to?(:read) or io_or_path_or_mixed.kind_of?(String))
    @io_or_path = io_or_path_or_mixed
    @content_type = content_type
  end
  
  # Probably a StringIO or similar object, which won't work with Typhoeus
  @requires_base_http_service = @io_or_path.respond_to?(:read) && !@io_or_path.kind_of?(File)

  # filename is used in the Ads API
  @filename = filename || "koala-io-file.dum"

  raise KoalaError.new("Invalid arguments to initialize an UploadableIO") unless @io_or_path
  raise KoalaError.new("Unable to determine MIME type for UploadableIO") if !@content_type && Koala.multipart_requires_content_type?
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



5
6
7
# File 'lib/koala/uploadable_io.rb', line 5

def content_type
  @content_type
end

#io_or_pathObject (readonly)

Returns the value of attribute io_or_path.



5
6
7
# File 'lib/koala/uploadable_io.rb', line 5

def io_or_path
  @io_or_path
end

#requires_base_http_serviceObject (readonly)

Returns the value of attribute requires_base_http_service.



5
6
7
# File 'lib/koala/uploadable_io.rb', line 5

def requires_base_http_service
  @requires_base_http_service
end

Class Method Details

.binary_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/koala/uploadable_io.rb', line 34

def self.binary_content?(content)
  content.is_a?(UploadableIO) || DETECTION_STRATEGIES.detect {|method| send(method, content)}
end

Instance Method Details

#to_fileObject



30
31
32
# File 'lib/koala/uploadable_io.rb', line 30

def to_file
  @io_or_path.is_a?(String) ? File.open(@io_or_path) : @io_or_path
end

#to_upload_ioObject



26
27
28
# File 'lib/koala/uploadable_io.rb', line 26

def to_upload_io
  UploadIO.new(@io_or_path, @content_type, @filename)
end