s3itch_client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ script: "bundle exec rake spec"
2
+ language: ruby
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
7
+ - jruby-18mode
8
+ - jruby-19mode
9
+ - rbx-18mode
10
+ - ree
11
+ notifications:
12
+ disable: true
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3itch_cli.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ s3itch_client (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.3.2)
10
+ crack (0.3.1)
11
+ diff-lcs (1.1.3)
12
+ multi_json (1.3.6)
13
+ rake (0.9.2.2)
14
+ rspec (2.11.0)
15
+ rspec-core (~> 2.11.0)
16
+ rspec-expectations (~> 2.11.0)
17
+ rspec-mocks (~> 2.11.0)
18
+ rspec-core (2.11.1)
19
+ rspec-expectations (2.11.3)
20
+ diff-lcs (~> 1.1.3)
21
+ rspec-mocks (2.11.2)
22
+ simplecov (0.6.4)
23
+ multi_json (~> 1.0)
24
+ simplecov-html (~> 0.5.3)
25
+ simplecov-html (0.5.3)
26
+ webmock (1.8.11)
27
+ addressable (>= 2.2.7)
28
+ crack (>= 0.1.7)
29
+
30
+ PLATFORMS
31
+ java
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler
36
+ rake
37
+ rspec (~> 2.11.0)
38
+ s3itch_client!
39
+ simplecov (~> 0.6.4)
40
+ webmock (~> 1.8.11)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Matias Korhonen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # S3itch Client
2
+
3
+ Upload files to your [s3itch][s3itch] instance directly from the command line.
4
+
5
+ Each file is given a unique, unguessable filename based on the original filename. e.g. `kitten.jpeg` will be turned into something like:
6
+
7
+ `kitten_def46901-9dfc-47ad-86b5-c6831d65d9ed.jpeg`.
8
+
9
+ ## Installation & Configuration
10
+
11
+ Install it from RubyGems:
12
+
13
+ $ gem install s3itch-cli
14
+
15
+ For your own convenience, create the `~/.s3itch.yml` file with the following contents:
16
+
17
+ ---
18
+ url: http://YOUR-S3ITCH-INSTANCE.herokuapp.com
19
+ username: S3ITCH_USERNAME
20
+ password: YOUR_PASSWORD
21
+
22
+ ## Usage
23
+
24
+ If you haven't defined your [s3itch][s3itch] URL, username, and password in `~/.s3itch.yml`, you'll have to do so via the CLI options.
25
+
26
+ Usage: s3itch [options] <filename>
27
+ -u, --url [URL] Set the s3itch host URL
28
+ -n, --user [USERNAME] Set the s3itch username
29
+ -p, --password [PASSWORD] Set the s3itch password
30
+ -h, --help Show this message
31
+ --version Show version
32
+
33
+ CLI arguments will always override any options set in the config file.
34
+
35
+ If you've configured you `~/.s3itch.yml` correctly, uploading an image is as simple as:
36
+
37
+ s3itch kittens.jpeg
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
46
+
47
+ [s3itch]: https://github.com/roidrage/s3itch
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
data/bin/s3itch ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "s3itch_client/cli"
4
+
5
+ begin
6
+ ARGV << "-h" unless ARGV.any?
7
+ puts S3itchClient::CLI.upload(ARGV, File.expand_path(ARGV.last))
8
+ rescue Exception => e
9
+ abort "#{e.class}: #{e.message}" unless e.kind_of? SystemExit
10
+ end
11
+
12
+ exit 0
@@ -0,0 +1,61 @@
1
+ require "s3itch_client"
2
+ require "s3itch_client/version"
3
+ require "optparse"
4
+ require "yaml"
5
+
6
+ module S3itchClient
7
+ module CLI
8
+ CONFIG_PATH = File.expand_path "~/.s3itch.yml"
9
+
10
+ def self.upload(argv, filepath)
11
+ S3itchClient.upload(filepath, parse_options(argv))
12
+ end
13
+
14
+ def self.parse_options(argv)
15
+ options = {}
16
+
17
+ OptionParser.new do |opts|
18
+ opts.banner = "Usage: s3itch [options] <filename>"
19
+
20
+ opts.on("-u", "--url [URL]", String, "Set the s3itch host URL") do |uri|
21
+ options["url"] = uri
22
+ end
23
+
24
+ opts.on("-n", "--user [USERNAME]", String, "Set the s3itch username") do |user|
25
+ options["username"] = user
26
+ end
27
+
28
+ opts.on("-p", "--password [PASSWORD]", String, "Set the s3itch password") do |password|
29
+ options["password"] = password
30
+ end
31
+
32
+ opts.on_tail("-h", "--help", "Show this message") do
33
+ puts opts
34
+ exit
35
+ end
36
+
37
+ opts.on_tail("--version", "Show version") do
38
+ puts "S3itchCli version #{S3itchClient::VERSION}"
39
+ exit
40
+ end
41
+ end.parse!
42
+
43
+ if File.exists? CONFIG_PATH
44
+ config = YAML.load_file(CONFIG_PATH)
45
+ options = config.merge(options)
46
+ elsif options[:url].nil?
47
+ abort <<-EOS
48
+ Couldn't find your configuration in '#{CONFIG_PATH}'
49
+ Please create the file with the following contents:
50
+
51
+ ---
52
+ url: http://YOUR-S3ITCH-INSTANCE.herokuapp.com
53
+ username: S3ITCH_USERNAME
54
+ password: YOUR_PASSWORD
55
+ EOS
56
+ end
57
+
58
+ options
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module S3itchClient
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,62 @@
1
+ require "s3itch_client/version"
2
+
3
+ require "net/http"
4
+ require "securerandom"
5
+ require "uri"
6
+ require "yaml"
7
+
8
+ module S3itchClient
9
+
10
+ def self.upload(filepath, options={})
11
+ options = indifferent_hash(options)
12
+
13
+ unless options[:url]
14
+ raise ArgumentError, "A URL must be provided"
15
+ end
16
+
17
+ url = options[:url]
18
+ username = options[:username]
19
+ password = options[:password]
20
+
21
+ if File.exists?(filepath) && !File.directory?(filepath)
22
+ uniq_name = build_unique_name(filepath)
23
+
24
+ uri = URI.parse "#{url}/#{uniq_name}"
25
+
26
+ http = Net::HTTP.new(uri.host, uri.port)
27
+ request = Net::HTTP::Put.new(uri.request_uri)
28
+
29
+ if username && password
30
+ request.basic_auth(username, password)
31
+ end
32
+
33
+ request.content_type = "application/octet-stream"
34
+ response = http.request(request, File.open(filepath).read)
35
+
36
+ if response["Location"]
37
+ return response["Location"]
38
+ else
39
+ raise StandardError, "Something went wrong [#{response.code}]"
40
+ end
41
+ else
42
+ raise ArgumentError, "No such file - #{filepath}"
43
+ end
44
+ end
45
+
46
+ def self.build_unique_name(filepath)
47
+ filename = File.basename(filepath)
48
+ extname = File.extname(filename)
49
+ basename = File.basename(filename, extname)
50
+
51
+ # Ruby 1.8.7 compatibility
52
+ uuid = SecureRandom.respond_to?(:uuid) ? SecureRandom.uuid : SecureRandom.hex
53
+
54
+ "#{basename}_#{uuid}#{extname}"
55
+ end
56
+
57
+ def self.indifferent_hash(hash)
58
+ indifferent = Hash.new { |h,k| h[k.to_s] if Symbol === k }
59
+ indifferent.merge(hash)
60
+ end
61
+
62
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3itch_client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "s3itch_client"
8
+ gem.version = S3itchClient::VERSION
9
+ gem.authors = ["Matias Korhonen"]
10
+ gem.email = ["me@matiaskorhonen.fi"]
11
+ gem.description = %q{Send files to s3itch from the command line}
12
+ gem.summary = %q{Upload files to S3 via your own s3itch instance}
13
+ gem.homepage = "https://github.com/k33l0r/s3itch_client"
14
+
15
+ gem.add_development_dependency "bundler"
16
+ gem.add_development_dependency "rake"
17
+ gem.add_development_dependency "rspec", "~> 2.11.0"
18
+ gem.add_development_dependency "webmock", "~> 1.8.11"
19
+ gem.add_development_dependency "simplecov", "~> 0.6.4"
20
+
21
+ gem.files = `git ls-files`.split($/)
22
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
23
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
+ gem.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ describe S3itchClient do
4
+ let(:kitten_path) { File.expand_path("../support/kitten.jpeg", __FILE__).to_s }
5
+
6
+ describe ".indifferent_hash" do
7
+
8
+ let(:hash) do
9
+ S3itchClient.indifferent_hash({
10
+ :symbol => "Foo",
11
+ "string" => "Bar"
12
+ })
13
+ end
14
+
15
+ it "lets you access string keys with symbols" do
16
+ hash[:string].should == "Bar"
17
+ end
18
+
19
+ it "lets you access symbol keys with symbols" do
20
+ hash[:symbol].should == "Foo"
21
+ end
22
+
23
+ it "lets you access string keys with strings" do
24
+ hash["string"].should == "Bar"
25
+ end
26
+
27
+ end
28
+
29
+ describe ".build_unique_name" do
30
+ it "build a unique name from a file path" do
31
+ name = S3itchClient.build_unique_name(kitten_path)
32
+ name.should match(/\Akitten_[\da-z-]{32,36}\.jpeg\z/)
33
+ end
34
+ end
35
+
36
+ describe ".upload" do
37
+ let(:options) do
38
+ {
39
+ :url => "http://s3itch.herokuapp.com",
40
+ :username => "s3itch",
41
+ :password => "secret"
42
+ }
43
+ end
44
+
45
+
46
+ it "raises an exception if no url is defined" do
47
+ expect { S3itchClient.upload(kitten_path, {}) }.to raise_error(ArgumentError)
48
+ end
49
+
50
+ it "raises an exception if the file does not exist" do
51
+ expect { S3itchClient.upload("does_not_exist", { :url => "foo" }) }.to raise_error(ArgumentError)
52
+ end
53
+
54
+ it "sends files to s3itch" do
55
+ stub = stub_request(:put, /s3itch.herokuapp.com/).
56
+ to_return(:body => "", :status => 201, :headers => { "Location" => "http://s3itch.exmaple.com/kitten.jpeg" })
57
+ S3itchClient.upload(kitten_path, options).should == "http://s3itch.exmaple.com/kitten.jpeg"
58
+ stub.should have_been_requested
59
+ end
60
+
61
+ it "raises an exception if the 'Location' header returned" do
62
+ stub = stub_request(:put, /s3itch.herokuapp.com/).
63
+ to_return(:body => "", :status => 201)
64
+ expect { S3itchClient.upload(kitten_path, options) }.to raise_error
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,10 @@
1
+ require "simplecov"
2
+ SimpleCov.start
3
+
4
+ require "bundler"
5
+
6
+ Bundler.setup
7
+
8
+ require "rspec"
9
+ require "webmock/rspec"
10
+ require "s3itch_client"
Binary file
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3itch_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matias Korhonen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.11.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.11.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.11
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.11
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 0.6.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.6.4
94
+ description: Send files to s3itch from the command line
95
+ email:
96
+ - me@matiaskorhonen.fi
97
+ executables:
98
+ - s3itch
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - .rspec
104
+ - .travis.yml
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/s3itch
111
+ - lib/s3itch_client.rb
112
+ - lib/s3itch_client/cli.rb
113
+ - lib/s3itch_client/version.rb
114
+ - s3itch_client.gemspec
115
+ - spec/s3itch_client_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/support/kitten.jpeg
118
+ homepage: https://github.com/k33l0r/s3itch_client
119
+ licenses: []
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ segments:
131
+ - 0
132
+ hash: 566530295880295081
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
140
+ - 0
141
+ hash: 566530295880295081
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.24
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Upload files to S3 via your own s3itch instance
148
+ test_files:
149
+ - spec/s3itch_client_spec.rb
150
+ - spec/spec_helper.rb
151
+ - spec/support/kitten.jpeg