Skip to content

Commit

Permalink
Initial commit, see README
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Mar 20, 2009
0 parents commit 6078db4
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Huzzah! An API for pcapr (http://pcapr.net)

Doesn't work yet. Do not consume unless you know what you're doing.

######################################################
Be sure to change ./features/step_definitions/config.yml to have your
pcapr username and password in order to run the tests

Cucumber tests: run 'rake features'
######################################################
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
require 'cucumber/rake/task'

Cucumber::Rake::Task.new do |t|
t.cucumber_opts = "--language en"
end
16 changes: 16 additions & 0 deletions features/download.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: Downloads
In order to retrieve data from pcapr
As a regular user
I want to be able to download a pcap file

Scenario Outline: Download a pcap file
Given I have a pcap file id <p_id>
When I download the pcap file
Then the pcap file should be download

Examples:
| p_id |
| 100 |
| 101 |

# vim: set ts=2 sw=2
3 changes: 3 additions & 0 deletions features/step_definitions/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
:username: your-username-here
:password: your-password-here
24 changes: 24 additions & 0 deletions features/step_definitions/download_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$:.unshift File.join(File.dirname(__FILE__))

require 'spec/expectations'
require 'rcapr'
require 'env'

Before do
@rc = Rcapr.new(@username, @password)
end

After do
end

Given /^I have a pcap file id (\d+)$/ do |id|
pending
end

When /^I download the pcap file$/ do
pending
end

Then /^the pcap file should be download$/ do
pending
end
6 changes: 6 additions & 0 deletions features/step_definitions/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'yaml'

f = File.new(File.dirname(__FILE__) + "/config.yml", "r")
@config = YAML.load(f)
@username = @config[:username]
@password = @config[:password]
24 changes: 24 additions & 0 deletions features/step_definitions/upload_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$:.unshift File.join(File.dirname(__FILE__))

require 'spec/expectations'
require 'rcapr'
require 'env'

Before do
@rc = Rcapr.new(@username, @password)
end

After do
end

Given /^I have a pcap file ([\S.]+)$/ do |pcap_file|
@filename = pcap_file
end

When /^I upload the pcap file$/ do
@result = @rc.upload(@filename)
end

Then /^the pcap file should be uploaded$/ do
@result.should =~ /\{ okay: true, id: "([\S]+)" \}/
end
15 changes: 15 additions & 0 deletions features/upload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Uploads
In order to contribute data to pcapr
As a regular user
I want to be able to upload a pcap file

Scenario Outline: Upload a pcap file
Given I have a pcap file <filename>
When I upload the pcap file
Then the pcap file should be uploaded

Examples:
| filename |
| t.pcap |

# vim: set ts=2 sw=2
43 changes: 43 additions & 0 deletions lib/rcapr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'httparty'
require 'pp'
require 'json'

class Rcapr
def initialize(username, password)
@username = username
@password = password
@api = RcaprAPI.new(@username, @password)
end

def upload(filename)
result = @api.upload(filename)
#pp result
# TODO: de-stub
#return '{ okay: true, id: "uploaded-pcap" }'
end
end

# (598)-~% curl --basic -u user@domain.com:******* -F file=@t.pcap -F description="testing the API" -F tags="api" http://www.pcapr.net/api/upload
# ({id:"c6be820e5c1cf9f9f37871602c7a8ed1", okay:true})

class RcaprAPI
include HTTParty
base_uri 'http://www.pcapr.net/api'

def initialize(u, p)
@auth = {:username => u, :password => p}
end

def upload(file)
begin
options = { :basic_auth => @auth, :file => file }
self.class.post('/upload', options)
rescue
return {:okay => false}.to_json
end
end

end

0 comments on commit 6078db4

Please sign in to comment.