Skip to content

Commit

Permalink
still researching how to upload a file with ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Apr 11, 2009
1 parent 2cd83f4 commit 742ca7e
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/rcapr.rb
Expand Up @@ -4,6 +4,7 @@
require 'httparty'
require 'json'
require 'open-uri'
require 'pp'

class Rcapr
def initialize(username, password)
Expand All @@ -13,9 +14,11 @@ def initialize(username, password)
end

def upload(filename)
return false unless File.exist?(filename)
STDERR.puts "[+] uploading #{filename}"
result = JSON.parse(@api.upload(filename))
#result = @api.upload(filename)
pp result
#result = JSON.parse(@api.upload(filename))
# '{ okay: true, id: "uploaded-pcap" }'
return result[:okay]
end
Expand All @@ -39,10 +42,17 @@ def initialize(u, p)
# Given a file, upload the file
def upload(file)
begin
options = { :basic_auth => @auth, :file => "@#{file}" }
result = self.class.post('/upload', options)
STDERR.puts "result: #{result}"
return result
# TODO: find a ruby way of uploading a file using basic auth
#options = { :basic_auth => @auth, :file => "@#{file}" }
#mp = Multipart.new(filename)
#mp.post("http://www.pcapr.net/api/upload")
#result = self.class.post('/upload', options)
#STDERR.puts "result: #{result}"
#return result

result = `curl --basic -u #{@auth[:username]}:#{@auth[:password]} -F file=@#{file} -F description="testing the API" -F tags="api" http://www.pcapr.net/api/upload`
return nil if result.nil?
return result.to_json
rescue
return {:okay => false}.to_json
end
Expand Down

0 comments on commit 742ca7e

Please sign in to comment.