incoming 0.1.4 → 0.1.5

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/README.md CHANGED
@@ -117,6 +117,38 @@ result = EmailReceiver.receive(req) # => Got message from whoever@wherever.com w
117
117
  # /etc/mail/aliases
118
118
  http_post: "|http_post -s 6d7e5337a0cd69f52c3fcf9f5af438b1 http://www.example.com/emails"
119
119
  ```
120
+ ## Qmail example:
121
+
122
+ ```ruby
123
+ class EmailReceiver < Incoming::Strategies::HTTPPost
124
+ setup :secret => '6d7e5337a0cd69f52c3fcf9f5af438b1'
125
+
126
+ def receive(mail)
127
+ puts %(Got message from #{mail.to.first} with subject "#{mail.subject}")
128
+ end
129
+ end
130
+
131
+ req = Rack::Request.new(env)
132
+ result = EmailReceiver.receive(req) # => Got message from whoever@wherever.com with subject "hello world"
133
+ ```
134
+
135
+ To setup a *global* incoming email alias:
136
+
137
+ ```
138
+ # /var/qmail/alias/.qmail-whoever - mails to whoever@ will be delivered to this alias.
139
+ |http_post -s 6d7e5337a0cd69f52c3fcf9f5af438b1 http://www.example.com/emails
140
+ ```
141
+
142
+ Domain-specific incoming aliases can be set as follows:
143
+
144
+ ```
145
+ #/var/qmail/control/virtualdomains
146
+ example.com:example
147
+
148
+ #~example/.qmail-whoever
149
+ |http_post -s 6d7e5337a0cd69f52c3fcf9f5af438b1 http://www.example.com/emails
150
+ ```
151
+ Now mails to `whoever@example.com` will be posted to the corresponding URL above. To post all mails for `example.com`, just add the above line to `~example/.qmail-default`.
120
152
 
121
153
  ## Example Rails controller
122
154
 
@@ -2,7 +2,7 @@ require 'mail'
2
2
  require 'incoming/strategy'
3
3
 
4
4
  module Incoming
5
- VERSION = '0.1.4'
5
+ VERSION = '0.1.5'
6
6
 
7
7
  module Strategies
8
8
  autoload :SendGrid, 'incoming/strategies/sendgrid'
@@ -41,11 +41,17 @@ module Incoming
41
41
  html_content = params[ self.class.default_options[:stripped] ? 'stripped-html' : 'body-html' ]
42
42
  text_content = params[ self.class.default_options[:stripped] ? 'stripped-text' : 'body-plain' ]
43
43
 
44
+ if self.class.default_options[:stripped] && text_content.to_s == ''
45
+ html_content = params['body-html']
46
+ text_content = params['body-plain']
47
+ end
48
+
49
+ attachments = 1.upto(params['attachment-count'].to_i).map do |num|
50
+ attachment_from_params(params["attachment-#{num}"])
51
+ end
52
+
44
53
  @message = Mail.new do
45
54
  headers Hash[JSON.parse(params['message-headers'])]
46
- from params['from']
47
- to params['recipient']
48
- subject params['subject']
49
55
 
50
56
  body text_content
51
57
 
@@ -54,9 +60,8 @@ module Incoming
54
60
  body html_content
55
61
  end if html_content
56
62
 
57
- 1.upto(params['attachment-count'].to_i).each do |num|
58
- attachment = params["attachment-#{num}"]
59
- add_file(:filename => attachment.original_filename, :content => attachment.read)
63
+ attachments.each do |attachment|
64
+ add_file(attachment)
60
65
  end
61
66
  end
62
67
  end
@@ -13,10 +13,6 @@ module Incoming
13
13
 
14
14
  @message = Mail.new do
15
15
  headers email.headers
16
- from email.from
17
- to email.to
18
- reply_to email.reply_to
19
- subject email.subject
20
16
 
21
17
  body email.text_body
22
18
 
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module Incoming
2
4
  module Strategies
3
5
  class SendGrid
@@ -5,16 +7,16 @@ module Incoming
5
7
 
6
8
  def initialize(request)
7
9
  params = request.params.dup
8
- envelope = JSON.parse(params['envelope'])
9
10
 
10
11
  # TODO: Properly handle encodings
11
12
  # encodings = JSON.parse(params['charsets'])
12
13
 
14
+ attachments = 1.upto(params['attachments'].to_i).map do |num|
15
+ attachment_from_params(params["attachment#{num}"])
16
+ end
17
+
13
18
  @message = Mail.new do
14
19
  header params['headers']
15
- from params['from']
16
- to envelope['to'].first
17
- subject params['subject']
18
20
 
19
21
  body params['text']
20
22
 
@@ -23,9 +25,8 @@ module Incoming
23
25
  body params['html']
24
26
  end if params['html']
25
27
 
26
- 1.upto(params['attachments'].to_i).each do |num|
27
- attachment = params["attachment#{num}"]
28
- add_file(:filename => attachment.original_filename, :content => attachment.read)
28
+ attachments.each do |attachment|
29
+ add_file(attachment)
29
30
  end
30
31
  end
31
32
  end
@@ -72,9 +72,7 @@ module Incoming
72
72
  # Public: Translates arguments into a Mail::Message object
73
73
  def initialize(*args) ; end
74
74
 
75
- protected
76
-
77
- # Protected: Evaluates message and performs appropriate action.
75
+ # Public: Evaluates message and performs appropriate action.
78
76
  # Override in subclass
79
77
  #
80
78
  # mail - A Mail::Message object
@@ -84,7 +82,7 @@ module Incoming
84
82
  raise NotImplementedError.new('You must implement #receive')
85
83
  end
86
84
 
87
- # Protected: Authenticates request before performing #receive
85
+ # Public: Authenticates request before performing #receive
88
86
  #
89
87
  # Examples:
90
88
  #
@@ -102,5 +100,27 @@ module Incoming
102
100
  def authenticate
103
101
  true
104
102
  end
103
+
104
+ protected
105
+
106
+ # Protected: Normalize file from params
107
+ #
108
+ # uploaded_file_or_hash - ActionController::UploadedFile,
109
+ # ActionDispatch::Http::UploadedFile, or Hash
110
+ #
111
+ # Returns Hash for Mail::Message#add_file
112
+ def attachment_from_params(uploaded_file_or_hash)
113
+ filename, content = if Hash === uploaded_file_or_hash
114
+ [uploaded_file_or_hash['filename'], uploaded_file_or_hash['tempfile'].read]
115
+ else
116
+ [uploaded_file_or_hash.original_filename, uploaded_file_or_hash.read]
117
+ end
118
+
119
+ {
120
+ :filename => filename,
121
+ :content => content
122
+ }
123
+ end
124
+
105
125
  end
106
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: incoming
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-11 00:00:00.000000000 Z
12
+ date: 2013-05-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mail
@@ -75,38 +75,6 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
- - !ruby/object:Gem::Dependency
79
- name: guard
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: guard-rspec
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
78
  description: Incoming! standardizes various mail parse apis, making it a snap to receive
111
79
  emails through HTTP post hooks.
112
80
  email:
@@ -141,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
109
  version: '0'
142
110
  segments:
143
111
  - 0
144
- hash: -2096037568336777979
112
+ hash: 1004414202407084554
145
113
  required_rubygems_version: !ruby/object:Gem::Requirement
146
114
  none: false
147
115
  requirements:
@@ -150,10 +118,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
118
  version: '0'
151
119
  segments:
152
120
  - 0
153
- hash: -2096037568336777979
121
+ hash: 1004414202407084554
154
122
  requirements: []
155
123
  rubyforge_project:
156
- rubygems_version: 1.8.25
124
+ rubygems_version: 1.8.23
157
125
  signing_key:
158
126
  specification_version: 3
159
127
  summary: Incoming! helps you receive email in your Rack apps.