Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Sep 7, 2011
0 parents commit aac6696
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.gem
.bundle
Gemfile.lock
pkg/*
tmp/*
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--backtrace
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use 1.9.2@letter_opener --create
2 changes: 2 additions & 0 deletions Gemfile
@@ -0,0 +1,2 @@
source "http://rubygems.org"
gemspec
9 changes: 9 additions & 0 deletions Rakefile
@@ -0,0 +1,9 @@
require 'bundler'
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'

desc "Run RSpec"
RSpec::Core::RakeTask.new do |t|
t.verbose = false
end
task :default => :spec
19 changes: 19 additions & 0 deletions letter_opener.gemspec
@@ -0,0 +1,19 @@
Gem::Specification.new do |s|
s.name = "letter_opener"
s.version = "0.0.1"
s.author = "Ryan Bates"
s.email = "ryan@railscasts.com"
s.homepage = "http://github.com/ryanb/letter_opener"
s.summary = "Preview mail in browser instead of sending."
s.description = "When mail is sent from your application, Open Mail will open a preview in the browser instead of sending."

s.files = Dir["{lib,spec}/**/*", "[A-Z]*"] - ["Gemfile.lock"]
s.require_path = "lib"

s.add_dependency 'launchy'
s.add_development_dependency 'rspec', '~> 2.6.0'
s.add_development_dependency 'mail', '~> 2.3.0'

s.rubyforge_project = s.name
s.required_rubygems_version = ">= 1.3.4"
end
3 changes: 3 additions & 0 deletions lib/letter_opener.rb
@@ -0,0 +1,3 @@
require "fileutils"
require "digest/sha1"
require "letter_opener/delivery_method"
17 changes: 17 additions & 0 deletions lib/letter_opener/delivery_method.rb
@@ -0,0 +1,17 @@
module LetterOpener
class DeliveryMethod
def initialize(options = {})
@options = {:location => './letter_opener'}.merge!(options)
end

def deliver!(mail)
identity = Time.now.to_i.to_s + Digest::SHA1.hexdigest(mail.encoded)[0..6]
path = File.join(@options[:location], identity)
FileUtils.mkdir_p(path)
File.open(File.join(path, "index.html"), 'w') do |f|
template = File.expand_path("../views/index.html.erb", __FILE__)
f.write ERB.new(File.read(template)).result(binding)
end
end
end
end
82 changes: 82 additions & 0 deletions lib/letter_opener/views/index.html.erb
@@ -0,0 +1,82 @@
<style type="text/css">
#message_headers {
position: absolute;
top: 0px;
left: 0;
width: 100%;
height: 85px;
padding: 10px 0 0 0;
margin: 0;
background: #fff;
font-size: 12px;
font-family: "Lucida Grande";
border-bottom: 1px solid #dedede;
overflow: hidden;
}

#message_headers dl {
margin: 0;
padding: 0;
}

#message_headers dt {
width: 60px;
padding: 1px;
float: left;
text-align: right;
font-weight: bold;
color: #7f7f7f;
}

#message_headers dd {
margin-left: 70px;
padding: 1px;
}

#message_headers p.alternate {
position: absolute;
top: 0;
right: 15px;
}

#message_headers p.alternate a {
color: #09c;
}

pre#message_body {
padding: 10px;
word-wrap: break-word;
}

body {
margin-top: 96px;
}
</style>

<div id="message_headers">
<dl>
<dt>From:</dt>
<dd><%= mail.from %></dd>

<dt>Subject:</dt>
<dd><strong><%= mail.subject %></strong></dd>

<dt>Date:</dt>
<dd><%= Time.now.strftime("%b %e, %Y %I:%M:%S %p %Z") %></dd>

<dt>To:</dt>
<dd><%= mail.to %></dd>
</dl>

<% if false && mail.multipart? %>
<p class="alternate">
<% if body_part.content_type && body_part.content_type.match(/text\/html/) %>
<a href="<%= name %>.txt">View plain text version</a>
<% else %>
<a href="<%= name %>.html">View HTML version</a>
<% end %>
</p>
<% end %>
</div>

<pre id="message_body"><%= mail.body %></pre>
24 changes: 24 additions & 0 deletions spec/letter_opener/delivery_method_spec.rb
@@ -0,0 +1,24 @@
require "spec_helper"

describe LetterOpener::DeliveryMethod do
before(:each) do
location = File.expand_path('../../../tmp/letter_opener', __FILE__)
FileUtils.rm_rf(location)
Mail.defaults do
delivery_method LetterOpener::DeliveryMethod, :location => location
end
@location = location
end

it "saves text into html document" do
mail = Mail.deliver do
from 'foo@example.com'
to 'bar@example.com'
subject 'Hello'
body 'World!'
end
html = File.read(Dir["#{@location}/*/index.html"].first)
html.should include("Hello")
html.should include("World!")
end
end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,8 @@
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

require "mail"

RSpec.configure do |config|
end

0 comments on commit aac6696

Please sign in to comment.