Class: Hayabusa::Http_session::Post_multipart::File_upload

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

Overview

This is the actual returned object for fileuploads. It is able to do various user-friendly things like save the content to a given path, return the filename, returns the content to a string and more.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ File_upload

Returns a new instance of File_upload.



149
150
151
# File 'lib/hayabusa_http_session_post_multipart.rb', line 149

def initialize(args)
  @args = args
end

Instance Method Details

#filenameObject

Returns the filename given for the fileupload.



164
165
166
# File 'lib/hayabusa_http_session_post_multipart.rb', line 164

def filename
  return @args[:fname]
end

#headersObject

Returns the headers given for the fileupload. Type and more should be here.



169
170
171
# File 'lib/hayabusa_http_session_post_multipart.rb', line 169

def headers
  return @args[:headers]
end

#io(&blk) ⇒ Object

Returns an IO to read from the upload wherever it is a temporary file or a string.



183
184
185
186
187
188
189
# File 'lib/hayabusa_http_session_post_multipart.rb', line 183

def io(&blk)
  if @args[:data].is_a?(StringIO)
    return @args[:data]
  else
    return File.open(@args[:data].path, "r", &blk)
  end
end

#lengthObject

Returns the size of the fileupload.



159
160
161
# File 'lib/hayabusa_http_session_post_multipart.rb', line 159

def length
  return @args[:data].length
end

#save_to(filepath) ⇒ Object

Saves the content of the fileupload to a given path.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/hayabusa_http_session_post_multipart.rb', line 192

def save_to(filepath)
  File.open(filepath, "w") do |fp|
    if @args[:data].is_a?(StringIO)
      fp.write(@args[:data].string)
    else
      #Stream output to avoid using too much memory.
      self.io do |fp_read|
        fp_read.lines do |line|
          fp.write(line)
        end
      end
    end
  end
end

#sizeObject

Returns the size of the upload.



154
155
156
# File 'lib/hayabusa_http_session_post_multipart.rb', line 154

def size
  return @args[:data].length
end

#to_json(*args) ⇒ Object

This methods prevents the object from being converted to JSON. This can make some serious bugs.



208
209
210
# File 'lib/hayabusa_http_session_post_multipart.rb', line 208

def to_json(*args)
  raise "File_upload-objects should not be converted to json."
end

#to_sObject

Returns the content of the file-upload as a string.



174
175
176
177
178
179
180
# File 'lib/hayabusa_http_session_post_multipart.rb', line 174

def to_s
  if @args[:data].is_a?(StringIO)
    return @args[:data].string
  else
    return File.read(@args[:data].path)
  end
end

#unsetObject



212
213
214
# File 'lib/hayabusa_http_session_post_multipart.rb', line 212

def unset
  
end