rack_gyazo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use default@rack_gyazo --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack_gyazo.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec',
5
+ :version => 2,
6
+ :all_after_pass => false,
7
+ :all_on_start => false,
8
+ :keep_failed => false do
9
+ watch(%r{^spec/.+_spec\.rb$})
10
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch('spec/spec_helper.rb') { "spec" }
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dan Pickett
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,55 @@
1
+ # RackGyazo
2
+
3
+ This rack application is the server side implementation of the excellent gyazo.com application for taking quick screen shots
4
+
5
+ ## Installation
6
+
7
+ Create a new directory and add a Gemfile. Add this line:
8
+
9
+ gem 'rack_gyazo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack_gyazo
18
+
19
+ In the same directory create a rack configuration file (config.ru) like the following:
20
+
21
+ ```ruby
22
+ require 'rubygems'
23
+ require 'bundler/setup'
24
+ Bundler.require
25
+
26
+ require 'rack_gyazo'
27
+ Rack::Gyazo.configure_with({
28
+ :adapter_options => {
29
+ :provider => 'AWS',
30
+ :aws_access_key_id => "Access Key",
31
+ :aws_secret_access_key => "Secret Key"
32
+ },
33
+ :bucket => "bucket_name"
34
+ })
35
+ run Rack::Gyazo::Application.new
36
+ ```
37
+
38
+ Modify your /Applications/Gyazo.app/Contents/Resources/script to point to your server (at the time of this writing, it can be found at line 41)
39
+
40
+ ```ruby
41
+ HOST = 'example.com'
42
+ CGI = '/uploads'
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ When you take screenshots with gyazo.app, it will now upload them to s3
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ end
10
+
11
+ task default: :spec
12
+
13
+ rescue LoadError
14
+ puts "RSpec is not installed"
15
+ end
16
+
17
+
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require
4
+
5
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
6
+ require 'rack_gyazo'
7
+ run Rack::Gyazo::Application.new
@@ -0,0 +1,51 @@
1
+ module Gyazo
2
+ class Image
3
+ attr_reader :file
4
+ def initialize(upload_data)
5
+ @file = upload_data[:tempfile]
6
+ end
7
+
8
+ def upload
9
+ @url = self.adapter.upload
10
+ end
11
+
12
+ def url
13
+ @url
14
+ end
15
+
16
+ def data
17
+ @data ||= @file.read
18
+ end
19
+
20
+ def name
21
+ @name ||= Digest::MD5.hexdigest(data).to_s + ".jpg"
22
+ end
23
+
24
+ protected
25
+ def adapter
26
+ @adapter ||= self.class.adapter_class.new(self)
27
+ end
28
+
29
+ class << self
30
+ def adapter_class
31
+ @adapter || ::Gyazo::UploadAdapters::Fog
32
+ end
33
+
34
+ def adapter=(sym_or_klass)
35
+ if sym_or_klass.kind_of?(Symbol)
36
+ @adapter = adapter_map[sym_or_klass]
37
+ else
38
+ @adapter = sym_or_klass
39
+ end
40
+ end
41
+
42
+ protected
43
+ def adapter_map
44
+ {
45
+ :fog => ::Gyazo::UploadAdapters::Fog
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,45 @@
1
+ require "fog"
2
+
3
+ module Gyazo
4
+ module UploadAdapters
5
+ class Fog
6
+ attr_reader :image
7
+
8
+ def initialize(image)
9
+ @image = image
10
+ end
11
+
12
+ def upload
13
+ connection.put_object(bucket, image.name, image.data,
14
+ {
15
+ 'Content-Type' => "image/jpeg",
16
+ 'x-amz-acl' => 'public-read'
17
+ }
18
+ )
19
+ public_url
20
+ end
21
+
22
+ def public_url
23
+ ["http://#{bucket}.s3.amazonaws.com", image.name].compact.join('/')
24
+ end
25
+
26
+ protected
27
+ def path
28
+ "/"
29
+ end
30
+
31
+ def connection
32
+ @connection ||= ::Fog::Storage.new(configuration[:adapter_options])
33
+ end
34
+
35
+ def bucket
36
+ configuration[:bucket]
37
+ end
38
+
39
+ def configuration
40
+ Rack::Gyazo.configuration
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,5 @@
1
+ module Gyazo
2
+ module UploadAdapters
3
+ end
4
+ end
5
+
data/lib/gyazo.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Gyazo
2
+ end
3
+
@@ -0,0 +1,32 @@
1
+ module Rack
2
+ module Gyazo
3
+ class Application
4
+ def call(env)
5
+ @env = env
6
+ if request.path == "/uploads" && request.post?
7
+ if !image.nil?
8
+ image.upload
9
+ [200, {'Content-Type' => 'text/plain'}, StringIO.new(image.url)]
10
+ else
11
+ [422, {'Content-Type' => 'text/plain'}, StringIO.new('imagedata not specified')]
12
+ end
13
+ else
14
+ [404, {'Content-Type' => 'text/plain'}, StringIO.new('file not found')]
15
+ end
16
+ end
17
+
18
+ protected
19
+ def request
20
+ @request ||= Rack::Request.new(@env)
21
+ end
22
+
23
+ def image
24
+ if request.params["imagedata"]
25
+ @image ||= ::Gyazo::Image.new(request.params["imagedata"])
26
+ else
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ module Rack
2
+ module Gyazo
3
+ class Configuration
4
+ def initialize(options = {})
5
+ @configuration = options
6
+ end
7
+
8
+ def [](key)
9
+ @configuration[key]
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Gyazo
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/rack/gyazo.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Rack
2
+ module Gyazo
3
+ class << self
4
+ def configure_with(hash)
5
+ @configuration = Rack::Gyazo::Configuration.new(hash)
6
+ end
7
+
8
+ def configuration
9
+ (@configuration || configure_with({}))
10
+ end
11
+ end
12
+ end
13
+ end
14
+
data/lib/rack_gyazo.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "rack"
2
+ require "rack/gyazo/version"
3
+
4
+ require 'gyazo'
5
+ require "gyazo/image"
6
+ require "gyazo/upload_adapters"
7
+ require "gyazo/upload_adapters/fog"
8
+
9
+ require "rack/gyazo"
10
+ require "rack/gyazo/configuration"
11
+ require "rack/gyazo/application"
12
+
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rack/gyazo/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dan Pickett"]
6
+ gem.email = ["dpickett@enlightsolutions.com"]
7
+ gem.description = %q{A Rack Application for Gyazo.app}
8
+ gem.summary = %q{A Fog-based Rack Application for Gyazo.app}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "rack_gyazo"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rack::Gyazo::VERSION
17
+
18
+ gem.add_dependency "rack"
19
+ gem.add_dependency "fog"
20
+ gem.add_dependency "rake"
21
+
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "mocha"
24
+ gem.add_development_dependency "pry"
25
+ gem.add_development_dependency "guard-rspec"
26
+ gem.add_development_dependency "rack-test"
27
+ gem.add_development_dependency 'rb-fsevent'
28
+ gem.add_development_dependency 'growl_notify'
29
+ end
Binary file
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gyazo::Image do
4
+ let(:file_path) do
5
+ File.join(File.dirname(__FILE__), "../fixtures/test_image.png")
6
+ end
7
+
8
+ let(:file) { File.open(file_path) }
9
+ let(:params) do
10
+ {
11
+ :filename => "test_image.png",
12
+ :type => "text/plain",
13
+ :name => "imagedata",
14
+ :tempfile => file,
15
+ :head=>
16
+ "Content-Disposition: form-data; name=\"imagedata\"; filename=\"test_image.png\"\r\nContent-Type: text/plain\r\nContent-Length: 35885\r\n"
17
+ }
18
+ end
19
+
20
+ subject { ::Gyazo::Image.new(params) }
21
+
22
+ describe "uploading" do
23
+ it "delegates to the upload adapter" do
24
+ mock_adapter_class = mock
25
+ mock_adapter = mock
26
+ mock_adapter.expects(:upload).returns("http://www.example.com")
27
+ mock_adapter_class.expects(:new).returns(mock_adapter)
28
+ ::Gyazo::Image.adapter = mock_adapter_class
29
+ subject.upload
30
+ end
31
+ end
32
+
33
+ describe "upload adapters" do
34
+ it "allows the client to specify a symbol as an adapter" do
35
+ ::Gyazo::Image.adapter = :fog
36
+ ::Gyazo::Image.adapter_class.should eql(::Gyazo::UploadAdapters::Fog)
37
+ end
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Gyazo::Application do
4
+ include Rack::Test::Methods
5
+
6
+ let(:image_path) do
7
+ File.join(File.dirname(__FILE__), "../../fixtures/test_image.png")
8
+ end
9
+
10
+ let(:image) { File.open(image_path) }
11
+
12
+ def app
13
+ Rack::Gyazo::Application.new
14
+ end
15
+
16
+ it "returns a 404 if the path isn't /uploads" do
17
+ post '/'
18
+ last_response.should_not be_ok
19
+ last_response.status.should eql(404)
20
+ end
21
+
22
+ it "returns a 404 if the method isn't post" do
23
+ get "/uploads"
24
+ last_response.should_not be_ok
25
+ last_response.status.should eql(404)
26
+ end
27
+
28
+ it "returns a 422 if an upload isn't specified" do
29
+ post "/uploads"
30
+ last_response.should_not be_ok
31
+ last_response.status.should eql(422)
32
+ end
33
+
34
+ it 'creates an image if I specify one' do
35
+ mock_image = mock('Image')
36
+ mock_image.expects(:upload).once
37
+ mock_image.stubs(:url).returns('http://example.org')
38
+ ::Gyazo::Image.expects(:new).returns(mock_image)
39
+
40
+ post '/uploads', :imagedata => Rack::Test::UploadedFile.new(image_path)
41
+ last_response.should be_ok
42
+ end
43
+ end
44
+
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Gyazo do
4
+ let(:configuration) do
5
+ {
6
+ :adapter => :fog,
7
+ :bucket => "some_bucket",
8
+ :adapter_options => {
9
+ :provider => :aws,
10
+ :aws_access_key => "access_key",
11
+ :aws_secret_access_key => "secret_key"
12
+ }
13
+ }
14
+ end
15
+ it "allows me to configure arbitrary options" do
16
+ Rack::Gyazo.configure_with(configuration)
17
+ end
18
+
19
+ it "defaults to a blank configuration" do
20
+ Rack::Gyazo.configuration.should_not be_nil
21
+ end
22
+ end
23
+
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+ require 'mocha'
3
+ require 'rack_gyazo'
4
+ require 'rack/test'
5
+ require 'pry'
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :mocha
9
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_gyazo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Pickett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &2156366840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2156366840
25
+ - !ruby/object:Gem::Dependency
26
+ name: fog
27
+ requirement: &2156366280 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2156366280
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &2156365680 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2156365680
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &2156364800 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2156364800
58
+ - !ruby/object:Gem::Dependency
59
+ name: mocha
60
+ requirement: &2156364100 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2156364100
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: &2156363380 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2156363380
80
+ - !ruby/object:Gem::Dependency
81
+ name: guard-rspec
82
+ requirement: &2156362680 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *2156362680
91
+ - !ruby/object:Gem::Dependency
92
+ name: rack-test
93
+ requirement: &2156361840 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *2156361840
102
+ - !ruby/object:Gem::Dependency
103
+ name: rb-fsevent
104
+ requirement: &2156360920 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *2156360920
113
+ - !ruby/object:Gem::Dependency
114
+ name: growl_notify
115
+ requirement: &2156359880 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *2156359880
124
+ description: A Rack Application for Gyazo.app
125
+ email:
126
+ - dpickett@enlightsolutions.com
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - .gitignore
132
+ - .rspec
133
+ - .rvmrc
134
+ - Gemfile
135
+ - Guardfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - config.ru
140
+ - lib/gyazo.rb
141
+ - lib/gyazo/image.rb
142
+ - lib/gyazo/upload_adapters.rb
143
+ - lib/gyazo/upload_adapters/fog.rb
144
+ - lib/rack/gyazo.rb
145
+ - lib/rack/gyazo/application.rb
146
+ - lib/rack/gyazo/configuration.rb
147
+ - lib/rack/gyazo/version.rb
148
+ - lib/rack_gyazo.rb
149
+ - rack_gyazo.gemspec
150
+ - spec/fixtures/test_image.png
151
+ - spec/gyazo/image_spec.rb
152
+ - spec/rack/gyazo/application_spec.rb
153
+ - spec/rack/gyazo_spec.rb
154
+ - spec/spec_helper.rb
155
+ homepage: ''
156
+ licenses: []
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ segments:
168
+ - 0
169
+ hash: 280352373364577039
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ! '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ segments:
177
+ - 0
178
+ hash: 280352373364577039
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 1.8.6
182
+ signing_key:
183
+ specification_version: 3
184
+ summary: A Fog-based Rack Application for Gyazo.app
185
+ test_files:
186
+ - spec/fixtures/test_image.png
187
+ - spec/gyazo/image_spec.rb
188
+ - spec/rack/gyazo/application_spec.rb
189
+ - spec/rack/gyazo_spec.rb
190
+ - spec/spec_helper.rb