Class: HTTP::FormData::File

### Home Classes Methods

### In Files

- lib/http/form_data/file.rb

### Parent

Object

### Methods

- ::new
- #size
- #to_s

### Class/Module Index

Quicksearch

- HTTP
- HTTP::FormData
- HTTP::FormData::Error
- HTTP::FormData::File
- HTTP::FormData::Multipart
- HTTP::FormData::Multipart::Param
- HTTP::FormData::Urlencoded

No matching classes.

# HTTP::FormData::File

Represents file form param.

@example Usage with StringIO

    io = StringIO.new "foo bar baz"
    FormData::File.new io, :filename => "foobar.txt"

@example Usage with IO

    File.open "/home/ixti/avatar.png" do |io|
      FormData::File.new io
    end

@example Usage with pathname

    FormData::File.new "/home/ixti/avatar.png"

### Constants

DEFAULT_MIME

  Default MIME type

### Attributes

filename[R]
mime_type[R]

### Public Class Methods

new(file_or_io, opts = {}) click to toggle source

@see DEFAULT_MIME @param [String, StringIO, File] file_or_io Filename or IO instance. @param [to_h] opts @option opts [to_s] :mime_type (DEFAULT_MIME) @option opts [to_s] :filename

    When `file` is a String, defaults to basename of `file`.
    When `file` is a File, defaults to basename of `file`.
    When `file` is a StringIO, defaults to `"stream-{object_id}"`

    # File lib/http/form_data/file.rb, line 33
    def initialize(file_or_io, opts = {})
      @file_or_io = file_or_io

      opts = FormData.ensure_hash opts

      @mime_type  = opts.fetch(:mime_type) { DEFAULT_MIME }
      @filename   = opts.fetch :filename do
        case file_or_io
        when String then ::File.basename file_or_io
        when ::File then ::File.basename file_or_io.path
        else             "stream-#{file_or_io.object_id}"
        end
      end
    end

### Public Instance Methods

size() click to toggle source

Returns content size.

@return [Fixnum]

    # File lib/http/form_data/file.rb, line 51
    def size
      with_io(&:size)
    end

to_s() click to toggle source

Returns content of a file of IO.

@return [String]

    # File lib/http/form_data/file.rb, line 58
    def to_s
      with_io(&:read)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.