swift_file 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,14 @@
1
1
  # SwiftFile
2
2
 
3
- TODO: Write a gem description
3
+ This gem provides an interface for uploading files to the [SwiftFile](https://www.swiftfile.net) service. There is support for uploading via the command line via the supplied executable `swift_file` as well as a basic API for adding support to your own classes.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Most likely you will want to simply install the gem:
8
+
9
+ $ gem install swift_file
10
+
11
+ Or to use this in your projects, add this line to your application's Gemfile:
8
12
 
9
13
  gem 'swift_file'
10
14
 
@@ -12,13 +16,13 @@ And then execute:
12
16
 
13
17
  $ bundle
14
18
 
15
- Or install it yourself as:
16
-
17
- $ gem install swift_file
18
-
19
- ## Usage
19
+ ## Command-line Usage
20
+ `swift_file -h`
20
21
 
21
- TODO: Write usage instructions here
22
+ Usage: swift_file [options] file1 file2 ...
23
+ -g, --group GROUP Add this file to a group
24
+ -e, --expires EXPIRY Set the length of time until this file expires [1m 1w 1d 1h]
25
+ -p[PASSWORD] Supply a password to encrypt the file.
22
26
 
23
27
  ## Contributing
24
28
 
@@ -1,38 +1,57 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "optparse"
3
+ require "highline/import"
3
4
  require "swift_file"
4
- require "pp"
5
5
 
6
6
  options = {}
7
7
 
8
8
  optparse = OptionParser.new do|opts|
9
- opts.banner = "Usage: swift_file [options] file"
9
+ opts.banner = "Usage: swift_file [options] file1 file2 ..."
10
10
 
11
11
  options[:group] = nil
12
12
  opts.on( '-g', '--group GROUP', 'Add this file to a group' ) do |group|
13
13
  options[:group] = group
14
14
  end
15
15
 
16
+ options[:expiry] = nil
17
+ opts.on( '-e', '--expires EXPIRY', "Set the length of time until this file expires [#{SwiftFile::SwiftUpload::SWIFTFILE_EXPIRY_OPTIONS.join(' ')}]" ) do |expiry|
18
+ options[:expiry] = expiry
19
+ end
20
+
16
21
  options[:password] = nil
17
- opts.on( '-p', '--password PASS', 'Secure the file with a password' ) do |pass|
18
- options[:password] = pass
22
+ opts.on( '-p[PASSWORD]', 'Supply a password to encrypt the file.' ) do |password|
23
+ options[:password] = password
19
24
  end
20
25
  end
21
26
 
22
- optparse.parse!
27
+ # Check if user has (correctly) supplied the -p flag without the password
28
+ password_opt_supplied = ARGV.include?('-p')
29
+
30
+ begin
31
+ optparse.parse!
32
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
33
+ puts "ERROR: #{e}"
34
+ end
35
+
36
+ if options[:password] == nil && password_opt_supplied
37
+ # FIXME: highline is not behaving on my installation, and
38
+ # disabling echo causes an infinte loop of errors
39
+ options[:password] = ask("Enter a password: ") # { |q| q.echo = false }
40
+ end
23
41
 
24
42
  begin
43
+ if ARGV.empty?
44
+ raise "no files supplied"
45
+ end
25
46
 
26
- f = File.new(ARGV[0])
47
+ sf = SwiftFile::SwiftUpload.new(options)
27
48
 
28
- sf = SwiftFile::SwiftUpload.new({
29
- :file => f,
30
- :group => options[:group],
31
- :password => options[:password]
32
- })
49
+ ARGV.each do |filepath|
50
+ sf.add_file File.new(filepath)
51
+ end
33
52
 
34
- sf.upload
53
+ sf.transfer
35
54
  puts sf.url
36
- rescue Exception => e
55
+ rescue => e
37
56
  puts "ERROR: #{e}"
38
57
  end
@@ -2,7 +2,6 @@ require "swift_file/version"
2
2
  require "swift_file/swift_upload"
3
3
 
4
4
  module SwiftFile
5
-
6
5
  class << self
7
6
  attr_accessor :swift_file, :swift_file_group, :swift_file_password
8
7
 
@@ -2,56 +2,83 @@ require 'curb'
2
2
 
3
3
  module SwiftFile
4
4
  class SwiftUpload
5
- @@swiftfile_host = "https://www.swiftfile.net"
6
- @@swiftfile_endpoint = ""
7
- @@swiftfile_cookie = "/tmp/swift_file.cookie"
5
+ SWIFTFILE_HOST = 'https://www.swiftfile.net'
6
+ SWIFTFILE_ENDPOINT = ''
7
+ SWIFTFILE_COOKIE = '/tmp/swift_file.cookie'
8
+ SWIFTFILE_EXPIRY_OPTIONS = %w(1m 1w 1d 1h)
9
+ SWIFTFILE_EXPIRY_DEFAULT = '1d'
8
10
 
9
- attr_accessor :file, :group, :url
11
+ attr_accessor :files, :group, :expiry, :url
10
12
 
11
13
  def initialize(params)
12
- if File.readable?(params[:file]) && File.file?(params[:file])
13
- @file = params[:file]
14
+ @files = params[:files] || []
15
+ @group = params[:group] || ''
16
+ @password = params[:password] || ''
17
+
18
+ if params[:expiry] && SWIFTFILE_EXPIRY_OPTIONS.include?(params[:expiry])
19
+ @expiry = params[:expiry]
14
20
  else
15
- raise ArgumentError.new('Invalid file supplied for upload')
21
+ @expiry = SWIFTFILE_EXPIRY_DEFAULT
16
22
  end
23
+ end
17
24
 
18
- @group = params[:group] || ''
19
- @password = params[:password] || ''
25
+ def add_file(file)
26
+ @files << file unless @files.include?(file)
20
27
  end
21
28
 
29
+ def transfer
30
+ if @files.empty?
31
+ raise 'No files supplied for uploed'
32
+ end
33
+
34
+ # start a session by requesting the url via GET
35
+ begin
36
+ client.perform
37
+ rescue Curl::Err => e
38
+ raise "Connection failed: #{e}"
39
+ end
22
40
 
23
- def upload
24
- # start a session by requestion the url via GET
25
- client.perform
41
+ # post the file and capture the resulting url
42
+ begin
43
+ client.multipart_form_post = true
44
+ client.http_post(form)
26
45
 
27
- # now post
28
- client.multipart_form_post = true
29
- client.http_post(
30
- Curl::PostField.file('file1', "#{@file.path}"),
31
- Curl::PostField.content('file_group[name]', @group),
32
- Curl::PostField.content('file_group[password]', @password),
33
- Curl::PostField.content('file_group_expiration', '1m')
34
- )
46
+ @url = client.last_effective_url
47
+ rescue Curl::Err => e
48
+ raise "Upload failed: #{e}"
49
+ ensure
50
+ client.close
51
+ end
35
52
 
36
- @url = client.last_effective_url
37
53
  @url
38
54
  end
39
55
 
40
- ##
41
- private
42
56
 
57
+ private
43
58
  def client
44
59
  if !@client
45
- url = "#{@@swiftfile_host}/#{@@swiftfile_endpoint}"
60
+ url = "#{SWIFTFILE_HOST}/#{SWIFTFILE_ENDPOINT}"
46
61
  @client = Curl::Easy.new(url) do |curl|
47
- # curl.verbose= true
48
- curl.follow_location = true
49
- curl.enable_cookies = true
50
- curl.cookiejar = @@swiftfile_cookie
62
+ # curl.verbose= true
63
+ curl.follow_location = true
64
+ curl.enable_cookies = true
65
+ curl.cookiejar = SWIFTFILE_COOKIE
66
+ curl.cookiefile = SWIFTFILE_COOKIE
51
67
  end
52
68
  end
53
-
54
69
  @client
55
70
  end
71
+
72
+ def form
73
+ fields = []
74
+ @files.each_with_index do |file, idx|
75
+ fields << Curl::PostField.file("file#{idx+1}", "#{file.path}")
76
+ end
77
+
78
+ fields << Curl::PostField.content('file_group[name]', @group)
79
+ fields << Curl::PostField.content('file_group[password]', @password)
80
+ fields << Curl::PostField.content('file_group_expiration', @expiry)
81
+ fields
82
+ end
56
83
  end
57
84
  end
@@ -1,3 +1,3 @@
1
1
  module SwiftFile
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,11 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SwiftUpload do
3
+ describe SwiftFile do
4
4
  it "should be able to upload test file" do
5
5
  f = File.new(File.dirname(__FILE__) + '/test_file.txt', 'rb')
6
- sf = SwiftFile::SwiftUpload.new({:file => f})
6
+ files = Array[f]
7
+ sf = SwiftFile::SwiftUpload.new({:files => files})
7
8
 
8
- sf.upload
9
+ sf.transfer
9
10
  sf.url.should =~ /^https/
10
11
  end
11
12
  end
@@ -18,5 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_development_dependency "rspec"
21
- gem.add_dependency("curb", ">= 0.8.1")
21
+ gem.add_dependency("highline", '>=1.6')
22
+ gem.add_dependency("curb")
22
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swift_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-02 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: highline
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '1.6'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '1.6'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: curb
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +50,7 @@ dependencies:
34
50
  requirements:
35
51
  - - ! '>='
36
52
  - !ruby/object:Gem::Version
37
- version: 0.8.1
53
+ version: '0'
38
54
  type: :runtime
39
55
  prerelease: false
40
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +58,7 @@ dependencies:
42
58
  requirements:
43
59
  - - ! '>='
44
60
  - !ruby/object:Gem::Version
45
- version: 0.8.1
61
+ version: '0'
46
62
  description: Upload a file to the SwiftFile service
47
63
  email:
48
64
  - mdeltito@contextllc.com