letter_opener_web_s3 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: dc475bf5a48817f23ffb3ee63d8cc9c3fc97d3dd
4
+ data.tar.gz: cc61d16ae357d63304c91489a74d8382bc27d5ee
5
+ SHA512:
6
+ metadata.gz: fa662c854c7dbf3a32586ad324ebabc4f2190eb4a89baccb080b18313002886dce85850d873f8622755ebc99e7ffe15fe9e7f339763f3695bb8b7f0410d2c0e2
7
+ data.tar.gz: 8fad276e7d5cd4ff8453ccad4b57bee647905e20d5f467f69ccedee6d955e637865e1a6c5b59dfc5087710fc9b4ea4469c5a4ba3049834b18f2b154e875eab12
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .ruby-gemset
12
+ .ruby-version
13
+
14
+ .idea
15
+
16
+ *.gem
17
+ # rspec failure tracking
18
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
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 vital.ryabchinskiy@gmail.com. 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 letter_opener_web_s3.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Vital Ryabchinskiy
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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # LetterOpenerWebS3
2
+
3
+ Addon for `letter_opener_web` and `letter_opener` gems to store files on Amazon S3
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'letter_opener_web_s3'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install letter_opener_web_s3
20
+
21
+ ## Usage
22
+
23
+ Configure AWS credentials and domain to rails app assets
24
+
25
+ ```ruby
26
+ LetterOpenerWebS3.configure do |spec|
27
+ spec.s3_config = {
28
+ region: AWS_S3_REGION,
29
+ bucket_name: AWS_S3_BUCKET,
30
+ access_key_id: AWS_S3_KEY_ID,
31
+ secret_access_key: AWS_S3_ACCESS_KEY
32
+ }
33
+ spec.domain = APP_DOMAIN
34
+ end
35
+ ```
36
+
37
+ ## TODO
38
+
39
+ Need check letters with attachments
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/letter_opener_web_s3. 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.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "letter_opener_web_s3"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'letter_opener_web_s3/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "letter_opener_web_s3"
8
+ spec.version = LetterOpenerWebS3::VERSION
9
+ spec.authors = ["Vital Ryabchinskiy"]
10
+ spec.email = ["vital.ryabchinskiy@gmail.com"]
11
+
12
+
13
+ spec.description = "Gives letter_opener and letter_opener_web work with S3"
14
+ spec.summary = spec.description
15
+ spec.homepage = "https://github.com/vitalinfo/letter_opener_web_s3"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec-rails", '~> 3.5'
28
+ spec.add_development_dependency "sqlite3", '~> 1.3'
29
+ spec.add_development_dependency "rails", "~> 4.0"
30
+ spec.add_dependency "letter_opener_web", "~> 1.3"
31
+ spec.add_dependency "aws-sdk", "~> 2"
32
+ end
@@ -0,0 +1,59 @@
1
+ module LetterOpenerWebS3::LetterExtension
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ cattr_accessor :letters_location do
6
+ "#{%w(tmp letter_opener).join('/')}/"
7
+ end
8
+
9
+ class << self
10
+ def search
11
+ letters = LetterOpenerWebS3.bucket.client.list_objects(bucket: LetterOpenerWebS3.bucket.name,
12
+ delimiter: '/',
13
+ prefix: letters_location)
14
+ .common_prefixes.map(&:prefix).map do |folder|
15
+ name = folder.gsub(letters_location, '')[0..-2]
16
+ new id: name, sent_at: Time.at(name.gsub(/\_.+/, '').to_i)
17
+ end
18
+ letters.sort_by(&:sent_at).reverse
19
+ end
20
+
21
+ def destroy_all
22
+ LetterOpenerWebS3.bucket.objects(prefix: letters_location).each(&:delete)
23
+ end
24
+ end
25
+
26
+ def attachments
27
+ @attachments ||= LetterOpenerWebS3.bucket.objects(prefix: File.join(base_dir, 'attachments'))
28
+ .each_with_object({}) do |file, hash|
29
+ hash[File.basename(file)] = File.expand_path(file)
30
+ end
31
+ end
32
+
33
+ def delete
34
+ LetterOpenerWebS3.bucket.objects(prefix: base_dir).each(&:delete)
35
+ end
36
+
37
+ def exists?
38
+ LetterOpenerWebS3.bucket.objects(prefix: base_dir).count > 0
39
+ end
40
+
41
+ private
42
+
43
+ def base_dir
44
+ "#{letters_location}#{id}"
45
+ end
46
+
47
+ def read_file(style)
48
+ Kernel.open(LetterOpenerWebS3.bucket
49
+ .object(File.join(base_dir, "#{style}.html")).presigned_url(:get)).read
50
+ rescue
51
+ ''
52
+ end
53
+
54
+ def style_exists?(style)
55
+ LetterOpenerWebS3.bucket.object(File.join(base_dir, "#{style}.html")).exists?
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,55 @@
1
+ module LetterOpenerWebS3::MessageExtension
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ def initialize(location, mail, part = nil)
6
+ @location = location.gsub("#{Rails.root.to_s}/", '')
7
+ @mail = mail
8
+ @part = part
9
+ @attachments = []
10
+ end
11
+
12
+ def render
13
+ if mail.attachments.any?
14
+ attachments_dir = File.join(@location, 'attachments')
15
+ # TODO: need check render with attachment
16
+ mail.attachments.each do |attachment|
17
+ filename = attachment.filename.gsub(/[^\w.]/, '_')
18
+ path = File.join(attachments_dir, filename)
19
+
20
+ object(path).put(body: attachment.body.raw_source, content_length: attachment.body.raw_source.size)
21
+
22
+ @attachments << [attachment.filename, "attachments/#{URI.escape(filename)}"]
23
+ end
24
+ end
25
+
26
+ str = ERB.new(template).result(binding)
27
+ object(filepath).put(body: str, content_length: str.size)
28
+ end
29
+
30
+ def template
31
+ letter_opener_path = $".select{|f| f.match(/letter_opener\/message.rb/)}.first
32
+ File.read(File.join(letter_opener_path.gsub('message.rb', ''), 'message.html.erb'))
33
+ end
34
+
35
+ def body
36
+ @body ||= begin
37
+ body = (@part || @mail).decoded
38
+
39
+ mail.attachments.each do |attachment|
40
+ body.gsub!(attachment.url, "attachments/#{attachment.filename}")
41
+ end
42
+
43
+ body.gsub!(/=\"\/assets/, "=\"#{URI::join(LetterOpenerWebS3.domain, 'assets')}") if LetterOpenerWebS3.domain
44
+
45
+ body
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def object(filepath)
52
+ LetterOpenerWebS3.bucket.object(filepath)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module LetterOpenerWebS3
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'active_support'
2
+ require 'aws-sdk'
3
+ require 'letter_opener'
4
+ require 'letter_opener_web'
5
+ require "letter_opener_web_s3/version"
6
+ require "letter_opener_web_s3/message_extension"
7
+ require "letter_opener_web_s3/letter_extension"
8
+
9
+ module LetterOpenerWebS3
10
+ mattr_accessor :s3_config
11
+ mattr_accessor :domain
12
+
13
+ # @api private
14
+ class S3ConfigError < StandardError; end
15
+
16
+ # @api private
17
+ class S3CredentialsError < StandardError
18
+ def message
19
+ "Credentials not found"
20
+ end
21
+ end
22
+
23
+ class << self
24
+ # Sets up an S3 backend
25
+ #
26
+ # @s3_config [String] region The AWS region to connect to
27
+ # @s3_config [String] bucket_name The name of the bucket where files will be stored
28
+ # @s3_config [String] access_key_id Access key ID
29
+ # @s3_config [String] secret_access_key Secret access key
30
+ # @domain [String] Domain to assets link
31
+ # @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/Core/Configuration.html
32
+ # @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3.html
33
+ def configure
34
+ yield self
35
+ end
36
+
37
+ def bucket
38
+ raise S3ConfigError if !s3_config.is_a?(Hash) ||
39
+ (s3_config.keys & [:region, :bucket_name, :access_key_id, :secret_access_key]).size != 4
40
+ return @bucket if @bucket
41
+ @s3 = Aws::S3::Resource.new s3_config.slice(:region, :access_key_id, :secret_access_key)
42
+ credentials = @s3.client.config.credentials
43
+ raise S3CredentialsError unless credentials
44
+ @bucket = @s3.bucket s3_config[:bucket_name]
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ ActionDispatch::Reloader.to_prepare(prepend: true) do
52
+ LetterOpener::Message.send :include, LetterOpenerWebS3::MessageExtension
53
+ LetterOpenerWeb::Letter.send :include, LetterOpenerWebS3::LetterExtension
54
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letter_opener_web_s3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vital Ryabchinskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: letter_opener_web
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: aws-sdk
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2'
111
+ description: Gives letter_opener and letter_opener_web work with S3
112
+ email:
113
+ - vital.ryabchinskiy@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - letter_opener_web_s3.gemspec
129
+ - lib/letter_opener_web_s3.rb
130
+ - lib/letter_opener_web_s3/letter_extension.rb
131
+ - lib/letter_opener_web_s3/message_extension.rb
132
+ - lib/letter_opener_web_s3/version.rb
133
+ homepage: https://github.com/vitalinfo/letter_opener_web_s3
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Gives letter_opener and letter_opener_web work with S3
157
+ test_files: []