griddler-mailgun 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b056d51cbc72ddb86286d5bc79ef3173ac5d266
4
+ data.tar.gz: 7d9753839331a38a462d32a19aff575853aea364
5
+ SHA512:
6
+ metadata.gz: 30e9313154d7578aa97d99db8c24e34e88dffa7284b5da31901b9197da5a126c95ae1cd294d8a9be758fe8b253bd12217203dd9a51672cf382b9d9778d96a8ed
7
+ data.tar.gz: 2ea37a65cacdea9c34d47e2589e2580ef1992305e49874f0acc476bb158e1e2d55f620f5b1a5d8263114208c7975aa0c406bdabe33fdb38ed68302e33f26d785
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'griddler', '1.0.0'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brad Pauly
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Griddler::Mailgun
2
+
3
+ [![Code Climate](https://codeclimate.com/github/bradpauly/griddler-mailgun.png)](https://codeclimate.com/github/bradpauly/griddler-mailgun)
4
+
5
+ This was extracted from the [griddler gem](https://github.com/thoughtbot/griddler) and is used to
6
+ parse emails forwarded to your application from [mailgun](http://mailgun.com/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'griddler'
14
+ gem 'griddler-mailgun'
15
+ ```
16
+
17
+ Then execute:
18
+
19
+ ```
20
+ bundle install
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ [Griddler](http://griddler.io/) comes with a route that, when used, handles the params passed to your application and then sends a Griddler::Email to EmailProcessor#process. Read [a blog post about it](http://robots.thoughtbot.com/handle-incoming-email-with-griddler) on the Thoughtbot blog.
26
+
27
+ #### Getting mailgun to POST to your application
28
+
29
+ Mailgun uses [routes](http://documentation.mailgun.com/user_manual.html#routes) to filter incoming emails and forward them to your app. A route is made up of a filter and an action. [The filter](http://documentation.mailgun.com/api-routes.html#filters) matches emails based on its properties and [the action](http://documentation.mailgun.com/api-routes.html#actions) determines what happens to the matched email.
30
+
31
+ For example, to forward any email sent to an example.net address to http://example.com/email_processor
32
+ the following route would be used:
33
+
34
+ Filter Expression:
35
+
36
+ ```
37
+ match_recipient(".*@example.net")
38
+ ```
39
+
40
+ Action:
41
+
42
+ ```
43
+ forward("http://example.com/email_processor")
44
+ ```
45
+
46
+ ## More Information
47
+
48
+ * [mailgun](http://www.mailgun.com)
49
+ * [mailgun API - Routes](http://documentation.mailgun.com/api-routes.html)
50
+ * [mailgun User Maunual - Routes](http://documentation.mailgun.com/user_manual.html#routes)
51
+
52
+ ## Credits
53
+
54
+ Griddler::Mailgun was extracted from Griddler by Brad Pauly.
55
+
56
+ Griddler was written by Caleb Thompson and Joel Oliveira.
57
+
58
+ ## License
59
+
60
+ Griddler::Mailgun is Copyright © 2014 Brad Pauly. It is free
61
+ software, and may be redistributed under the terms specified
62
+ in the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task default: :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'griddler/mailgun/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "griddler-mailgun"
8
+ spec.version = Griddler::Mailgun::VERSION
9
+ spec.authors = ["Brad Pauly"]
10
+ spec.email = ["hi@bradpauly.com"]
11
+ spec.summary = %q{Mailgun adapter for Griddler}
12
+ spec.description = %q{Adapter to allow the use of mailgun forwarded routes with Griddler.}
13
+ spec.homepage = "https://github.com/bradpauly/griddler-mailgun"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency 'rspec'
23
+
24
+ spec.add_dependency 'griddler'
25
+ end
@@ -0,0 +1,10 @@
1
+ require 'griddler'
2
+ require 'griddler/mailgun/version'
3
+ require 'griddler/mailgun/adapter'
4
+
5
+ module Griddler
6
+ module Mailgun
7
+ end
8
+ end
9
+
10
+ Griddler.adapter_registry.register(:mailgun, Griddler::Mailgun::Adapter)
@@ -0,0 +1,76 @@
1
+ module Griddler
2
+ module Mailgun
3
+ class Adapter
4
+ def initialize(params)
5
+ @params = params
6
+ end
7
+
8
+ def self.normalize_params(params)
9
+ adapter = new(params)
10
+ adapter.normalize_params
11
+ end
12
+
13
+ def normalize_params
14
+ {
15
+ to: to_recipients,
16
+ cc: cc_recipients,
17
+ from: determine_sender,
18
+ subject: params[:subject],
19
+ text: params['body-plain'],
20
+ html: params['body-html'],
21
+ attachments: attachment_files,
22
+ headers: headers
23
+ }
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :params
29
+
30
+ def determine_sender
31
+ sender = param_or_header(:From)
32
+ sender ||= params[:sender]
33
+ end
34
+
35
+ def to_recipients
36
+ to_emails = param_or_header(:To)
37
+ to_emails ||= params[:recipient]
38
+ to_emails.split(',').map(&:strip)
39
+ end
40
+
41
+ def cc_recipients
42
+ cc = param_or_header(:Cc)
43
+ cc.split(',').map(&:strip)
44
+ end
45
+
46
+ def headers
47
+ @headers ||= extract_headers
48
+ end
49
+
50
+ def extract_headers
51
+ return nil unless params['message-headers']
52
+
53
+ extracted_headers = {}
54
+ parsed_headers = JSON.parse(params['message-headers'])
55
+ parsed_headers.each{ |h| extracted_headers[h[0]] = h[1] }
56
+ ActiveSupport::HashWithIndifferentAccess.new(extracted_headers)
57
+ end
58
+
59
+ def param_or_header(key)
60
+ if params[key].present?
61
+ params[key]
62
+ else
63
+ headers[key]
64
+ end
65
+ end
66
+
67
+ def attachment_files
68
+ attachment_count = params['attachment-count'].to_i
69
+
70
+ attachment_count.times.map do |index|
71
+ params.delete("attachment-#{index+1}")
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ module Griddler
2
+ module Mailgun
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
Binary file
Binary file
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ describe Griddler::Mailgun::Adapter do
4
+ it 'registers itself with griddler' do
5
+ Griddler.adapter_registry[:mailgun].should eq Griddler::Mailgun::Adapter
6
+ end
7
+ end
8
+
9
+ describe Griddler::Mailgun::Adapter, '.normalize_params' do
10
+ it 'falls back to headers for cc' do
11
+ params = default_params.merge(Cc: '')
12
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(params)
13
+ expect(normalized_params[:cc]).to eq ["Brandon Stark <brandon@example.com>", "Arya Stark <arya@example.com>"]
14
+ end
15
+
16
+ it 'passes the received array of files' do
17
+ params = default_params.merge(
18
+ 'attachment-count' => 2,
19
+ 'attachment-1' => upload_1,
20
+ 'attachment-2' => upload_2
21
+ )
22
+
23
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(params)
24
+ normalized_params[:attachments].should eq [upload_1, upload_2]
25
+ end
26
+
27
+ it 'has no attachments' do
28
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(default_params)
29
+ normalized_params[:attachments].should be_empty
30
+ end
31
+
32
+ it 'gets sender from headers' do
33
+ params = default_params.merge(From: '')
34
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(params)
35
+ expect(normalized_params[:from]).to eq "Jon Snow <jon@example.com>"
36
+ end
37
+
38
+ it 'falls back to sender without headers or From' do
39
+ params = default_params.merge(From: '', 'message-headers' => '{}')
40
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(params)
41
+ expect(normalized_params[:from]).to eq "jon@example.com"
42
+ end
43
+
44
+ it 'gets full address from headers' do
45
+ params = default_params.merge(To: '')
46
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(params)
47
+ expect(normalized_params[:to]).to eq ["John Doe <johndoe@example.com>", "Jane Doe <janedoe@example.com>"]
48
+ end
49
+
50
+ it 'handles multiple To addresses' do
51
+ params = default_params.merge(
52
+ To: 'Alice Cooper <alice@example.org>, John Doe <john@example.com>'
53
+ )
54
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(params)
55
+ expect(normalized_params[:to]).to eq [
56
+ 'Alice Cooper <alice@example.org>',
57
+ 'John Doe <john@example.com>'
58
+ ]
59
+ end
60
+
61
+ it 'handles missing params' do
62
+ normalized_params = Griddler::Mailgun::Adapter.normalize_params(short_params)
63
+ end
64
+
65
+ def upload_1
66
+ @upload_1 ||= ActionDispatch::Http::UploadedFile.new(
67
+ filename: 'photo1.jpg',
68
+ type: 'image/jpeg',
69
+ tempfile: fixture_file('photo1.jpg')
70
+ )
71
+ end
72
+
73
+ def upload_2
74
+ @upload_2 ||= ActionDispatch::Http::UploadedFile.new(
75
+ filename: 'photo2.jpg',
76
+ type: 'image/jpeg',
77
+ tempfile: fixture_file('photo2.jpg')
78
+ )
79
+ end
80
+
81
+ def fixture_file(file_name)
82
+ cwd = File.expand_path File.dirname(__FILE__)
83
+ File.new(File.join(cwd, '../../', 'fixtures', file_name))
84
+ end
85
+
86
+ def json_headers
87
+ "[
88
+ [\"Subject\", \"multiple recipients and CCs\"],
89
+ [\"From\", \"Jon Snow <jon@example.com>\"],
90
+ [\"To\", \"John Doe <johndoe@example.com>, Jane Doe <janedoe@example.com>\"],
91
+ [\"Cc\", \"Brandon Stark <brandon@example.com>, Arya Stark <arya@example.com>\"]
92
+ ]"
93
+ end
94
+
95
+ def short_params
96
+ params = {
97
+ To: 'Hello World <hi@example.com>',
98
+ From: 'There <there@example.com>',
99
+ Cc: 'emily@example.com',
100
+ 'body-plain' => 'hi',
101
+ }
102
+ end
103
+
104
+ def default_params
105
+ params = ActiveSupport::HashWithIndifferentAccess.new(
106
+ {
107
+ "Cc"=>"Brandon Stark <brandon@example.com>, Arya Stark <arya@example.com>",
108
+ "From"=>"Jon Snow <jon@example.com>",
109
+ "Subject"=>"multiple recipients and CCs",
110
+ "To"=>"John Doe <johndoe@example.com>, Jane Doe <janedoe@example.com>",
111
+ "body-html"=>"<div dir=\"ltr\">And attachments. Two of them. An image and a text file.</div>\r\n",
112
+ "body-plain"=>"And attachments. Two of them. An image and a text file.\r\n",
113
+ "from"=>"Jon Snow <jon@example.com>",
114
+ "recipient"=>"johndoe@example.com",
115
+ "sender"=>"jon@example.com",
116
+ "stripped-html"=>"<div dir=\"ltr\">And attachments. Two of them. An image and a text file.</div>\r\n",
117
+ "stripped-signature"=>"",
118
+ "stripped-text"=>"And attachments. Two of them. An image and a text file.",
119
+ "subject"=>"multiple recipients and CCs",
120
+ "timestamp"=>"1402113646",
121
+ "message-headers"=>json_headers
122
+ }
123
+ )
124
+ end
125
+ end
@@ -0,0 +1,12 @@
1
+ require 'griddler/testing'
2
+ require 'griddler/mailgun'
3
+ require 'action_dispatch'
4
+ require 'active_support/core_ext/string'
5
+ require 'active_support/core_ext/hash'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.order = 'random'
11
+ config.include Griddler::Testing
12
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: griddler-mailgun
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brad Pauly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: griddler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Adapter to allow the use of mailgun forwarded routes with Griddler.
56
+ email:
57
+ - hi@bradpauly.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - griddler-mailgun.gemspec
70
+ - lib/griddler/mailgun.rb
71
+ - lib/griddler/mailgun/adapter.rb
72
+ - lib/griddler/mailgun/version.rb
73
+ - spec/fixtures/photo1.jpg
74
+ - spec/fixtures/photo2.jpg
75
+ - spec/griddler/mailgun/adapter_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: https://github.com/bradpauly/griddler-mailgun
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Mailgun adapter for Griddler
101
+ test_files:
102
+ - spec/fixtures/photo1.jpg
103
+ - spec/fixtures/photo2.jpg
104
+ - spec/griddler/mailgun/adapter_spec.rb
105
+ - spec/spec_helper.rb