Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed messages not being saved and retrieved #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion letter_opener_web_s3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec-rails", '~> 3.5'
spec.add_development_dependency "activesupport", ">= 4.0"
spec.add_dependency "letter_opener_web", "~> 1.3"
spec.add_dependency "aws-sdk", "~> 2"
spec.add_dependency "aws-sdk-s3", "~> 1.96.1"
end
2 changes: 1 addition & 1 deletion lib/letter_opener_web_s3.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'active_support'
require 'aws-sdk'
require 'aws-sdk-s3'
require 'letter_opener'
require 'letter_opener_web'
require "letter_opener_web_s3/version"
Expand Down
16 changes: 11 additions & 5 deletions lib/letter_opener_web_s3/letter_extension.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'net/http'

module LetterOpenerWebS3::LetterExtension
extend ActiveSupport::Concern

Expand Down Expand Up @@ -34,6 +36,10 @@ def delete
LetterOpenerWebS3.bucket.objects(prefix: base_dir).each(&:delete)
end

def valid?
exists?
end

def exists?
LetterOpenerWebS3.bucket.objects(prefix: base_dir).count > 0
end
Expand All @@ -44,16 +50,16 @@ def base_dir
"#{letters_location}#{id}"
end

def url(style)
LetterOpenerWebS3.bucket.object(File.join(base_dir, "#{style}.html")).presigned_url(:get)
end

def read_file(style)
Kernel.open(LetterOpenerWebS3.bucket
.object(File.join(base_dir, "#{style}.html")).presigned_url(:get)).read
rescue
''
Net::HTTP.get_response(URI.parse(url(style))).body
end

def style_exists?(style)
LetterOpenerWebS3.bucket.object(File.join(base_dir, "#{style}.html")).exists?
end

end
end
11 changes: 4 additions & 7 deletions lib/letter_opener_web_s3/message_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module LetterOpenerWebS3::MessageExtension
extend ActiveSupport::Concern

included do
def initialize(location, mail, part = nil)
def initialize(mail, options = {})
location = options[:location]
@location = location.gsub("#{Rails.root.to_s}/", '')
@mail = mail
@part = part
@part = options[:part]
@template = options[:message_template] || LetterOpener.configuration.message_template
@attachments = []
end

Expand All @@ -26,11 +28,6 @@ def render
object(filepath).put(body: str, content_length: str.size)
end

def template
letter_opener_path = $".select{|f| f.match(/letter_opener\/message.rb/)}.first
File.read(File.join(letter_opener_path.gsub('message.rb', ''), 'message.html.erb'))
end

def body
@body ||= begin
body = (@part || @mail).decoded
Expand Down