carrierwave-roz 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9690c466cb06c56135c474bc188796a29d51fd3
4
+ data.tar.gz: aeb01fe972feea8eae898a3023ae99cd2fb62d23
5
+ SHA512:
6
+ metadata.gz: b6f2787578fd2c66fd2cce2604ace6288948fa248d434dcb594df772e9ad216a06fd50cad85db162088170011c32d63d6e88f890e7511b9e62c5a30fb8cefaaf
7
+ data.tar.gz: f95b3492ec338d17d70ad9b261c7bed1f1e9f4e41b0d7596988869dd6124c0717bb59220faed2f55a406881f2405e5871476c6e68e3a6ae9cb305214ee6f2f90
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 by Biola University
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ CarrierWave Roz
2
+ ===============
3
+
4
+ __TODO__
@@ -0,0 +1,85 @@
1
+ require 'open-uri'
2
+ require 'ostruct'
3
+
4
+ module CarrierWave
5
+ module Storage
6
+ class Roz < Abstract
7
+ def store!(file)
8
+ f = CarrierWave::Storage::Roz::File.new(uploader, uploader.store_path)
9
+ f.store(file)
10
+ f
11
+ end
12
+
13
+ def retrieve!(identifier)
14
+ CarrierWave::Storage::Roz::File.new(uploader, uploader.store_path(identifier))
15
+ end
16
+
17
+ class File
18
+ attr_reader :path
19
+ attr_reader :uploader
20
+ attr_reader :file
21
+
22
+ def initialize(uploader, path)
23
+ @uploader = uploader
24
+ @path = path
25
+ end
26
+
27
+ def store(file)
28
+ response = client.upload(file, path)
29
+
30
+ unless (200..208).include? response.code.to_i
31
+ # json = JSON.parse(response.to_str)
32
+ # TODO: try to parse and raise JSON error message
33
+ raise response.to_s
34
+ end
35
+ end
36
+
37
+ def url(*args)
38
+ URI.join(uploader.files_base_url, path).to_s
39
+ end
40
+
41
+ def filename
42
+ ::File.basename(path)
43
+ end
44
+
45
+ def read
46
+ file.content
47
+ end
48
+
49
+ def delete
50
+ response = client.delete(path)
51
+
52
+ unless (200..208).include? response.code.to_i
53
+ # json = JSON.parse(response.to_str)
54
+ # TODO: try to parse and raise JSON error message
55
+ raise response.to_s
56
+ end
57
+ end
58
+
59
+ def content_type
60
+ file.content_type
61
+ end
62
+
63
+ def length
64
+ file.length
65
+ end
66
+
67
+ alias :content_length :length
68
+ alias :file_length :length
69
+ alias :size :length
70
+
71
+ protected
72
+
73
+ def client
74
+ CarrierwaveRoz::Client.new(uploader.api_base_url, uploader.access_id, uploader.secret_key)
75
+ end
76
+
77
+ def file
78
+ @file ||= open(url) do |f|
79
+ OpenStruct.new content_type: f.content_type, content: f.read, length: f.length
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,56 @@
1
+ require 'net/http/post/multipart'
2
+ require 'api_auth'
3
+
4
+ module CarrierwaveRoz
5
+ class Client
6
+ VERSION = 'v1'
7
+
8
+ attr_reader :base_url
9
+ attr_reader :access_id
10
+ attr_reader :secret_key
11
+
12
+ def initialize(base_url, access_id, secret_key)
13
+ @base_url = base_url
14
+ @access_id = access_id
15
+ @secret_key = secret_key
16
+ end
17
+
18
+ def upload(file, path)
19
+ uri = url_for(:upload)
20
+ upload = UploadIO.new(file.to_file, file.content_type, file.original_filename)
21
+ request = Net::HTTP::Post::Multipart.new uri.path, 'file' => upload, 'path' => path
22
+
23
+ # Set Content-MD5 header.
24
+ # This works around an incompatibility between api_auth and multipart-post.
25
+ # It should no longer be required if https://github.com/mgomes/api_auth/pull/47 is merged.
26
+ body = request.body_stream.read
27
+ request.body_stream.rewind
28
+ request['Content-MD5'] = Digest::MD5.base64digest(body)
29
+
30
+ send request, uri
31
+ end
32
+
33
+ def delete(path)
34
+ uri = url_for(:delete, path: path)
35
+
36
+ send Net::HTTP::Delete.new(uri.request_uri), uri
37
+ end
38
+
39
+ private
40
+
41
+ def url_for(action, params = {})
42
+ URI.join(base_url, "#{VERSION}/files/#{action}").tap do |uri|
43
+ uri.query = URI.encode_www_form(params).presence
44
+ end
45
+ end
46
+
47
+ def send(request, uri)
48
+ ApiAuth.sign! request, access_id, secret_key
49
+
50
+ Net::HTTP.start(uri.host, uri.port) do |http|
51
+ http.use_ssl = true if uri.scheme == 'https'
52
+ http.request(request)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module CarrierwaveRoz
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'carrierwave'
2
+
3
+ module CarrierwaveRoz
4
+ autoload :Client, 'carrierwave_roz/client'
5
+ end
6
+
7
+ CarrierWave::Storage.autoload :Roz, 'carrierwave/storage/roz'
8
+
9
+ class CarrierWave::Uploader::Base
10
+ add_config :api_base_url
11
+ add_config :files_base_url
12
+ add_config :access_id
13
+ add_config :secret_key
14
+
15
+ configure do |config|
16
+ config.storage_engines[:roz] = "CarrierWave::Storage::Roz"
17
+ config.api_base_url = 'http://roz.dev'
18
+ config.files_base_url = 'http://assets.dev'
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-roz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Crownoble
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multipart-post
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: CarrierWave plugin for storing files in the Roz files API
42
+ email: adam@obledesign.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - MIT-LICENSE
48
+ - README.md
49
+ - lib/carrierwave/storage/roz.rb
50
+ - lib/carrierwave_roz.rb
51
+ - lib/carrierwave_roz/client.rb
52
+ - lib/carrierwave_roz/version.rb
53
+ homepage: https://github.com/biola/carrierwave-roz
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.2.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: CarrierWave storage plugin for Roz
77
+ test_files: []
78
+ has_rdoc: