Skip to content

Commit

Permalink
Fully working with v6
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekOnCoffee committed Feb 18, 2014
1 parent 8a23f64 commit 2e1ef25
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 190 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ group :production do
gem 'unicorn'
end

gem 'sinatra'
gem 'tilt', '~> 1.4.1'
gem 'tilt-jbuilder', require: 'sinatra/jbuilder'

gem 'endpoint_base', :git => '[email protected]:spree/endpoint_base.git'
# :path => '../endpoint_base'
52 changes: 30 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
GIT
remote: [email protected]:spree/endpoint_base.git
revision: 885403ef1a336cf152e056f9a1803bde9d4c2167
revision: 3e237c59812ef0c4a169cb2ed96f874f3a3f64bf
specs:
endpoint_base (2.0)
active_support
endpoint_base (0.1.1)
activesupport
jbuilder
json
sinatra (= 1.3.4)
sinatra-contrib

GEM
remote: https://rubygems.org/
specs:
active_support (3.0.0)
activesupport (= 3.0.0)
activesupport (3.0.0)
activesupport (4.0.2)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.4)
backports (3.3.1)
atomic (1.1.14)
capistrano (2.15.4)
highline
net-scp (>= 1.0.0)
Expand All @@ -26,7 +28,6 @@ GEM
crack (0.3.2)
diff-lcs (1.2.4)
dotenv (0.7.0)
eventmachine (1.0.3)
ffi (1.8.1)
foreman (0.63.0)
dotenv (>= 0.7)
Expand All @@ -45,14 +46,19 @@ GEM
httparty (0.11.0)
multi_json (~> 1.0)
multi_xml (>= 0.5.2)
json (1.8.0)
i18n (0.6.9)
jbuilder (2.0.2)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)
json (1.8.1)
kgio (2.8.0)
listen (1.0.3)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
rb-kqueue (>= 0.2)
lumberjack (1.0.3)
method_source (0.8.1)
minitest (4.7.5)
multi_json (1.7.2)
multi_xml (0.5.3)
net-scp (1.1.0)
Expand All @@ -67,7 +73,7 @@ GEM
method_source (~> 0.8)
slop (~> 3.4)
rack (1.5.2)
rack-protection (1.5.0)
rack-protection (1.5.1)
rack
rack-test (0.6.2)
rack (>= 1.0)
Expand All @@ -85,21 +91,20 @@ GEM
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
sinatra (1.3.4)
sinatra (1.4.3)
rack (~> 1.4)
rack-protection (~> 1.3)
tilt (~> 1.3, >= 1.3.3)
sinatra-contrib (1.3.2)
backports (>= 2.0)
eventmachine
rack-protection
rack-test
sinatra (~> 1.3.0)
tilt (~> 1.3)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.4.4)
terminal-notifier-guard (1.5.3)
thor (0.18.1)
thread_safe (0.1.3)
atomic
tilt (1.4.1)
tilt-jbuilder (0.5.3)
jbuilder
tilt (<= 1.4.1)
tzinfo (0.3.38)
unicorn (4.6.2)
kgio (~> 2.6)
rack
Expand All @@ -121,7 +126,10 @@ DEPENDENCIES
rack-test
rb-fsevent (~> 0.9.1)
rspec
sinatra
terminal-notifier-guard
tilt (~> 1.4.1)
tilt-jbuilder
unicorn
vcr
webmock
44 changes: 14 additions & 30 deletions mandrill_endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
require File.expand_path(File.dirname(__FILE__) + '/lib/mandrill_sender')
Dir['./lib/**/*.rb'].each { |f| require f }

class MandrillEndpoint < EndpointBase
class MandrillEndpoint < EndpointBase::Sinatra::Base

set :logging, true

post '/send_mail' do
post '/send_email' do
# convert variables into Mandrill array / hash format.
#
global_merge_vars = @message[:payload]['email']['variables'].map do |name, value|

global_merge_vars = @payload['email']['variables'].map do |name, value|
{ 'name' => name, 'content' => value }
end

template = @message[:payload]['email']['template']
to_addr = @message[:payload]['email']['to']
from_addr = @message[:payload]['email']['from']
subject = @message[:payload]['email']['subject']
template = @payload['email']['template']
to_addr = @payload['email']['to']
from_addr = @payload['email']['from']
subject = @payload['email']['subject']

# create Mandrill request
#
Expand Down Expand Up @@ -45,31 +45,15 @@ class MandrillEndpoint < EndpointBase
#ugly because it could be a hash or an array
#https://mandrillapp.com/api/docs/messages.html
response = [response.parsed_response].flatten.first

if response.key? 'reject_reason'
response.delete('reject_reason') if response['reject_reason'].nil?
end
reason = response['reject_reason'] || response['reject_reason']

if %w{sent queued}.include?(response['status'])
process_result 200, {
'message_id' => @message[:message_id],
'notifications' => [{
'level' => 'info',
'subject' => "Sent '#{subject}' email to #{to_addr}",
'description' => "Sent '#{subject}' email to #{to_addr}",
'mandrill' => response
}]
}
set_summary "Sent '#{subject}' email to #{to_addr}"
add_value :mandrill_message_id, response["_id"]
process_result 200
else
process_result 200, {
'message_id' => @message[:message_id],
'notifications' => [{
'level' => 'warning',
'subject' => "Failed to send '#{subject}' email to #{to_addr}",
'description' => "Failed to send '#{subject}' email to #{to_addr}",
'mandrill' => response
}]
}
set_summary "Failed to send '#{subject}' email to #{to_addr} - #{reason}"
process_result 200
end
end

Expand Down
137 changes: 0 additions & 137 deletions public/endpoint.json

This file was deleted.

26 changes: 25 additions & 1 deletion spec/mandrill_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,31 @@ def app
described_class
end

let(:payload) { {} }
let(:payload) {
'{"request_id": "12e12341523e449c3000001",
"parameters": {
"mandrill.api_key":"abc123"},
"email": {
"to": "[email protected]",
"from": "[email protected]",
"subject": "Order R123456 was shipped!",
"template": "order_confirmation",
"variables": {
"customer.name": "John Smith",
"order.total": "100.00",
"order.tracking": "XYZ123"
}
}
}'
}

it "should respond to POST send_email" do
VCR.use_cassette('mail_chimp_send') do
post '/send_email', payload, auth
last_response.status.should == 200
last_response.body.should match /Sent 'Order R123456 was shipped!' email/
end
end

end

Loading

0 comments on commit 2e1ef25

Please sign in to comment.