freya 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/freya.rb +3 -51
- data/lib/freya/email.rb +45 -0
- data/lib/freya/gmail.rb +23 -0
- data/lib/freya/template.rb +25 -0
- data/lib/freya/version.rb +1 -1
- data/spec/freya_spec.rb +23 -9
- data/spec/spec_helper.rb +15 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 515e1502e619118de4e98afefbb1fffa741b6b8e
|
4
|
+
data.tar.gz: 92d32ab14f439374c40ef72f449fcf1e22f8dafe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05fc4c172a4b0c7e61bce3da6ee2f3e2ed30d4e589a428e12842e22c9babd414e9e28ae53fcf6057c8ee5c623f141d75fd813b7d67e1669267fe33d24b483cd8
|
7
|
+
data.tar.gz: 369b83801ecc30a6fa1c8c429ef4424d05d4e749156178ac372215c46f4de71628147461409f4837bb90cbfec5848618c706ab814e46277b0f7a5a9ec97b1536
|
data/lib/freya.rb
CHANGED
@@ -1,55 +1,7 @@
|
|
1
1
|
require 'freya/version'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
2
|
+
require 'freya/template'
|
3
|
+
require 'freya/email'
|
4
|
+
require 'freya/gmail'
|
5
5
|
|
6
6
|
module Freya
|
7
|
-
class Template
|
8
|
-
def self.config=(config)
|
9
|
-
@config ||= config
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.config
|
13
|
-
@config
|
14
|
-
end
|
15
|
-
|
16
|
-
def initialize
|
17
|
-
return unless File.exists?(File.join(Rails.root, 'config', 'emails.yml'))
|
18
|
-
self.class.config ||= HashWithIndifferentAccess.new(YAML.load(IO.read(File.join(Rails.root, 'config', 'emails.yml'))))
|
19
|
-
end
|
20
|
-
|
21
|
-
def [](name)
|
22
|
-
name.present? ? name.to_s.split('.').inject(self.class.config) { |result, n| result.fetch(n) } : nil
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class Email < OpenStruct
|
27
|
-
def link
|
28
|
-
extras = %w{ cc bcc body subject }.select { |extra| send(extra).present? }.map { |extra| [extra, send(extra)] }.map do |extra|
|
29
|
-
name = extra[0]
|
30
|
-
value = extra[1]
|
31
|
-
|
32
|
-
#cc and #bcc must be repeated for every email in the array
|
33
|
-
[value].flatten.map do |component|
|
34
|
-
"#{name}=#{Rack::Utils.escape_path(component)}"
|
35
|
-
end
|
36
|
-
end.compact
|
37
|
-
|
38
|
-
extras = extras.empty? ? '' : '?' + extras.join('&')
|
39
|
-
|
40
|
-
"#{to}#{extras}"
|
41
|
-
end
|
42
|
-
|
43
|
-
def body
|
44
|
-
Template.new[name]
|
45
|
-
end
|
46
|
-
|
47
|
-
def cc
|
48
|
-
([base_cc].flatten + [self[:cc]].flatten - [to]).compact.uniq
|
49
|
-
end
|
50
|
-
|
51
|
-
def bcc
|
52
|
-
([base_bcc].flatten + [self[:bcc]].flatten - [to]).compact.uniq
|
53
|
-
end
|
54
|
-
end
|
55
7
|
end
|
data/lib/freya/email.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'freya'
|
2
|
+
|
3
|
+
module Freya
|
4
|
+
class Email < OpenStruct
|
5
|
+
def link
|
6
|
+
params = params_mapping.select { |param_name, url_param_name| send(param_name).present? }.map do |param_name, url_param_name|
|
7
|
+
[send(param_name)].flatten.map do |param|
|
8
|
+
"#{url_param_name}=#{Rack::Utils.escape_path(param)}"
|
9
|
+
end
|
10
|
+
end.compact
|
11
|
+
|
12
|
+
base_url + '?' + (params + extra_params).join('&')
|
13
|
+
end
|
14
|
+
|
15
|
+
def body
|
16
|
+
Template.new[name]
|
17
|
+
end
|
18
|
+
|
19
|
+
def cc
|
20
|
+
([base_cc].flatten + [self[:cc]].flatten - [to]).compact.uniq
|
21
|
+
end
|
22
|
+
|
23
|
+
def bcc
|
24
|
+
([base_bcc].flatten + [self[:bcc]].flatten - [to]).compact.uniq
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def param_names
|
30
|
+
%w(cc bcc body subject)
|
31
|
+
end
|
32
|
+
|
33
|
+
def params_mapping
|
34
|
+
param_names.inject({}) { |params, param| params[param] = param; params }
|
35
|
+
end
|
36
|
+
|
37
|
+
def base_url
|
38
|
+
to
|
39
|
+
end
|
40
|
+
|
41
|
+
def extra_params
|
42
|
+
[]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/freya/gmail.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'freya'
|
2
|
+
|
3
|
+
module Freya
|
4
|
+
class Gmail < Email
|
5
|
+
private
|
6
|
+
|
7
|
+
def param_names
|
8
|
+
super << 'to'
|
9
|
+
end
|
10
|
+
|
11
|
+
def params_mapping
|
12
|
+
super.merge({'subject' => 'su'})
|
13
|
+
end
|
14
|
+
|
15
|
+
def base_url
|
16
|
+
'https://mail.google.com/mail'
|
17
|
+
end
|
18
|
+
|
19
|
+
def extra_params
|
20
|
+
['view=cm', 'fs=1']
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'freya'
|
2
|
+
require 'yaml'
|
3
|
+
require 'rails'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
|
6
|
+
module Freya
|
7
|
+
class Template
|
8
|
+
def self.config=(config)
|
9
|
+
@config ||= config
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.config
|
13
|
+
@config
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
return unless File.exists?(File.join(Rails.root, 'config', 'emails.yml'))
|
18
|
+
self.class.config ||= HashWithIndifferentAccess.new(YAML.load(IO.read(File.join(Rails.root, 'config', 'emails.yml'))))
|
19
|
+
end
|
20
|
+
|
21
|
+
def [](name)
|
22
|
+
name.present? ? name.to_s.split('.').inject(self.class.config) { |result, n| result.fetch(n) } : nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/freya/version.rb
CHANGED
data/spec/freya_spec.rb
CHANGED
@@ -1,14 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require 'pry'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
3
|
describe Freya::Email do
|
5
|
-
before do
|
6
|
-
File.stub(exists?: true)
|
7
|
-
Rails.stub(root: 'freya')
|
8
|
-
Freya.send(:remove_const, 'Template')
|
9
|
-
load 'lib/freya.rb'
|
10
|
-
end
|
11
|
-
|
12
4
|
describe 'methods' do
|
13
5
|
before do
|
14
6
|
IO.stub(read:
|
@@ -32,6 +24,8 @@ describe Freya::Email do
|
|
32
24
|
end
|
33
25
|
|
34
26
|
it 'returns a email body based on names separated by point' do
|
27
|
+
reload_template
|
28
|
+
|
35
29
|
IO.stub(read:
|
36
30
|
<<-EOS
|
37
31
|
test:
|
@@ -92,6 +86,8 @@ describe Freya::Email do
|
|
92
86
|
test_email2: This is the second test email
|
93
87
|
EOS
|
94
88
|
)
|
89
|
+
|
90
|
+
reload_template
|
95
91
|
end
|
96
92
|
|
97
93
|
it 'responds to emails defined in emails.yml' do
|
@@ -114,8 +110,26 @@ describe Freya::Email do
|
|
114
110
|
EOS
|
115
111
|
)
|
116
112
|
|
113
|
+
reload_template
|
114
|
+
|
117
115
|
Freya::Template.new[:test1][:email].should eq('This is the first test email')
|
118
116
|
Freya::Template.new[:test2][:email].should eq('This is the second test email')
|
119
117
|
end
|
120
118
|
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe Freya::Gmail do
|
122
|
+
before do
|
123
|
+
IO.stub(read:
|
124
|
+
<<-EOS
|
125
|
+
test_email: This is the test email
|
126
|
+
EOS
|
127
|
+
)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'builds the mail link for gmail if the type is set to gmail' do
|
131
|
+
Freya::Gmail.new(name: 'test_email', subject: 'subject', to: 'test@test.com').link.should eq(
|
132
|
+
'https://mail.google.com/mail?body=This%20is%20the%20test%20email&su=subject&to=test%40test.com&view=cm&fs=1'
|
133
|
+
)
|
134
|
+
end
|
121
135
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'freya'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
def reload_template
|
5
|
+
Freya.send(:remove_const, 'Template')
|
6
|
+
load 'lib/freya/template.rb'
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.before(:each) do
|
11
|
+
File.stub(exists?: true)
|
12
|
+
Rails.stub(root: 'freya')
|
13
|
+
reload_template
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: freya
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matteo Depalo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -94,8 +94,12 @@ files:
|
|
94
94
|
- Rakefile
|
95
95
|
- freya.gemspec
|
96
96
|
- lib/freya.rb
|
97
|
+
- lib/freya/email.rb
|
98
|
+
- lib/freya/gmail.rb
|
99
|
+
- lib/freya/template.rb
|
97
100
|
- lib/freya/version.rb
|
98
101
|
- spec/freya_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
99
103
|
homepage: ''
|
100
104
|
licenses:
|
101
105
|
- MIT
|
@@ -122,3 +126,5 @@ specification_version: 4
|
|
122
126
|
summary: Minimal email generator
|
123
127
|
test_files:
|
124
128
|
- spec/freya_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
has_rdoc:
|