lettr 1.1.0 → 1.1.1
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.
- data/init.rb +0 -3
- data/lib/lettr.rb +37 -69
- data/lib/lettr/action_mailer.rb +1 -1
- data/lib/lettr/api_mailing.rb +1 -1
- data/lib/lettr/base.rb +92 -31
- data/lib/lettr/mail_extensions.rb +36 -33
- data/lib/lettr/mailer.rb +3 -0
- data/lib/lettr/railtie.rb +11 -0
- metadata +5 -4
data/init.rb
CHANGED
data/lib/lettr.rb
CHANGED
@@ -1,98 +1,66 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'active_support'
|
3
|
+
require 'rest-client'
|
3
4
|
|
4
5
|
module Lettr
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
7
|
+
autoload :Base, 'lettr/base'
|
8
|
+
autoload :Resource, 'lettr/resource'
|
9
|
+
autoload :Recipient, 'lettr/recipient'
|
10
|
+
autoload :Deliverable, 'lettr/deliverable'
|
11
|
+
autoload :ObjectConverter, 'lettr/object_converter'
|
12
|
+
autoload :ApiMailing, 'lettr/api_mailing'
|
13
|
+
autoload :Delivery, 'lettr/delivery'
|
14
|
+
autoload :Collection, 'lettr/collection'
|
15
|
+
autoload :RenderedMailing, 'lettr/rendered_mailing'
|
16
|
+
autoload :Whitelist, 'lettr/whitelist'
|
17
|
+
autoload :ActionMailer, 'lettr/action_mailer'
|
18
|
+
autoload :Mailer, 'lettr/mailer'
|
19
|
+
autoload :MailExtensions, 'lettr/mail_extensions'
|
20
|
+
#require 'lettr/mail_extensions'
|
21
|
+
#require 'lettr/railtie' if defined? Rails::Railtie
|
19
22
|
|
20
23
|
mattr_accessor :host
|
21
24
|
mattr_accessor :attributes
|
22
25
|
mattr_accessor :protocol
|
23
|
-
mattr_accessor :api_mailings
|
24
26
|
mattr_accessor :api_key
|
25
27
|
|
26
28
|
self.attributes ||= %w{ gender firstname lastname street ccode pcode city }
|
27
|
-
self.protocol ||= '
|
28
|
-
self.host ||= '
|
29
|
-
self.api_mailings = {}
|
29
|
+
self.protocol ||= 'https'
|
30
|
+
self.host ||= 'lettr.de'
|
30
31
|
|
31
32
|
def self.credentials=(credentials)
|
32
|
-
Base.user = credentials[:user]
|
33
|
-
Base.pass = credentials[:pass]
|
33
|
+
Lettr::Base.user = credentials[:user]
|
34
|
+
Lettr::Base.pass = credentials[:pass]
|
34
35
|
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
if recipient.respond_to? attribute
|
41
|
-
rec.send("#{attribute}=", recipient.send(attribute))
|
42
|
-
end
|
43
|
-
end
|
44
|
-
rec.approved = true
|
45
|
-
unless rec.save
|
46
|
-
raise rec.errors.join(' ')
|
37
|
+
class << self
|
38
|
+
%w{ subscribe unsubscribe }.each do |meth|
|
39
|
+
define_method meth do |*args|
|
40
|
+
Lettr::Base.send(meth, *args)
|
47
41
|
end
|
48
|
-
rec
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.unsubscribe(email)
|
52
|
-
Recipient.delete_by_email(email)
|
53
42
|
end
|
54
|
-
|
55
|
-
def self.load_api_mailing_or_fail_loud *args
|
56
|
-
identifier = args[0]
|
57
|
-
api_mailing = self.api_mailings[identifier] ||= ApiMailing.find(identifier)
|
58
|
-
options = args[1]
|
59
|
-
api_mailing.delivery_options = options
|
60
|
-
return api_mailing
|
61
|
-
rescue RestClient::ResourceNotFound => e
|
62
|
-
_create_rendered_mail( *args )
|
63
|
-
end
|
64
|
-
|
65
|
-
def self._check_options_for_rendered_mail! options
|
66
|
-
[:subject, :recipient].each do |opt|
|
67
|
-
raise ArgumentError.new ":#{opt} is required" unless options.has_key?( opt )
|
68
|
-
end
|
69
|
-
raise ArgumentError.new ":html or :text is required" unless (options.has_key?( :text ) || options.has_key?( :html ))
|
70
|
-
end
|
71
|
-
|
72
|
-
def self._create_rendered_mail *args
|
73
|
-
_check_options_for_rendered_mail! args[1]
|
74
|
-
mailing = RenderedMailing.find args[0].to_s
|
75
|
-
mailing.attributes = args[1].merge(:identifier => args[0].to_s)
|
76
|
-
mailing
|
77
|
-
rescue RestClient::ResourceNotFound
|
78
|
-
mailing = RenderedMailing.new args[1].merge(:identifier => args[0].to_s)
|
79
|
-
unless mailing.save
|
80
|
-
raise ArgumentError.new mailing.errors.join(' ')
|
81
|
-
end
|
82
|
-
mailing
|
83
|
-
end
|
84
|
-
|
85
|
-
def self.api_mailings
|
86
|
-
@@api_mailings
|
87
43
|
end
|
88
44
|
|
89
45
|
def self.method_missing *args, &block
|
90
46
|
if args.first.to_s =~ /^to_/
|
91
47
|
super
|
92
48
|
end
|
93
|
-
load_api_mailing_or_fail_loud *args
|
49
|
+
Lettr::Base.load_api_mailing_or_fail_loud *args
|
94
50
|
rescue RestClient::ResourceNotFound
|
95
51
|
super
|
96
52
|
end
|
97
53
|
|
98
54
|
end
|
55
|
+
|
56
|
+
if defined? Rails::Railtie
|
57
|
+
class Lettr::Railtie < Rails::Railtie
|
58
|
+
|
59
|
+
initializer 'lettr.init' do
|
60
|
+
ActiveSupport.on_load(:action_mailer) do
|
61
|
+
ActionMailer::Base.add_delivery_method :lettr, Lettr::MailExtensions::Lettr
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
data/lib/lettr/action_mailer.rb
CHANGED
data/lib/lettr/api_mailing.rb
CHANGED
data/lib/lettr/base.rb
CHANGED
@@ -1,42 +1,103 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
module Lettr
|
3
|
+
class Base
|
4
|
+
DEFAULT_HEADERS = { :accept => :json }
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
+
cattr_accessor :site, :user, :pass, :content_type, :api_mailings
|
7
|
+
attr_reader :client
|
6
8
|
|
7
|
-
|
8
|
-
resource_args = [ self.class.site_url ]
|
9
|
-
resource_args += [self.class.user, self.class.pass] unless Lettr.api_key
|
10
|
-
@client = RestClient::Resource.new *resource_args
|
11
|
-
end
|
9
|
+
self.api_mailings = {}
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def initialize
|
12
|
+
resource_args = [ self.class.site_url ]
|
13
|
+
resource_args += [self.class.user, self.class.pass] unless Lettr.api_key
|
14
|
+
@client = RestClient::Resource.new *resource_args
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
payload.merge! :files => object.files if object.respond_to?(:files) && object.files
|
21
|
-
client[path].post(payload, headers)
|
22
|
-
end
|
17
|
+
def self.site_url
|
18
|
+
"#{Lettr.protocol}://#{Lettr.host}/"
|
19
|
+
end
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
def save object
|
22
|
+
path = object.collection_path
|
23
|
+
payload = object.to_payload
|
24
|
+
payload.merge! :files => object.files if object.respond_to?(:files) && object.files
|
25
|
+
client[path].post(payload, headers)
|
26
|
+
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def destroy object
|
29
|
+
client[object.path].delete headers
|
30
|
+
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
def find path
|
33
|
+
ActiveSupport::JSON.decode(client[path].get headers)
|
34
|
+
end
|
35
|
+
|
36
|
+
def [] path
|
37
|
+
client[path]
|
38
|
+
end
|
39
|
+
|
40
|
+
def headers
|
41
|
+
headers = DEFAULT_HEADERS
|
42
|
+
headers.merge! 'X-Lettr-API-key' => Lettr.api_key if Lettr.api_key
|
43
|
+
headers
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.subscribe(recipient)
|
47
|
+
raise 'Object muss über das Attribut :email verfügen.' unless recipient.respond_to? :email
|
48
|
+
rec = Lettr::Recipient.new recipient.email
|
49
|
+
Lettr.attributes.each do |attribute|
|
50
|
+
if recipient.respond_to? attribute
|
51
|
+
rec.send("#{attribute}=", recipient.send(attribute))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
rec.approved = true
|
55
|
+
unless rec.save
|
56
|
+
raise rec.errors.join(' ')
|
57
|
+
end
|
58
|
+
rec
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.unsubscribe(email)
|
62
|
+
Lettr::Recipient.delete_by_email(email)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.load_api_mailing_or_fail_loud *args
|
66
|
+
identifier = args[0]
|
67
|
+
api_mailing = self.api_mailings[identifier] ||= Lettr::ApiMailing.find(identifier)
|
68
|
+
options = args[1]
|
69
|
+
api_mailing.delivery_options = options
|
70
|
+
return api_mailing
|
71
|
+
rescue RestClient::ResourceNotFound => e
|
72
|
+
_create_rendered_mail( *args )
|
73
|
+
end
|
74
|
+
|
75
|
+
def self._check_options_for_rendered_mail! options
|
76
|
+
[:subject, :recipient].each do |opt|
|
77
|
+
raise ArgumentError.new ":#{opt} is required" unless options.has_key?( opt )
|
78
|
+
end
|
79
|
+
raise ArgumentError.new ":html or :text is required" unless (options.has_key?( :text ) || options.has_key?( :html ))
|
80
|
+
end
|
81
|
+
|
82
|
+
def self._create_rendered_mail *args
|
83
|
+
_check_options_for_rendered_mail! args[1]
|
84
|
+
mailing = Lettr::RenderedMailing.find args[0].to_s
|
85
|
+
mailing.attributes = args[1].merge(:identifier => args[0].to_s)
|
86
|
+
mailing
|
87
|
+
rescue RestClient::ResourceNotFound
|
88
|
+
mailing = Lettr::RenderedMailing.new args[1].merge(:identifier => args[0].to_s)
|
89
|
+
unless mailing.save
|
90
|
+
raise ArgumentError.new mailing.errors.join(' ')
|
91
|
+
end
|
92
|
+
mailing
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.api_mailings
|
96
|
+
@@api_mailings
|
97
|
+
end
|
35
98
|
|
36
|
-
def headers
|
37
|
-
headers = DEFAULT_HEADERS
|
38
|
-
headers.merge! 'X-Lettr-API-key' => Lettr.api_key if Lettr.api_key
|
39
|
-
headers
|
40
99
|
end
|
41
100
|
|
42
101
|
end
|
102
|
+
|
103
|
+
|
@@ -1,44 +1,47 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module Lettr
|
2
|
+
module MailExtensions
|
3
|
+
class Lettr
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(values={})
|
6
|
+
@settings = values
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
lettr_request_hash[:html] = mail.body
|
19
|
-
when /text\/plain/
|
20
|
-
lettr_request_hash[:text] = mail.body
|
21
|
-
end
|
22
|
-
else
|
23
|
-
mail.parts.each do |part|
|
24
|
-
case part.content_type
|
9
|
+
def deliver!(mail)
|
10
|
+
lettr_request_hashes = []
|
11
|
+
raise 'no recipient' unless mail.to
|
12
|
+
raise 'no subject' unless mail.subject
|
13
|
+
mail.to.each do |recipient|
|
14
|
+
lettr_request_hash = {}
|
15
|
+
lettr_request_hash[:recipient] = recipient
|
16
|
+
lettr_request_hash[:subject] = mail.subject
|
17
|
+
if mail.parts.empty?
|
18
|
+
case mail.content_type
|
25
19
|
when /text\/html/
|
26
|
-
lettr_request_hash[:html] =
|
20
|
+
lettr_request_hash[:html] = mail.body
|
27
21
|
when /text\/plain/
|
28
|
-
lettr_request_hash[:text] =
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
lettr_request_hash[:text] = mail.body
|
23
|
+
end
|
24
|
+
else
|
25
|
+
mail.parts.each do |part|
|
26
|
+
case part.content_type
|
27
|
+
when /text\/html/
|
28
|
+
lettr_request_hash[:html] = part.body
|
29
|
+
when /text\/plain/
|
30
|
+
lettr_request_hash[:text] = part.body
|
31
|
+
else
|
32
|
+
lettr_request_hash[:attachments] ||= []
|
33
|
+
lettr_request_hash[:attachments] << part.body
|
34
|
+
end
|
32
35
|
end
|
33
36
|
end
|
37
|
+
lettr_request_hashes << lettr_request_hash
|
38
|
+
end
|
39
|
+
identifier = @settings[:template] || mail.subject.downcase.gsub(/\s/, '_')
|
40
|
+
lettr_request_hashes.each do |lettr_request_hash|
|
41
|
+
::Lettr.send(identifier, lettr_request_hash).deliver
|
34
42
|
end
|
35
|
-
lettr_request_hashes << lettr_request_hash
|
36
|
-
end
|
37
|
-
identifier = @settings[:template] || mail.subject.downcase.gsub(/\s/, '_')
|
38
|
-
lettr_request_hashes.each do |lettr_request_hash|
|
39
|
-
::Lettr.send(identifier, lettr_request_hash).deliver
|
40
43
|
end
|
41
44
|
end
|
42
|
-
end
|
43
45
|
|
46
|
+
end
|
44
47
|
end
|
data/lib/lettr/mailer.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lettr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 1
|
10
|
+
version: 1.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Digineo GmbH
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01
|
18
|
+
date: 2011-02-01 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -102,6 +102,7 @@ extra_rdoc_files: []
|
|
102
102
|
files:
|
103
103
|
- lib/lettr/action_mailer.rb
|
104
104
|
- lib/lettr/whitelist.rb
|
105
|
+
- lib/lettr/railtie.rb
|
105
106
|
- lib/lettr/collection.rb
|
106
107
|
- lib/lettr/resource.rb
|
107
108
|
- lib/lettr/object_converter.rb
|