mailstro 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,14 @@
1
1
  module Mailstro
2
- class ConfigurationError < StandardError; end
3
-
4
2
  class Configuration
3
+ attr_accessor :api_endpoint
5
4
  attr_accessor :api_key
6
- attr_accessor :endpoint
7
- attr_accessor :use_ssl
8
- attr_accessor :api_version
9
5
 
10
6
  def initialize
11
- @use_ssl = true
12
- @endpoint = 'api.mailstroapp.com'
13
- @api_version = 1
7
+ @api_endpoint = 'https://api.mailstroapp.com/v1'
14
8
  end
15
9
 
16
10
  def validate!
17
- if api_key.nil?
18
- raise ConfigurationError, "api_key not provided"
19
- end
11
+ raise Error::ConfigurationError, "api_key not provided" if api_key.nil?
20
12
  end
21
13
  end
22
14
  end
@@ -1,57 +1,21 @@
1
1
  module Mailstro
2
- class Delivery
3
- require 'net/http'
4
- require 'openssl'
5
-
6
- attr_reader :template, :payload
7
-
8
- def self.deliver(template, recipient, payload)
9
- new(template, recipient, payload).deliver
10
- end
11
-
12
- def initialize(template, recipient, payload)
13
- @template = template
14
- @recipient = recipient
15
- @payload = payload
16
- end
17
-
18
- def deliver
19
- http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
20
- http.use_ssl = endpoint_uri.scheme == "https"
21
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
22
-
23
- request = Net::HTTP::Post.new(endpoint_uri.request_uri, { 'Content-Type' =>'application/json' })
24
- request.body = JSON.generate(post_data)
25
-
26
- Response.new(http.request(request)).json_body
27
- end
28
-
29
- def recipient
30
- if @recipient.is_a?(String)
31
- { :email => @recipient }
32
- else
33
- @recipient
34
- end
2
+ class Delivery < Resource
3
+ def self.deliver(template_name, contact_email, payload)
4
+ new(template_name, contact_email, payload).deliver
35
5
  end
36
6
 
37
- private
7
+ attr_reader :contact_email, :template_name
38
8
 
39
- def post_data
40
- {
41
- 'template' => template,
42
- 'recipient' => recipient,
43
- 'payload' => payload,
44
- 'api_key' => Mailstro.configuration.api_key
45
- }
9
+ def initialize(template_name, contact_email, payload)
10
+ @template_name = template_name
11
+ @contact_email = contact_email
12
+ @payload = payload
46
13
  end
47
14
 
48
- def endpoint_uri
49
- @endpoint_uri ||= URI.parse(endpoint)
50
- end
51
-
52
- def endpoint
53
- (Mailstro.configuration.use_ssl ? "https://" : "http://") +
54
- "#{Mailstro.configuration.endpoint}/v#{Mailstro.configuration.api_version}/postman"
15
+ def deliver
16
+ post("deliveries", :template_name => @template_name,
17
+ :contact_email => @contact_email,
18
+ :payload => @payload)
55
19
  end
56
20
  end
57
21
  end
@@ -0,0 +1,9 @@
1
+ require 'faraday'
2
+
3
+ module Mailstro
4
+ module Error
5
+ class ConfigurationError < StandardError; end
6
+ class AuthorisationError < Faraday::Error::ClientError; end
7
+ class ValidationError < StandardError; end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Mailstro
2
+ module Middleware
3
+ module Response
4
+ class RaiseError < Faraday::Response::RaiseError
5
+ def on_complete(env)
6
+ case env[:status]
7
+ when 401
8
+ raise Error::AuthorisationError.new("api_key not authorised", env)
9
+ when 422
10
+ raise Error::ValidationError.new(env[:body][:errors])
11
+ else
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'hashie/mash'
4
+
5
+ require_relative 'middleware/response/raise_error'
6
+
7
+ module Mailstro
8
+ class Resource
9
+ private
10
+
11
+ def params(params)
12
+ params.merge(:api_key => Mailstro.configuration.api_key)
13
+ end
14
+
15
+ def post(path, body = {})
16
+ connection.post(path) do |request|
17
+ request.headers = {
18
+ 'Content-Type' => 'application/json'
19
+ }
20
+ request.body = params(body)
21
+ end.body
22
+ end
23
+
24
+ def connection
25
+ @connection ||= Faraday.new(:url => Mailstro.configuration.api_endpoint) do |faraday|
26
+ faraday.use Mailstro::Middleware::Response::RaiseError
27
+
28
+ faraday.request :url_encoded
29
+ faraday.request :json
30
+
31
+ faraday.response :mashify
32
+ faraday.response :json
33
+
34
+ faraday.adapter Faraday.default_adapter
35
+ end
36
+ end
37
+ end
38
+ end
data/lib/mailstro/test.rb CHANGED
@@ -12,30 +12,30 @@ module Mailstro
12
12
  end
13
13
 
14
14
  def self.enable
15
- def Mailstro.deliver(template, recipient, payload = {})
16
- Mailstro::Test.deliveries << Mailstro::Delivery.new(template, recipient, payload)
15
+ def Mailstro.deliver(template_name, contact_email, payload = {})
16
+ Mailstro::Test.deliveries << Mailstro::Delivery.new(template_name, contact_email, payload)
17
17
  true # insert response here
18
18
  end
19
19
 
20
- def Mailstro.has_delivered?(template)
21
- Mailstro::Test.has_delivered?(template)
20
+ def Mailstro.has_delivered?(template_name)
21
+ Mailstro::Test.has_delivered?(template_name)
22
22
  end
23
23
 
24
24
  @@enabled = true
25
25
  end
26
26
 
27
27
  def self.disable
28
- def Mailstro.deliver(template, recipient, payload = {})
29
- Mailstro::Delivery.deliver(template, recipient, payload)
28
+ def Mailstro.deliver(template_name, contact_email, payload = {})
29
+ Mailstro::Delivery.deliver(template_name, contact_email, payload)
30
30
  end
31
31
 
32
32
  @@enabled = false
33
33
  end
34
34
 
35
- def self.has_delivered?(template)
36
- templates = @@deliveries.map(&:template)
35
+ def self.has_delivered?(template_name)
36
+ templates = @@deliveries.map(&:template_name)
37
37
 
38
- templates.include?(template)
38
+ templates.include?(template_name)
39
39
  end
40
40
  end
41
41
  end
@@ -1,3 +1,3 @@
1
1
  module Mailstro
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/mailstro.rb CHANGED
@@ -1,9 +1,11 @@
1
- require "mailstro/version"
2
- require "mailstro/configuration"
3
- require "mailstro/delivery"
4
- require "mailstro/response"
1
+ require_relative "mailstro/version"
2
+ require_relative "mailstro/configuration"
3
+ require_relative "mailstro/resource"
4
+ require_relative "mailstro/error"
5
+ require_relative "mailstro/delivery"
5
6
 
6
7
  module Mailstro
8
+
7
9
  class << self
8
10
  attr_accessor :configuration
9
11
  end
@@ -14,7 +16,7 @@ module Mailstro
14
16
  configuration.validate!
15
17
  end
16
18
 
17
- def self.deliver(template, recipient, payload = {})
18
- Delivery.deliver(template, recipient, payload)
19
+ def self.deliver(template, contact_email, payload = {})
20
+ Delivery.deliver(template, contact_email, payload)
19
21
  end
20
22
  end
@@ -18,7 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_dependency "faraday"
22
+ spec.add_dependency "faraday_middleware"
23
+ spec.add_dependency "hashie"
24
+ spec.add_development_dependency "bundler"
22
25
  spec.add_development_dependency "dotenv"
23
26
  spec.add_development_dependency "rake"
24
27
  spec.add_development_dependency "rspec"
File without changes
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'posting a delivery', :integration do
4
- let(:template) { :test }
5
- let(:recipient) { { :email => "test@example.com" } }
6
- let(:payload) { { :greeting => "Gday!" } }
4
+ let(:template_name) { :test }
5
+ let(:contact_email) { "test@example.com" }
6
+ let(:payload) { { :greeting => "Gday!" } }
7
7
 
8
8
  before do
9
9
  Mailstro.configure do |config|
@@ -12,26 +12,26 @@ describe 'posting a delivery', :integration do
12
12
  end
13
13
 
14
14
  let(:expected_body) {{
15
- "api_key" => "lolapi",
16
- "template" => "test",
17
- "recipient" => { "email" => "test@example.com" },
18
- "payload" => { "greeting" => "Gday!" }
15
+ "api_key" => "lolapi",
16
+ "template_name" => "test",
17
+ "contact_email" => "test@example.com",
18
+ "payload" => { "greeting" => "Gday!" }
19
19
  }}
20
20
 
21
21
  it "succesfully submits a delivery to mailstro" do
22
- stub_request(:post, "https://api.mailstroapp.com/v1/postman").
23
- with(:body => expected_body).to_return(:status => 200, :body => fixture("postman.json"))
22
+ stub_request(:post, "https://api.mailstroapp.com/v1/deliveries").
23
+ with(:body => expected_body).to_return(:status => 200, :body => fixture("deliveries.json"))
24
24
 
25
- response = Mailstro.deliver(template, recipient, payload)
25
+ response = Mailstro.deliver(template_name, contact_email, payload)
26
26
  response['ok'].should == true
27
27
  end
28
28
 
29
29
  it "raises an error if the api key is not authorized" do
30
- stub_request(:post, "https://api.mailstroapp.com/v1/postman").
30
+ stub_request(:post, "https://api.mailstroapp.com/v1/deliveries").
31
31
  to_return(:status => 401)
32
32
 
33
33
  expect do
34
- Mailstro.deliver(template, recipient, payload)
35
- end.to raise_error(Mailstro::AuthorisationError, "api_key not authorised")
34
+ Mailstro.deliver(template_name, contact_email, payload)
35
+ end.to raise_error(Mailstro::Error::AuthorisationError, "api_key not authorised")
36
36
  end
37
37
  end
@@ -3,15 +3,15 @@ require 'spec_helper'
3
3
  describe Mailstro::Configuration do
4
4
  subject(:configuration) { Mailstro::Configuration.new }
5
5
 
6
- it "has a default endpoint" do
7
- configuration.endpoint.should =~ /mailstro/
6
+ it "has a default api endpoint" do
7
+ configuration.api_endpoint.should == "https://api.mailstroapp.com/v1"
8
8
  end
9
9
 
10
10
  describe "#validate!" do
11
11
  it "raises an error if an api_key is nil" do
12
12
  expect {
13
13
  subject.validate!
14
- }.to raise_error(Mailstro::ConfigurationError)
14
+ }.to raise_error(Mailstro::Error::ConfigurationError)
15
15
  end
16
16
  end
17
17
  end
@@ -10,22 +10,22 @@ describe Mailstro do
10
10
  it "allows you to pass a block to configure Mailstro" do
11
11
  Mailstro.configure do |config|
12
12
  config.api_key = 'x'
13
- config.endpoint = 'mailstro.dev'
13
+ config.api_endpoint = 'mailstro.dev'
14
14
  end
15
15
 
16
- Mailstro.configuration.endpoint.should == 'mailstro.dev'
16
+ Mailstro.configuration.api_endpoint.should == 'mailstro.dev'
17
17
  end
18
18
  end
19
19
 
20
20
  describe ".delivery" do
21
21
  let(:template) { "name" }
22
- let(:recipient) { { :email => "foo@bar.com" } }
22
+ let(:contact) { { :email => "foo@bar.com" } }
23
23
  let(:payload) { { :data => "here" } }
24
24
 
25
25
  it "creates a delivery object and delivers it" do
26
- Mailstro::Delivery.should_receive(:deliver).with(template, recipient, payload)
26
+ Mailstro::Delivery.should_receive(:deliver).with(template, contact, payload)
27
27
 
28
- Mailstro.deliver(template, recipient, payload)
28
+ Mailstro.deliver(template, contact, payload)
29
29
  end
30
30
  end
31
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailstro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,72 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
12
+ date: 2013-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: hashie
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  - !ruby/object:Gem::Dependency
15
63
  name: bundler
16
64
  requirement: !ruby/object:Gem::Requirement
17
65
  none: false
18
66
  requirements:
19
- - - ~>
67
+ - - ! '>='
20
68
  - !ruby/object:Gem::Version
21
- version: '1.3'
69
+ version: '0'
22
70
  type: :development
23
71
  prerelease: false
24
72
  version_requirements: !ruby/object:Gem::Requirement
25
73
  none: false
26
74
  requirements:
27
- - - ~>
75
+ - - ! '>='
28
76
  - !ruby/object:Gem::Version
29
- version: '1.3'
77
+ version: '0'
30
78
  - !ruby/object:Gem::Dependency
31
79
  name: dotenv
32
80
  requirement: !ruby/object:Gem::Requirement
@@ -91,16 +139,16 @@ files:
91
139
  - lib/mailstro.rb
92
140
  - lib/mailstro/configuration.rb
93
141
  - lib/mailstro/delivery.rb
94
- - lib/mailstro/response.rb
142
+ - lib/mailstro/error.rb
143
+ - lib/mailstro/middleware/response/raise_error.rb
144
+ - lib/mailstro/resource.rb
95
145
  - lib/mailstro/rspec.rb
96
146
  - lib/mailstro/test.rb
97
147
  - lib/mailstro/version.rb
98
148
  - mailstro-ruby.gemspec
99
- - spec/fixtures/postman.json
149
+ - spec/fixtures/deliveries.json
100
150
  - spec/integration/posting_a_delivery_spec.rb
101
151
  - spec/mailstro/configuration_spec.rb
102
- - spec/mailstro/delivery_spec.rb
103
- - spec/mailstro/response_spec.rb
104
152
  - spec/mailstro/test_spec.rb
105
153
  - spec/mailstro/version_spec.rb
106
154
  - spec/mailstro_spec.rb
@@ -133,11 +181,9 @@ signing_key:
133
181
  specification_version: 3
134
182
  summary: Ruby notifier for mailstro.co
135
183
  test_files:
136
- - spec/fixtures/postman.json
184
+ - spec/fixtures/deliveries.json
137
185
  - spec/integration/posting_a_delivery_spec.rb
138
186
  - spec/mailstro/configuration_spec.rb
139
- - spec/mailstro/delivery_spec.rb
140
- - spec/mailstro/response_spec.rb
141
187
  - spec/mailstro/test_spec.rb
142
188
  - spec/mailstro/version_spec.rb
143
189
  - spec/mailstro_spec.rb
@@ -1,17 +0,0 @@
1
- module Mailstro
2
- class AuthorisationError < StandardError; end
3
-
4
- class Response
5
- require 'json'
6
-
7
- def initialize(raw)
8
- @raw = raw
9
- end
10
-
11
- def json_body
12
- raise AuthorisationError.new("api_key not authorised") if @raw.code.to_i == 401
13
-
14
- JSON.parse(@raw.body)
15
- end
16
- end
17
- end
@@ -1,65 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mailstro::Delivery do
4
- let(:template) { "name" }
5
- let(:recipient) { { :email => "foo@bar.com" } }
6
- let(:payload) { { :data => "here" } }
7
-
8
- describe '.deliver' do
9
- let(:delivery) { double }
10
-
11
- it "creates an instance and delivers it" do
12
- Mailstro::Delivery.should_receive(:new).with(template, recipient, payload).and_return(delivery)
13
- delivery.should_receive(:deliver)
14
-
15
- Mailstro::Delivery.deliver(template, recipient, payload)
16
- end
17
- end
18
-
19
- describe "#delivery" do
20
- let(:delivery) { Mailstro::Delivery.new(template, recipient, payload) }
21
- let(:request) { double.as_null_object }
22
- let(:response) { double(:body => '{ "status": "OK" }').as_null_object }
23
- let(:http) { double(:request => response).as_null_object }
24
- let(:configuration) { double(:use_ssl => true, :endpoint => 'foo.com', :api_version => '2', :api_key => '1234') }
25
-
26
- before do
27
- Mailstro.stub(:configuration => configuration)
28
- Net::HTTP.should_receive(:new).with('foo.com', 443).and_return(http)
29
- Net::HTTP::Post.should_receive(:new).with('/v2/postman', { "Content-Type"=>"application/json" }).and_return(request)
30
- end
31
-
32
- it "sends a json payload to the mailstro server" do
33
- http.should_receive(:request).with(request).and_return(response)
34
-
35
- delivery.deliver
36
- end
37
-
38
- it "generates the correct payload" do
39
- expected_form_data = {
40
- 'template' => 'name',
41
- 'recipient' => { 'email' => 'foo@bar.com' },
42
- 'payload' => { 'data' => 'here' },
43
- 'api_key' => '1234'
44
- }
45
-
46
- request.should_receive(:body=) do |body|
47
- parsed_body = JSON.parse(body)
48
- parsed_body.should == expected_form_data
49
- end
50
-
51
- delivery.deliver
52
- end
53
-
54
- it "parses the JSON response" do
55
- delivery.deliver['status'].should == 'OK'
56
- end
57
- end
58
-
59
- describe "#recipient" do
60
- it "handles short and long versions" do
61
- Mailstro::Delivery.new(nil, 'a@a.com', nil).recipient.should == { :email => 'a@a.com' }
62
- Mailstro::Delivery.new(nil, { :email => 'a@a.com' }, nil).recipient.should == { :email => 'a@a.com' }
63
- end
64
- end
65
- end
@@ -1,27 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mailstro::Response do
4
- let(:http_response) { double(:code => http_status, :body => http_body) }
5
- let(:http_status) { 200 }
6
- let(:http_body) { '{ "status": "OK" }' }
7
-
8
- subject(:response) { Mailstro::Response.new(http_response) }
9
-
10
- describe "#json_body" do
11
- context "when successful" do
12
- it "returns the html body as JSON" do
13
- response.json_body['status'].should == 'OK'
14
- end
15
- end
16
-
17
- context "when authentication fails" do
18
- let(:http_status) { 401 }
19
-
20
- it "raises an error to alert the user" do
21
- expect {
22
- response.json_body
23
- }.to raise_error(Mailstro::AuthorisationError)
24
- end
25
- end
26
- end
27
- end