mailercity 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mailercity.rb +10 -5
- data/lib/mailercity/version.rb +1 -1
- data/spec/mailercity_spec.rb +20 -9
- metadata +2 -2
data/lib/mailercity.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "faraday"
|
2
|
+
require "json"
|
2
3
|
require "mailercity/helper"
|
3
4
|
require "mailercity/version"
|
4
5
|
require 'mailercity/errors/authentication_error'
|
@@ -27,16 +28,20 @@ module Mailercity
|
|
27
28
|
@@api_key
|
28
29
|
end
|
29
30
|
|
30
|
-
def self.request(path,
|
31
|
+
def self.request(path, args)
|
31
32
|
raise AuthenticationError.new('No API key provided. (HINT: set your API key using "Mailercity.api_key = <API-KEY>".') unless api_key
|
32
|
-
|
33
|
-
|
33
|
+
payload = {:args => args}
|
34
|
+
response = Faraday.post(api_url(path)) do |req|
|
35
|
+
req.headers['Content-Type'] = 'application/json'
|
36
|
+
req.headers['X-Api-Key'] = Mailercity.api_key
|
37
|
+
req.body = ::JSON.dump(payload)
|
38
|
+
end
|
34
39
|
end
|
35
40
|
|
36
41
|
def self.const_missing(const_name)
|
37
42
|
new_class = Class.new(super_class=Message)
|
38
43
|
|
39
|
-
# Non-obvious: we assign it
|
44
|
+
# Non-obvious: we assign it in order to name it.
|
40
45
|
const_set(const_name, new_class)
|
41
46
|
end
|
42
47
|
|
@@ -61,7 +66,7 @@ module Mailercity
|
|
61
66
|
|
62
67
|
private
|
63
68
|
|
64
|
-
def self.method_missing(method_name, args, &block)
|
69
|
+
def self.method_missing(method_name, *args, &block)
|
65
70
|
new(template: method_name, params: args)
|
66
71
|
end
|
67
72
|
end
|
data/lib/mailercity/version.rb
CHANGED
data/spec/mailercity_spec.rb
CHANGED
@@ -5,29 +5,40 @@ Mailercity.api_base = "http://testhost"
|
|
5
5
|
Mailercity.api_key = "testkey"
|
6
6
|
|
7
7
|
describe Mailercity do
|
8
|
-
def stub_post(url,
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
def stub_post(url, status, *args)
|
9
|
+
payload = {:args => args}
|
10
|
+
body = JSON.dump(payload)
|
11
|
+
api_key = Mailercity.api_key
|
12
|
+
stub_request(:post, url).
|
13
|
+
with(:body => body, :headers => {'X-Api-Key' => api_key, 'Content-Type' => 'application/json'}).
|
14
|
+
to_return(:status => status, :body => "", :headers => {})
|
12
15
|
end
|
13
16
|
|
17
|
+
let(:user) { {"id"=>1, "email"=>"rob@robforman.com", "first_name"=>"Rob", "last_name"=>"Forman"} }
|
18
|
+
let(:account) { {"id"=>1, "name"=>"Awesometown"} }
|
19
|
+
|
14
20
|
it "can create dynamic mailer classes and template methods while passing appropriate parameters" do
|
15
|
-
mail = Mailercity::MyTestMailer.my_test_template(
|
21
|
+
mail = Mailercity::MyTestMailer.my_test_template(user)
|
16
22
|
mail.should be_kind_of Mailercity::Message
|
17
23
|
end
|
18
24
|
|
19
25
|
describe ".deliver" do
|
20
26
|
context "without http error" do
|
21
27
|
it "should post and return true" do
|
22
|
-
stub = stub_post("http://testhost/my_test_mailer/my_test_template",
|
23
|
-
Mailercity::MyTestMailer.my_test_template(
|
28
|
+
stub = stub_post("http://testhost/my_test_mailer/my_test_template", 201, user)
|
29
|
+
Mailercity::MyTestMailer.my_test_template(user).deliver.should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should post multiple values in order" do
|
33
|
+
stub = stub_post("http://testhost/my_test_mailer/my_test_template", 201, user, account)
|
34
|
+
Mailercity::MyTestMailer.my_test_template(user, account).deliver.should == true
|
24
35
|
end
|
25
36
|
end
|
26
37
|
|
27
38
|
context "with http error" do
|
28
39
|
it "should fail to post and return false" do
|
29
|
-
stub = stub_post("http://testhost/my_test_mailer/my_test_template",
|
30
|
-
Mailercity::MyTestMailer.my_test_template(
|
40
|
+
stub = stub_post("http://testhost/my_test_mailer/my_test_template", 403, user)
|
41
|
+
Mailercity::MyTestMailer.my_test_template(user).deliver.should == false
|
31
42
|
end
|
32
43
|
end
|
33
44
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailercity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|