shrine-webdav 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a8ff691d2208dfa2d84ffe07f02a264ced26cbe
4
+ data.tar.gz: c5e50429ba8788a0f5aa35e4e9437cd1dd2bd897
5
+ SHA512:
6
+ metadata.gz: 8294f24e37d5405cabd75cc537f2d484f98d72876d072c558ed5a37c03582eec7860fc46586b3fa9097cbd9ad27f28fdc11dc3db678690269ec1776a5f9b02cc
7
+ data.tar.gz: 16896d69aff2bccc93d0f643594c15dca182be9410807257c3f22a28d05e4a2586ced39a2508e7edcd0d56a67b2b5f401169863035dfb1b327adf046e98346c1
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at i.kushmantsev@fun-box.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shrine-webdav.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ivan Kushmantsev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,103 @@
1
+ # Shrine::Storage::WebDAV
2
+
3
+ Provides a simple WebDAV storage for Shrine.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shrine-webdav'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Suppose you have Report model which should be able to store data in a remote WebDAV storage.
16
+ ```ruby
17
+ class Report < ApplicationRecord
18
+ end
19
+ # == Schema Information
20
+ #
21
+ # Table name: reports
22
+ #
23
+ # id :uuid not null, primary key
24
+ # name :string not null
25
+ # created_at :datetime not null
26
+ # updated_at :datetime not null
27
+ ```
28
+ Before you can add attributes pointing to remote files to your model you should describe Uploader class:
29
+ ```ruby
30
+ # app/models/reports/report_uploader.rb
31
+ class ReportUploader < Shrine
32
+ plugin :activerecord
33
+ plugin :logging, logger: Rails.logger
34
+
35
+ def generate_location(_io, context)
36
+ uuid = context[:record].id
37
+ "#{uuid}.#{context[:name]}"
38
+ end
39
+ end
40
+ ```
41
+ You don't have to override method `generate_location`, but if you didn't you would have random file names.
42
+
43
+ Now you can add the attributes:
44
+ ```ruby
45
+ class Report < ApplicationRecord
46
+ include ReportUploader::Attachment.new(:pdf)
47
+ include ReportUploader::Attachment.new(:xls)
48
+ end
49
+ ```
50
+ Note corresponding migrations:
51
+ ```ruby
52
+ class AddFileAttributes < ActiveRecord::Migration[5.1]
53
+ def change
54
+ add_column :reports, :xls_data, :text
55
+ add_column :reports, :pdf_data, :text
56
+ end
57
+ end
58
+ ```
59
+
60
+ Create file `shrine.rb` in `config/initializers/` to configure WebDAV storage:
61
+ ```ruby
62
+ # config/initializers/shrine.rb
63
+ require 'shrine'
64
+ require "shrine/storage/webdav"
65
+
66
+ Shrine.storages = {
67
+ cache: Shrine::Storage::WebDAV.new(host: 'http://webdav-server.com', prefix: 'your_project/cache'),
68
+ store: Shrine::Storage::WebDAV.new(host: 'http://webdav-server.com', prefix: 'your_project/store')
69
+ }
70
+ ```
71
+
72
+ Now you can use your virtual attributes `pdf` and `xls` like this:
73
+ ```ruby
74
+ report = Report.new(name: 'Senseless report')
75
+
76
+ # here you are going to have file sample.pdf uploaded
77
+ # to "http://webdav-server.com/your_project/cache/#{report.id}.pdf"
78
+ report.pdf = File.open('sample.pdf')
79
+
80
+ # file sample.xls is being uploading
81
+ # to "http://webdav-server.com/your_project/cache/#{report.id}.xls"
82
+ report.xls = File.open('sample.xls')
83
+
84
+ # after commiting in database both files sample.pdf and sample.xls have
85
+ # been uploaded to "http://webdav-server.com/your_project/store/..."
86
+ report.save
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
92
+
93
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `shrine-webdav.gemspec`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/funbox/shrine-webdav. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,70 @@
1
+ require 'shrine'
2
+
3
+ class Shrine
4
+ module Storage
5
+ class WebDAV
6
+ def initialize(host:, prefix: nil)
7
+ @host = host
8
+ mkpath(prefix) unless prefix.nil? || prefix.empty?
9
+ @host = path(host, prefix)
10
+ end
11
+
12
+ def upload(io, id, shrine_metadata: {}, **upload_options)
13
+ mkpath_to_file(id)
14
+ response = HTTP.put(path(@host, id), body: io.read)
15
+ return if (200..299).cover?(response.code.to_i)
16
+ raise "uploading of #{path(@host, id)} failed, the server response was #{response}"
17
+ end
18
+
19
+ def url(id, **options)
20
+ id
21
+ end
22
+
23
+ def open(id)
24
+ tempfile = Tempfile.new('webdav-', binmode: true)
25
+ response = HTTP.get(path(@host, id))
26
+ unless response.code.to_i == 200
27
+ tempfile.close!
28
+ raise "downloading of #{path(@host, id)} failed, the server response was #{response}"
29
+ end
30
+ tempfile.write(response)
31
+ tempfile.open
32
+ tempfile
33
+ end
34
+
35
+ def exists?(id)
36
+ response = HTTP.head(path(@host, id))
37
+ (200..299).cover?(response.code.to_i)
38
+ end
39
+
40
+ def delete(id)
41
+ HTTP.delete(path(@host, id))
42
+ end
43
+
44
+ private
45
+
46
+ def path(host, uri)
47
+ [host, uri].compact.join('/')
48
+ end
49
+
50
+ def mkpath_to_file(path_to_file)
51
+ last_slash = path_to_file.rindex('/')
52
+ path = path_to_file[0..last_slash]
53
+ mkpath(path)
54
+ end
55
+
56
+ def mkpath(path)
57
+ dirs = []
58
+ path.split('/').each do |dir|
59
+ dirs << "#{dirs[-1]}/#{dir}"
60
+ end
61
+ dirs.each do |dir|
62
+ response = HTTP.request(:mkcol, "#{@host}#{dir}")
63
+ unless (200..301).cover?(response.code.to_i)
64
+ raise "creation of directory #{@host}#{dir} failed, the server response was #{response}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'shrine-webdav'
6
+ spec.version = '0.1.0'
7
+ spec.authors = ['Ivan Kushmantsev']
8
+ spec.email = ['i.kushmantsev@fun-box.ru']
9
+
10
+ spec.summary = 'Provides a simple WebDAV storage for Shrine.'
11
+ spec.description = 'Provides a simple WebDAV storage for Shrine.'
12
+ spec.homepage = 'https://github.com/funbox/shrine-webdav'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'shrine', '>= 2.0'
23
+ spec.add_dependency 'http', '~> 2.2', '>= 2.2.2'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.14'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'webmock'
29
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-webdav
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Kushmantsev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shrine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.2.2
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '2.2'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.2.2
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.14'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.14'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: webmock
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: Provides a simple WebDAV storage for Shrine.
104
+ email:
105
+ - i.kushmantsev@fun-box.ru
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".travis.yml"
113
+ - CODE_OF_CONDUCT.md
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - lib/shrine/webdav.rb
119
+ - shrine-webdav.gemspec
120
+ homepage: https://github.com/funbox/shrine-webdav
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Provides a simple WebDAV storage for Shrine.
144
+ test_files: []