griddler-sendgrid 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be5d9a9db3c7daa2c8becdb06c09410bfeea98e1
4
- data.tar.gz: bf3746c3b5e995637e8e54c8e2839ccd281823fb
3
+ metadata.gz: 99daea93402040c7584d5bf32550dc43d26a1189
4
+ data.tar.gz: ddbacdea54e3a9554fde47297f7a3215d1d2346d
5
5
  SHA512:
6
- metadata.gz: 52805795500e3655a05eb660069a094348f767e44940a4182f1ec110b5a0c5793607dbbebcb1ce27d1ce5b6947fbf530327233f2604aa6eabb0dd8cd6a3bd170
7
- data.tar.gz: 46432b91937362c31bc53aa4b62609d823bb7812e2f396c4ec38da585dac8d78bb360c699d99de6f799aeb996e8cf8422bc641221b405f38a3e2871b0ffcf816
6
+ metadata.gz: 966ae6c5066e393f93ba03225330fc7f669d24aff2075aea53c5e8601e7f9264ea7a8b7e504817e74d7ecd3b203fe4eba80124aca55eda187b599e1f23b0e9cb
7
+ data.tar.gz: ce6fe0b401e16bf63123692b09efc57adcdb881198e1460b196f48a90087a4f6d3ba3600b57be86bd495545dd7ceeb5e7b8551d3b322bbb81cefb7001fed0563
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  Griddler::Sendgrid
2
2
  ==================
3
3
 
4
- This is an adapter that allows [Griddler](/thoughtbot/griddler) to be used with
4
+ This is an adapter that allows [Griddler](https://github.com/thoughtbot/griddler) to be used with
5
5
  [SendGrid's Parse API].
6
6
 
7
- [SendGrid's Parse API]: http://sendgrid.com/docs/API%20Reference/Webhooks/parse.html
7
+ [SendGrid's Parse API]: http://sendgrid.com/docs/API_Reference/Webhooks/parse.html
8
8
 
9
9
  Installation
10
10
  ------------
@@ -12,7 +12,6 @@ Installation
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'griddler'
16
15
  gem 'griddler-sendgrid'
17
16
  ```
18
17
 
@@ -20,16 +19,17 @@ Usage
20
19
  -----
21
20
 
22
21
  * SendGrid has done a [great
23
- tutorial](http://blog.sendgrid.com/receiving-email-in-your-rails-app-with-griddler/)
22
+ tutorial](https://sendgrid.com/blog/receiving-email-in-your-rails-app-with-griddler/)
24
23
  on integrating Griddler with your application.
25
24
  * And of course, view our own blog post on the subject over at [Giant
26
25
  Robots](http://robots.thoughtbot.com/handle-incoming-email-with-griddler).
26
+ * *Note:* Make sure to uncheck the "Spam Check" and "Send Raw" checkboxes on the [Parse Webhook settings page](http://sendgrid.com/developer/reply), otherwise the returned parsed email will have the body stripped out.
27
27
 
28
28
  More Information
29
29
  ----------------
30
30
 
31
31
  * [SendGrid](http://www.sendgrid.com)
32
- * [SendGrid Parse API](http://www.sendgrid.com/docs/API Reference/Webhooks/parse.html)
32
+ * [SendGrid Parse API](http://www.sendgrid.com/docs/API_Reference/Webhooks/parse.html)
33
33
 
34
34
  Credits
35
35
  -------
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
 
20
20
  spec.add_development_dependency 'rake'
21
- spec.add_development_dependency 'rspec'
21
+ spec.add_development_dependency 'rspec', '~> 3.5'
22
22
 
23
23
  spec.add_dependency 'griddler'
24
24
  end
@@ -12,8 +12,9 @@ module Griddler
12
12
 
13
13
  def normalize_params
14
14
  params.merge(
15
- to: recipients(:to),
16
- cc: recipients(:cc),
15
+ to: recipients(:to).map(&:format),
16
+ cc: recipients(:cc).map(&:format),
17
+ bcc: get_bcc,
17
18
  attachments: attachment_files,
18
19
  )
19
20
  end
@@ -23,17 +24,48 @@ module Griddler
23
24
  attr_reader :params
24
25
 
25
26
  def recipients(key)
26
- ( params[key] || '' ).split(',')
27
+ Mail::AddressList.new(params[key] || '').addresses
27
28
  end
28
29
 
29
- def attachment_files
30
- params.delete('attachment-info')
31
- attachment_count = params[:attachments].to_i
30
+ def get_bcc
31
+ if bcc = bcc_from_envelope
32
+ bcc - recipients(:to).map(&:address) - recipients(:cc).map(&:address)
33
+ else
34
+ []
35
+ end
36
+ end
37
+
38
+ def bcc_from_envelope
39
+ JSON.parse(params[:envelope])["to"] if params[:envelope].present?
40
+ end
32
41
 
42
+ def attachment_files
33
43
  attachment_count.times.map do |index|
34
- params.delete("attachment#{index + 1}".to_sym)
44
+ extract_file_at(index)
45
+ end
46
+ end
47
+
48
+ def attachment_count
49
+ params[:attachments].to_i
50
+ end
51
+
52
+ def extract_file_at(index)
53
+ filename = attachment_filename(index)
54
+
55
+ params.delete("attachment#{index + 1}".to_sym).tap do |file|
56
+ if filename.present?
57
+ file.original_filename = filename
58
+ end
35
59
  end
36
60
  end
61
+
62
+ def attachment_filename(index)
63
+ attachment_info.fetch("attachment#{index + 1}", {})["filename"]
64
+ end
65
+
66
+ def attachment_info
67
+ @attachment_info ||= JSON.parse(params.delete("attachment-info") || "{}")
68
+ end
37
69
  end
38
70
  end
39
71
  end
@@ -1,5 +1,5 @@
1
1
  module Griddler
2
2
  module Sendgrid
3
- VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -23,15 +23,15 @@ describe Griddler::Sendgrid::Adapter, '.normalize_params' do
23
23
  attachment2: upload_2,
24
24
  'attachment-info' => <<-eojson
25
25
  {
26
- 'attachment2': {
27
- 'filename': 'photo2.jpg',
28
- 'name': 'photo2.jpg',
29
- 'type': 'image/jpeg'
26
+ "attachment2": {
27
+ "filename": "photo2.jpg",
28
+ "name": "photo2.jpg",
29
+ "type": "image/jpeg"
30
30
  },
31
- 'attachment1': {
32
- 'filename': 'photo1.jpg',
33
- 'name': 'photo1.jpg',
34
- 'type': 'image/jpeg'
31
+ "attachment1": {
32
+ "filename": "photo1.jpg",
33
+ "name": "photo1.jpg",
34
+ "type": "image/jpeg"
35
35
  }
36
36
  }
37
37
  eojson
@@ -44,6 +44,33 @@ describe Griddler::Sendgrid::Adapter, '.normalize_params' do
44
44
  normalized_params.should_not have_key(:attachment_info)
45
45
  end
46
46
 
47
+ it "uses sendgrid attachment info for filename" do
48
+ params = default_params.merge(
49
+ attachments: "2",
50
+ attachment1: upload_1,
51
+ attachment2: upload_2,
52
+ "attachment-info" => <<-eojson
53
+ {
54
+ "attachment2": {
55
+ "filename": "sendgrid-filename2.jpg",
56
+ "name": "photo2.jpg",
57
+ "type": "image/jpeg"
58
+ },
59
+ "attachment1": {
60
+ "filename": "sendgrid-filename1.jpg",
61
+ "name": "photo1.jpg",
62
+ "type": "image/jpeg"
63
+ }
64
+ }
65
+ eojson
66
+ )
67
+
68
+ attachments = normalize_params(params)[:attachments]
69
+
70
+ attachments.first.original_filename.should eq "sendgrid-filename1.jpg"
71
+ attachments.second.original_filename.should eq "sendgrid-filename2.jpg"
72
+ end
73
+
47
74
  it 'has no attachments' do
48
75
  params = default_params.merge(attachments: '0')
49
76
 
@@ -51,10 +78,14 @@ describe Griddler::Sendgrid::Adapter, '.normalize_params' do
51
78
  normalized_params[:attachments].should be_empty
52
79
  end
53
80
 
54
- it 'wraps to in an array' do
81
+ it 'splits to into an array' do
55
82
  normalized_params = normalize_params(default_params)
56
83
 
57
- normalized_params[:to].should eq [default_params[:to]]
84
+ normalized_params[:to].should eq [
85
+ '"Mr Fugushima at Fugu, Inc" <hi@example.com>',
86
+ 'Foo bar <foo@example.com>',
87
+ 'no-name@example.com',
88
+ ]
58
89
  end
59
90
 
60
91
  it 'wraps cc in an array' do
@@ -70,12 +101,40 @@ describe Griddler::Sendgrid::Adapter, '.normalize_params' do
70
101
  normalized_params[:cc].should eq []
71
102
  end
72
103
 
104
+ it 'returns an array even if bcc is an empty string' do
105
+ params = default_params.merge(envelope: '')
106
+ normalized_params = normalize_params(params)
107
+
108
+ normalized_params[:bcc].should eq []
109
+ end
110
+
111
+ it 'wraps bcc in an array' do
112
+ normalized_params = normalize_params(default_params)
113
+
114
+ normalized_params[:bcc].should eq ["johny@example.com"]
115
+ end
116
+
117
+ it 'returns an array even if bcc is empty' do
118
+ params = default_params.merge(envelope: nil)
119
+ normalized_params = normalize_params(params)
120
+
121
+ normalized_params[:bcc].should eq []
122
+ end
123
+
124
+ it 'returns an empty array when the envelope to is the same as the base to' do
125
+ params = default_params.merge(envelope: "{\"to\":[\"hi@example.com\"]}")
126
+ normalized_params = normalize_params(params)
127
+
128
+ normalized_params[:bcc].should eq []
129
+ end
130
+
73
131
  def default_params
74
132
  {
75
133
  text: 'hi',
76
- to: 'hi@example.com',
134
+ to: '"Mr Fugushima at Fugu, Inc" <hi@example.com>, Foo bar <foo@example.com>, <no-name@example.com>',
77
135
  cc: 'cc@example.com',
78
136
  from: 'there@example.com',
137
+ envelope: "{\"to\":[\"johny@example.com\"], \"from\": [\"there@example.com\"]}",
79
138
  }
80
139
  end
81
140
  end
data/spec/spec_helper.rb CHANGED
@@ -3,8 +3,10 @@ require 'griddler/sendgrid'
3
3
  require 'action_dispatch'
4
4
 
5
5
  RSpec.configure do |config|
6
- config.treat_symbols_as_metadata_keys_with_true_values = true
7
6
  config.run_all_when_everything_filtered = true
8
7
  config.order = 'random'
9
8
  config.include Griddler::Testing
9
+ config.expect_with(:rspec) do |c|
10
+ c.syntax = [:should, :expect]
11
+ end
10
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: griddler-sendgrid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Thompson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-16 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.5'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3.5'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: griddler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.2.2
96
+ rubygems_version: 2.5.2
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: SendGrid adapter for Griddler