griddler 0.5.0 → 0.6.1

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: 3573025fb9bfe09cc1746e637a3992d7349d58e5
4
- data.tar.gz: 1c818804c1f63e95613531fc79afba41c5964213
3
+ metadata.gz: 01c15f81e30fcd63ca2c9cb4bc9e2e8678264c5f
4
+ data.tar.gz: 37a8b24a5ce5b94ef4e7f7d907298abfb3517cd2
5
5
  SHA512:
6
- metadata.gz: c3c628a41d84ee8f0319b0d4016916c60f50b42e97b0358a5772bd79301d67b132b8712525f18aca403b1e6b9636fe28aef1f2d629a1f7c3da969b2ce1e1d19d
7
- data.tar.gz: 951ecc70b7cf18c8b8ef81b303098ff5139a64f15c7fa0c8b70d0d631a44f86b59478c031881b6e1b957ab8e44b07114840a3687a470c14666051f6e0b9470d2
6
+ metadata.gz: 44c21089a2ede6e88242b9e6b24fc38bda03bc0a0dbcbf92e74e0a4a792d000cc374bcbb1da8e99bc922e934dbf8fa4506c164034293ebb482c62c80c1a8ece0
7
+ data.tar.gz: 82704ef5699bc9c2b5c8fa1f274a21f37bb63993f6da44d74f273414d4464c2fea834464afe1f77919c1bd9a88abeb46ea7adeeb4676feaa8aa19562cf2d8a15
data/README.md CHANGED
@@ -31,15 +31,25 @@ Add griddler to your application's Gemfile and run `bundle install`:
31
31
  gem 'griddler'
32
32
  ```
33
33
 
34
- Griddler comes with a default endpoint that will be displayed at the bottom
35
- of the output of `rake routes`. If there is a previously defined route that
36
- matches `/email_processor` -- or you would like to rename the matched path -- you
37
- may add the route to the desired position in routes.rb with the following:
34
+ In `config/routes.rb` you may either use the provided method `mount_griddler`
35
+ or set the route manually. Examples:
38
36
 
39
37
  ```ruby
38
+ # mount using default path
39
+ mount_griddler
40
+
41
+ # mount using a custom path
42
+ mount_griddler('/email/incoming')
43
+
44
+ # the "get off my lawn", DIY approach:
40
45
  post '/email_processor' => 'griddler/emails#create'
41
46
  ```
42
47
 
48
+ **NOTE:** Currently the default route is added to the bottom of your route table.
49
+ By version 0.7.0 it will be removed and you will be required to add the route
50
+ with one of the three above methods.
51
+
52
+
43
53
  Defaults
44
54
  --------
45
55
 
@@ -72,16 +82,31 @@ that responds to:
72
82
 
73
83
  Each of those has some sensible defaults.
74
84
 
75
- `.from`, `.raw_body`, `.raw_headers`, and `.subject` will contain the obvious
85
+ `.raw_body`, `.raw_headers`, and `.subject` will contain the obvious
76
86
  values found in the email, the raw values from those fields.
77
87
 
78
88
  `.body` will contain the full contents of the email body **unless** there is a
79
89
  line in the email containing the string `-- Reply ABOVE THIS LINE --`. In that
80
90
  case `.body` will contain everything before that line.
81
91
 
82
- `.to` will contain all of the text before the email's "@" character. We've found
83
- that this is the most often used portion of the email address and consider it to
84
- be the token we'll key off of for interaction with our application.
92
+ `.to` will contain an array of hashes. Each hash will have the following
93
+ information of each recipient:
94
+
95
+ * `token`: All the text before the email's "@". We've found that this is the
96
+ most often used portion of the email address and consider it to be the token
97
+ we'll key off of for interaction with our application.
98
+
99
+ * `host`: All the text after the email's "@". This is important to filter
100
+ the recipients sent to the application vs emails to other domains. More
101
+ info below on the Upgrading to 0.5.0 section.
102
+
103
+ * `email`: The email address of the recipient.
104
+
105
+ * `full`: The whole recipient field. E.g, `Some User <hello@example.com>`
106
+ * `name`: The name of the recipient. E.g, `Some User`
107
+
108
+ `.from` will default to the `email` value of a hash like `.to`, and can be
109
+ configured to return the full hash.
85
110
 
86
111
  `.attachments` will contain an array of attachments as multipart/form-data files
87
112
  which can be passed off to attachment libraries like Carrierwave or Paperclip.
@@ -98,23 +123,25 @@ are shown below with sample overrides following. In `config/initializer/griddler
98
123
  ```ruby
99
124
  Griddler.configure do |config|
100
125
  config.processor_class = EmailProcessor # MyEmailProcessor
101
- config.to = :token # :full, :email, :hash
126
+ config.to = :hash # :full, :email, :token
127
+ config.from = :email # :full, :token, :hash
102
128
  # :raw => 'AppName <s13.6b2d13dc6a1d33db7644@mail.myapp.com>'
103
129
  # :email => 's13.6b2d13dc6a1d33db7644@mail.myapp.com'
104
130
  # :token => 's13.6b2d13dc6a1d33db7644'
105
- # :hash => { raw: '', email: '', token: '', host: '' }
131
+ # :hash => { raw: [...], email: [...], token: [...], host: [...],
132
+ name: [...] }
106
133
  config.reply_delimiter = '-- REPLY ABOVE THIS LINE --'
107
- config.email_service = :sendgrid
134
+ config.email_service = :sendgrid # :cloudmailin, :postmark, :mandrill
108
135
  end
109
136
  ```
110
137
 
111
138
  * `config.processor_class` is the class Griddler will use to handle your incoming emails.
112
139
  * `config.reply_delimiter` is the string searched for that will split your body.
113
- * `config.to` is the format of the returned value for the `:to` key in
114
- the email object. `:hash` will return all options within a -- (surprise!) -- hash.
115
- * `config.email_service` tells Griddler which email service you are using. The supported
116
- email service options are `:sendgrid` (the default), `:cloudmailin` (expects
117
- multipart format), `:postmark` and `:mandrill`.
140
+ * `config.to` and `config.from` are the format of the returned value for that
141
+ address in the email object. `:hash` will return all options within a -- (surprise!) -- hash.
142
+ * `config.email_service` tells Griddler which email service you are using. The
143
+ supported email service options are `:sendgrid` (the default), `:cloudmailin`
144
+ (expects multipart format), `:postmark` and `:mandrill`.
118
145
 
119
146
  Testing In Your App
120
147
  -------------------
@@ -126,7 +153,7 @@ following sample factory.
126
153
  ```ruby
127
154
  factory :email, class: OpenStruct do
128
155
  # Assumes Griddler.configure.to is :hash (default)
129
- to [{ raw: 'to_user@email.com', email: 'to_user@email.com', token: 'to_user', host: 'email.com' }]
156
+ to [{ full: 'to_user@email.com', email: 'to_user@email.com', token: 'to_user', host: 'email.com', name: nil }]
130
157
  from 'user@email.com'
131
158
  subject 'email subject'
132
159
  body 'Hello!'
data/lib/griddler.rb CHANGED
@@ -5,6 +5,7 @@ require 'griddler/engine'
5
5
  require 'griddler/email'
6
6
  require 'griddler/email_parser'
7
7
  require 'griddler/configuration'
8
+ require 'griddler/route_extensions'
8
9
  require 'griddler/adapters/sendgrid_adapter'
9
10
  require 'griddler/adapters/cloudmailin_adapter'
10
11
  require 'griddler/adapters/postmark_adapter'
@@ -13,7 +13,7 @@ module Griddler
13
13
  def normalize_params
14
14
  {
15
15
  to: extract_recipients,
16
- from: params[:FromFull][:Email],
16
+ from: full_email(params[:FromFull]),
17
17
  subject: params[:Subject],
18
18
  text: params[:TextBody],
19
19
  html: params[:HtmlBody],
@@ -16,7 +16,7 @@ module Griddler
16
16
  end
17
17
 
18
18
  class Configuration
19
- attr_accessor :processor_class, :reply_delimiter
19
+ attr_accessor :processor_class, :reply_delimiter, :from
20
20
 
21
21
  def to
22
22
  @to ||= :hash
@@ -33,6 +33,10 @@ module Griddler
33
33
  @to = type
34
34
  end
35
35
 
36
+ def from
37
+ @from ||= :email
38
+ end
39
+
36
40
  def processor_class
37
41
  @processor_class ||= EmailProcessor
38
42
  end
@@ -46,6 +50,10 @@ module Griddler
46
50
  end
47
51
 
48
52
  def email_service=(new_email_service)
53
+ if new_email_service == :default
54
+ new_email_service = :sendgrid
55
+ end
56
+
49
57
  @email_service_adapter = adapter_class.fetch(new_email_service) { raise Griddler::Errors::EmailServiceAdapterNotFound }
50
58
  end
51
59
 
@@ -10,7 +10,7 @@ module Griddler
10
10
  @params = params
11
11
 
12
12
  @to = recipients
13
- @from = extract_address(params[:from], :email)
13
+ @from = extract_address(params[:from], config.from)
14
14
  @subject = params[:subject]
15
15
 
16
16
  @body = extract_body
@@ -64,8 +64,6 @@ module Griddler
64
64
  clean_text(params[:text])
65
65
  elsif params.key? :html
66
66
  clean_html(params[:html])
67
- else
68
- raise Errors::EmailBodyNotFound
69
67
  end
70
68
  end
71
69
 
@@ -13,12 +13,14 @@ require 'mail'
13
13
  module Griddler::EmailParser
14
14
  def self.parse_address(full_address)
15
15
  email_address = extract_email_address(full_address)
16
+ name = extract_name(full_address)
16
17
  token, host = split_address(email_address)
17
18
  {
18
19
  token: token,
19
20
  host: host,
20
21
  email: email_address,
21
22
  full: full_address,
23
+ name: name,
22
24
  }
23
25
  end
24
26
 
@@ -26,17 +28,13 @@ module Griddler::EmailParser
26
28
  if body.blank?
27
29
  ""
28
30
  else
29
- delimeter = Griddler.configuration.reply_delimiter
30
- body.split(delimeter).first.
31
- split(/^\s*[-]+\s*Original Message\s*[-]+\s*$/).first.
32
- split(/^\s*--\s*$/).first.
33
- gsub(/On.*wrote:/, '').
34
- split(/[\r]*\n/).reject do |line|
35
- line =~ /^\s*>/ ||
31
+ remove_reply_portion(body)
32
+ .split(/[\r]*\n/)
33
+ .reject do |line|
34
+ line =~ /^\s+>/ ||
36
35
  line =~ /^\s*Sent from my /
37
36
  end.
38
37
  join("\n").
39
- gsub(/^\s*On.*\r?\n?\s*.*\s*wrote:$/,'').
40
38
  strip
41
39
  end
42
40
  end
@@ -52,11 +50,40 @@ module Griddler::EmailParser
52
50
 
53
51
  private
54
52
 
53
+ def self.reply_delimeter_regex
54
+ delimiter = Array(Griddler.configuration.reply_delimiter).join('|')
55
+ %r{#{delimiter}}
56
+ end
57
+
55
58
  def self.extract_email_address(full_address)
56
59
  full_address.split('<').last.delete('>').strip
57
60
  end
58
61
 
62
+ def self.extract_name(full_address)
63
+ full_address = full_address.strip
64
+ name = full_address.split('<').first.strip
65
+ if name.present? && name != full_address
66
+ name
67
+ end
68
+ end
69
+
59
70
  def self.split_address(email_address)
60
71
  email_address.try :split, '@'
61
72
  end
73
+
74
+ def self.regex_split_points
75
+ [
76
+ reply_delimeter_regex,
77
+ /^\s*[-]+\s*Original Message\s*[-]+\s*$/,
78
+ /^\s*--\s*$/,
79
+ /On.*wrote:/,
80
+ /^\s*On.*\r?\n?\s*.*\s*wrote:$/
81
+ ]
82
+ end
83
+
84
+ def self.remove_reply_portion(body)
85
+ regex_split_points.inject(body) do |result, split_point|
86
+ result.split(split_point).first || ""
87
+ end
88
+ end
62
89
  end
@@ -1,4 +1,9 @@
1
1
  module Griddler
2
2
  class Engine < ::Rails::Engine
3
+ initializer 'griddler.routes',
4
+ after: 'action_dispatch.prepare_dispatcher' do |app|
5
+
6
+ ActionDispatch::Routing::Mapper.send :include, Griddler::RouteExtensions
7
+ end
3
8
  end
4
9
  end
@@ -3,9 +3,6 @@ module Griddler
3
3
  end
4
4
 
5
5
  module Errors
6
- class EmailBodyNotFound < Griddler::Error
7
- end
8
-
9
6
  class EmailServiceAdapterNotFound < Griddler::Error
10
7
  end
11
8
  end
@@ -0,0 +1,7 @@
1
+ module Griddler
2
+ module RouteExtensions
3
+ def mount_griddler(path='/email_processor')
4
+ post path => 'griddler/emails#create', as: :email_processor
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Griddler
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: griddler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Thompson
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-04-21 00:00:00.000000000 Z
14
+ date: 2013-09-04 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -104,6 +104,7 @@ files:
104
104
  - lib/griddler/email_parser.rb
105
105
  - lib/griddler/engine.rb
106
106
  - lib/griddler/errors.rb
107
+ - lib/griddler/route_extensions.rb
107
108
  - lib/griddler/version.rb
108
109
  - lib/griddler.rb
109
110
  - lib/tasks/griddler_tasks.rake
@@ -133,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
134
  version: '0'
134
135
  requirements: []
135
136
  rubyforge_project:
136
- rubygems_version: 2.0.0
137
+ rubygems_version: 2.0.5
137
138
  signing_key:
138
139
  specification_version: 4
139
140
  summary: SendGrid Parse API client Rails Engine