mailstro 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in mailstro-ruby.gemspec
3
+ group :test do
4
+ gem 'webmock'
5
+ end
6
+
4
7
  gemspec
data/Readme.md CHANGED
@@ -33,15 +33,9 @@ TODO: Write a gem description
33
33
  To send an email with the template name `welcome` use the following:
34
34
 
35
35
  ```ruby
36
- Mailstro.deliver(:welcome, :recipient => { :email => "test@example.com" }, :payload => { :greeting => "Hi" })
36
+ Mailstro.deliver(:welcome, "test@example.com", :greeting => "Hi")
37
37
  ```
38
38
 
39
- ## Testing
40
-
41
- Copy the `.env-sample` file, and rename it to `.env`. Here, put in your
42
- Mailstro credentials. This should allow you to run the integration tests
43
- against your account.
44
-
45
39
  ## Contributing
46
40
 
47
41
  1. Fork it
@@ -1,4 +1,6 @@
1
1
  module Mailstro
2
+ class ConfigurationError < StandardError; end
3
+
2
4
  class Configuration
3
5
  attr_accessor :api_key
4
6
  attr_accessor :endpoint
@@ -10,5 +12,11 @@ module Mailstro
10
12
  @endpoint = 'api.mailstroapp.com'
11
13
  @api_version = 1
12
14
  end
15
+
16
+ def validate!
17
+ if api_key.nil?
18
+ raise ConfigurationError, "api_key not provided"
19
+ end
20
+ end
13
21
  end
14
22
  end
@@ -2,18 +2,17 @@ module Mailstro
2
2
  class Delivery
3
3
  require 'net/http'
4
4
  require 'openssl'
5
- require 'json'
6
5
 
7
- attr_reader :template, :recipient, :payload
6
+ attr_reader :template, :payload
8
7
 
9
- def self.deliver(*args)
10
- new(*args).deliver
8
+ def self.deliver(template, recipient, payload)
9
+ new(template, recipient, payload).deliver
11
10
  end
12
11
 
13
- def initialize(template, options)
12
+ def initialize(template, recipient, payload)
14
13
  @template = template
15
- @recipient = options[:recipient]
16
- @payload = options[:payload]
14
+ @recipient = recipient
15
+ @payload = payload
17
16
  end
18
17
 
19
18
  def deliver
@@ -24,24 +23,26 @@ module Mailstro
24
23
  request = Net::HTTP::Post.new(endpoint_uri.request_uri, { 'Content-Type' =>'application/json' })
25
24
  request.body = JSON.generate(post_data)
26
25
 
27
- response = http.request(request)
28
-
29
- # response.status
30
- # response["header-here"] # All headers are lowercase
26
+ Response.new(http.request(request)).json_body
27
+ end
31
28
 
32
- JSON.parse(response.body)
29
+ def recipient
30
+ if @recipient.is_a?(String)
31
+ { :email => @recipient }
32
+ else
33
+ @recipient
34
+ end
33
35
  end
34
36
 
35
37
  private
36
38
 
37
39
  def post_data
38
- { 'template' => template,
40
+ {
41
+ 'template' => template,
39
42
  'recipient' => recipient,
40
- 'api_key' => Mailstro.configuration.api_key }.tap do |payload|
41
- if self.payload.kind_of?(Hash)
42
- payload['payload'] = self.payload
43
- end
44
- end
43
+ 'payload' => payload,
44
+ 'api_key' => Mailstro.configuration.api_key
45
+ }
45
46
  end
46
47
 
47
48
  def endpoint_uri
@@ -0,0 +1,17 @@
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
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(*args)
16
- Mailstro::Test.deliveries << Mailstro::Delivery.new(*args)
15
+ def Mailstro.deliver(template, recipient, payload = {})
16
+ Mailstro::Test.deliveries << Mailstro::Delivery.new(template, recipient, payload)
17
17
  true # insert response here
18
18
  end
19
19
 
20
- def Mailstro.has_delivered?(*args)
21
- Mailstro::Test.has_delivered?(*args)
20
+ def Mailstro.has_delivered?(template)
21
+ Mailstro::Test.has_delivered?(template)
22
22
  end
23
23
 
24
24
  @@enabled = true
25
25
  end
26
26
 
27
27
  def self.disable
28
- def Mailstro.deliver(*args)
29
- Mailstro::Delivery.deliver(*args)
28
+ def Mailstro.deliver(template, recipient, payload = {})
29
+ Mailstro::Delivery.deliver(template, recipient, payload)
30
30
  end
31
31
 
32
32
  @@enabled = false
33
33
  end
34
34
 
35
- def self.has_delivered?(options)
35
+ def self.has_delivered?(template)
36
36
  templates = @@deliveries.map(&:template)
37
37
 
38
- templates.include?(options[:template])
38
+ templates.include?(template)
39
39
  end
40
40
  end
41
41
  end
@@ -1,3 +1,3 @@
1
1
  module Mailstro
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/mailstro.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "mailstro/version"
2
2
  require "mailstro/configuration"
3
3
  require "mailstro/delivery"
4
+ require "mailstro/response"
4
5
 
5
6
  module Mailstro
6
7
  class << self
@@ -10,9 +11,10 @@ module Mailstro
10
11
  def self.configure
11
12
  self.configuration ||= Configuration.new
12
13
  yield(configuration)
14
+ configuration.validate!
13
15
  end
14
16
 
15
- def self.deliver(*args)
16
- Delivery.deliver(*args)
17
+ def self.deliver(template, recipient, payload = {})
18
+ Delivery.deliver(template, recipient, payload)
17
19
  end
18
20
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "dotenv"
22
23
  spec.add_development_dependency "rake"
23
24
  spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "vcr"
25
25
  end
@@ -0,0 +1 @@
1
+ { "ok": true }
@@ -7,15 +7,31 @@ describe 'posting a delivery', :integration do
7
7
 
8
8
  before do
9
9
  Mailstro.configure do |config|
10
- config.use_ssl = ENV['MAILSTRO_USE_SSL'] == 'true'
11
- config.endpoint = ENV['MAILSTRO_ENDPOINT']
12
- config.api_version = ENV['MAILSTRO_API_VERSION']
13
- config.api_key = ENV['MAILSTRO_API_KEY']
10
+ config.api_key = 'lolapi'
14
11
  end
15
12
  end
16
13
 
17
- it "succesfully submits a delivery to mailstro", :vcr do
18
- response = Mailstro.deliver(template, :recipient => recipient, :payload => payload)
14
+ let(:expected_body) {{
15
+ "api_key" => "lolapi",
16
+ "template" => "test",
17
+ "recipient" => { "email" => "test@example.com" },
18
+ "payload" => { "greeting" => "Gday!" }
19
+ }}
20
+
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"))
24
+
25
+ response = Mailstro.deliver(template, recipient, payload)
19
26
  response['ok'].should == true
20
27
  end
28
+
29
+ it "raises an error if the api key is not authorized" do
30
+ stub_request(:post, "https://api.mailstroapp.com/v1/postman").
31
+ to_return(:status => 401)
32
+
33
+ expect do
34
+ Mailstro.deliver(template, recipient, payload)
35
+ end.to raise_error(Mailstro::AuthorisationError, "api_key not authorised")
36
+ end
21
37
  end
@@ -6,4 +6,12 @@ describe Mailstro::Configuration do
6
6
  it "has a default endpoint" do
7
7
  configuration.endpoint.should =~ /mailstro/
8
8
  end
9
+
10
+ describe "#validate!" do
11
+ it "raises an error if an api_key is nil" do
12
+ expect {
13
+ subject.validate!
14
+ }.to raise_error(Mailstro::ConfigurationError)
15
+ end
16
+ end
9
17
  end
@@ -5,19 +5,19 @@ describe Mailstro::Delivery do
5
5
  let(:recipient) { { :email => "foo@bar.com" } }
6
6
  let(:payload) { { :data => "here" } }
7
7
 
8
- describe '.delivery' do
8
+ describe '.deliver' do
9
9
  let(:delivery) { double }
10
10
 
11
- it "creates an instance of the Delivery and delivers it" do
12
- Mailstro::Delivery.should_receive(:new).with(template, :recipient => recipient, :payload => payload).and_return(delivery)
11
+ it "creates an instance and delivers it" do
12
+ Mailstro::Delivery.should_receive(:new).with(template, recipient, payload).and_return(delivery)
13
13
  delivery.should_receive(:deliver)
14
14
 
15
- Mailstro::Delivery.deliver(template, :recipient => recipient, :payload => payload)
15
+ Mailstro::Delivery.deliver(template, recipient, payload)
16
16
  end
17
17
  end
18
18
 
19
19
  describe "#delivery" do
20
- let(:delivery) { Mailstro::Delivery.new(template, :recipient => recipient, :payload => payload) }
20
+ let(:delivery) { Mailstro::Delivery.new(template, recipient, payload) }
21
21
  let(:request) { double.as_null_object }
22
22
  let(:response) { double(:body => '{ "status": "OK" }').as_null_object }
23
23
  let(:http) { double(:request => response).as_null_object }
@@ -55,4 +55,11 @@ describe Mailstro::Delivery do
55
55
  delivery.deliver['status'].should == 'OK'
56
56
  end
57
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
58
65
  end
@@ -0,0 +1,27 @@
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
@@ -12,13 +12,13 @@ describe Mailstro::Test do
12
12
  end
13
13
 
14
14
  it "doesn't send deliveries when enabled" do
15
- Mailstro::Delivery.should_not_receive(:deliver).with(:template_name, {})
15
+ Mailstro::Delivery.should_not_receive(:deliver).with(:template_name, 'a@a.com', {})
16
16
 
17
- Mailstro.deliver(:template_name, {})
17
+ Mailstro.deliver(:template_name, 'a@a.com')
18
18
  end
19
19
 
20
20
  it "adds the delivery to the deliveries array" do
21
- Mailstro.deliver(:template_name, {})
21
+ Mailstro.deliver(:template_name, 'a@a.com')
22
22
 
23
23
  Mailstro::Test.deliveries.should_not be_empty
24
24
  end
@@ -28,9 +28,9 @@ describe Mailstro::Test do
28
28
  it 'restores the previous behaviour of .deliver' do
29
29
  Mailstro::Test.enable
30
30
  Mailstro::Test.disable
31
- Mailstro::Delivery.should_receive(:deliver).with(:template_name, {})
31
+ Mailstro::Delivery.should_receive(:deliver).with(:template_name, 'a@a.com', {})
32
32
 
33
- Mailstro.deliver(:template_name, {})
33
+ Mailstro.deliver(:template_name, 'a@a.com')
34
34
  end
35
35
  end
36
36
 
@@ -38,7 +38,7 @@ describe Mailstro::Test do
38
38
  before do
39
39
  Mailstro::Test.enable
40
40
 
41
- Mailstro.deliver(:template_name, {})
41
+ Mailstro.deliver(:template_name, 'a@a.com')
42
42
  end
43
43
 
44
44
  after do
@@ -63,7 +63,7 @@ describe Mailstro::Test do
63
63
  end
64
64
 
65
65
  it 'returns true if there was a delivery matching the template name give' do
66
- Mailstro.deliver(:template_name, {})
66
+ Mailstro.deliver(:template_name, 'a@a.com')
67
67
 
68
68
  Mailstro.has_delivered?(:template => :template_name)
69
69
  end
@@ -1,9 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mailstro do
4
+
5
+ before do
6
+ Mailstro.configuration = nil
7
+ end
8
+
4
9
  describe ".configure" do
5
10
  it "allows you to pass a block to configure Mailstro" do
6
11
  Mailstro.configure do |config|
12
+ config.api_key = 'x'
7
13
  config.endpoint = 'mailstro.dev'
8
14
  end
9
15
 
@@ -17,9 +23,9 @@ describe Mailstro do
17
23
  let(:payload) { { :data => "here" } }
18
24
 
19
25
  it "creates a delivery object and delivers it" do
20
- Mailstro::Delivery.should_receive(:deliver).with(template, :recipient => recipient, :payload => payload)
26
+ Mailstro::Delivery.should_receive(:deliver).with(template, recipient, payload)
21
27
 
22
- Mailstro.deliver(template, :recipient => recipient, :payload => payload)
28
+ Mailstro.deliver(template, recipient, payload)
23
29
  end
24
30
  end
25
31
  end
@@ -0,0 +1,3 @@
1
+ def fixture(name)
2
+ File.read(File.join(FIXTURES_PATH, name))
3
+ end
@@ -0,0 +1 @@
1
+ require 'webmock/rspec'
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailstro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Keith Pitt
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-05-21 00:00:00.000000000 Z
12
+ date: 2013-07-15 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,50 +22,57 @@ dependencies:
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
26
29
  version: '1.3'
27
30
  - !ruby/object:Gem::Dependency
28
- name: rake
31
+ name: dotenv
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
- name: rspec
47
+ name: rake
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
- name: vcr
63
+ name: rspec
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - '>='
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - '>='
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  description: Ruby notifier for mailstro.co
@@ -73,7 +82,6 @@ executables: []
73
82
  extensions: []
74
83
  extra_rdoc_files: []
75
84
  files:
76
- - .env-sample
77
85
  - .gitignore
78
86
  - .rspec
79
87
  - Gemfile
@@ -83,50 +91,56 @@ files:
83
91
  - lib/mailstro.rb
84
92
  - lib/mailstro/configuration.rb
85
93
  - lib/mailstro/delivery.rb
94
+ - lib/mailstro/response.rb
86
95
  - lib/mailstro/rspec.rb
87
96
  - lib/mailstro/test.rb
88
97
  - lib/mailstro/version.rb
89
- - mailmask-ruby.gemspec
90
- - spec/fixtures/cassettes/posting_a_delivery/succesfully_submits_a_delivery_to_mailstro.yml
98
+ - mailstro-ruby.gemspec
99
+ - spec/fixtures/postman.json
91
100
  - spec/integration/posting_a_delivery_spec.rb
92
101
  - spec/mailstro/configuration_spec.rb
93
102
  - spec/mailstro/delivery_spec.rb
103
+ - spec/mailstro/response_spec.rb
94
104
  - spec/mailstro/test_spec.rb
95
105
  - spec/mailstro/version_spec.rb
96
106
  - spec/mailstro_spec.rb
97
107
  - spec/spec_helper.rb
98
- - spec/support/vcr.rb
108
+ - spec/support/fixtures.rb
109
+ - spec/support/webmock.rb
99
110
  homepage: http://www.mailstro.co
100
111
  licenses:
101
112
  - MIT
102
- metadata: {}
103
113
  post_install_message:
104
114
  rdoc_options: []
105
115
  require_paths:
106
116
  - lib
107
117
  required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
108
119
  requirements:
109
- - - '>='
120
+ - - ! '>='
110
121
  - !ruby/object:Gem::Version
111
122
  version: '0'
112
123
  required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
113
125
  requirements:
114
- - - '>='
126
+ - - ! '>='
115
127
  - !ruby/object:Gem::Version
116
128
  version: '0'
117
129
  requirements: []
118
130
  rubyforge_project:
119
- rubygems_version: 2.0.0
131
+ rubygems_version: 1.8.23
120
132
  signing_key:
121
- specification_version: 4
133
+ specification_version: 3
122
134
  summary: Ruby notifier for mailstro.co
123
135
  test_files:
124
- - spec/fixtures/cassettes/posting_a_delivery/succesfully_submits_a_delivery_to_mailstro.yml
136
+ - spec/fixtures/postman.json
125
137
  - spec/integration/posting_a_delivery_spec.rb
126
138
  - spec/mailstro/configuration_spec.rb
127
139
  - spec/mailstro/delivery_spec.rb
140
+ - spec/mailstro/response_spec.rb
128
141
  - spec/mailstro/test_spec.rb
129
142
  - spec/mailstro/version_spec.rb
130
143
  - spec/mailstro_spec.rb
131
144
  - spec/spec_helper.rb
132
- - spec/support/vcr.rb
145
+ - spec/support/fixtures.rb
146
+ - spec/support/webmock.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: bf855e15283478602f514d022748a103f567ea4d
4
- data.tar.gz: a9574d49338b69186a9d3944a2b2222fa58e4fd5
5
- SHA512:
6
- metadata.gz: 2a529d88fe60006764f3d8219b9c88b271190fab3edd704a6d501e80d3ded776ae58a1c84342c79710626f413ae6a90d619bf676eb046c81f9d72e0d88f4e72b
7
- data.tar.gz: b48beeb0cd6a31fbd2bb913e325832db8235eb499a0ecf00c191b984b7154687244a855660cac1e0ceee2826b567f8b5d5c0c799366b27a27963047506f8327e
data/.env-sample DELETED
@@ -1,4 +0,0 @@
1
- MAILMASK_API_KEY=secret
2
- MAILMASK_ENDPOINT=api.mailmask.co
3
- MAILMASK_API_VERSION=1
4
- MAILMASK_USE_SSL=true
@@ -1,54 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: post
5
- uri: http://ENDPOINT/v1/postman
6
- body:
7
- encoding: UTF-8
8
- string: '{"template":"test","recipient":{"email":"test@example.com"},"api_key":"API_KEY","payload":{"greeting":"Gday!"}}'
9
- headers:
10
- Content-Type:
11
- - application/json
12
- Accept-Encoding:
13
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
- Accept:
15
- - '*/*'
16
- User-Agent:
17
- - Ruby
18
- response:
19
- status:
20
- code: 200
21
- message: OK
22
- headers:
23
- X-Frame-Options:
24
- - SAMEORIGIN
25
- X-Xss-Protection:
26
- - 1; mode=block
27
- X-Content-Type-Options:
28
- - nosniff
29
- X-Ua-Compatible:
30
- - chrome=1
31
- X-Xhr-Current-Location:
32
- - /v1/postman
33
- Content-Type:
34
- - application/json; charset=utf-8
35
- Etag:
36
- - '"82380d1e263b6093f3c7535690fcdd75"'
37
- Cache-Control:
38
- - max-age=0, private, must-revalidate
39
- Set-Cookie:
40
- - request_method=POST; path=/
41
- X-Request-Id:
42
- - 7c8249cb-b3d6-4108-8a97-dba45471019a
43
- X-Runtime:
44
- - '0.084302'
45
- Vary:
46
- - Accept-Encoding
47
- Connection:
48
- - close
49
- body:
50
- encoding: UTF-8
51
- string: '{"ok":true}'
52
- http_version:
53
- recorded_at: Tue, 21 May 2013 10:17:29 GMT
54
- recorded_with: VCR 2.5.0
data/spec/support/vcr.rb DELETED
@@ -1,9 +0,0 @@
1
- require 'vcr'
2
-
3
- VCR.configure do |config|
4
- config.cassette_library_dir = FIXTURES_PATH.join('cassettes')
5
- config.hook_into :webmock
6
- config.filter_sensitive_data('API_KEY') { ENV['MAILSTRO_API_KEY'] }
7
- config.filter_sensitive_data('ENDPOINT') { ENV['MAILSTRO_ENDPOINT'] }
8
- config.configure_rspec_metadata!
9
- end