Class: Filebin

Inherits:
Object
  • Object
show all
Includes:
UrlShort
Defined in:
lib/filebin.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Filebin

This method takes a hash containing the variables in the POST request.

fbin = Filebin.new({ "path" => "file_path"})


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/filebin.rb', line 19

def initialize(options)
    options["MAX_FILE_SIZE"] = "82428800"

    # Get UPLOAD_IDENTIFIER
    clnt = HTTPClient.new
    res = clnt.get('http://filebin.ca').content
    doc = Hpricot(res)
    options["UPLOAD_IDENTIFIER"] = doc.at("//div[@id='form']/fieldset/form/input[2]")['value']

    # Upload file
    File.open(options["path"]) do |file|
        options["file"] = file
        options.delete("path")
        clnt = HTTPClient.new
        res = clnt.post('http://filebin.ca/upload.php', options).content
        doc = Hpricot(res)
    end

    doc.inner_html.match(/file is available at (.*)\n/)
    @link = $1
end

Instance Method Details

Returns the link to the uploaded file.



42
43
44
# File 'lib/filebin.rb', line 42

def link
    @link
end

Returns a shortened link to the uploaded file.



47
48
49
# File 'lib/filebin.rb', line 47

def short_link
    UrlShort::IsGd.shorten @link
end