sendgrid_events 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sendgrid_events (0.0.2)
4
+ sendgrid_events (0.0.3)
5
5
  enumerated_field
6
6
  rails (~> 3.0.15)
7
7
  sendgrid
@@ -1,5 +1,5 @@
1
1
  SendgridEvents::Configure.config do
2
2
  handle %w[processed deferred delivered open click bounce dropped spamreport unsubscribe]
3
- receive = true
4
- mount_at = "/sendgrid_events/"
3
+ receive true
4
+ mount_at "/sendgrid_events/receiver"
5
5
  end
@@ -1,2 +1,2 @@
1
1
 
2
- match "#{SendgridEvents::Configure.mount_at}receiver" => "sendgrid_events/sendgrid_events#receiver", :via => :post
2
+ match SendgridEvents::Configure.mount_at => "sendgrid_events/sendgrid_events#receiver", :via => :post
@@ -14,21 +14,42 @@ module SendgridEvents
14
14
  @prev_migration_nr.to_s
15
15
  end
16
16
 
17
- def install_migrations
17
+ def install
18
+ migrations
19
+ initializer
20
+ routes
21
+ middleware
22
+ end
23
+
24
+ private
25
+
26
+ def migrations
18
27
  puts "Copying Sengrid Events migrations..."
19
- migration_template "sendgrid_events_table.rb", "db/migrate/sendgrid_events_table.rb"
28
+ begin
29
+ migration_template "sendgrid_events_table.rb", "db/migrate/sendgrid_events_table.rb"
30
+ rescue
31
+ puts "Already have that migration"
32
+ end
20
33
  end
21
34
 
22
- def install_initializer
35
+ def initializer
23
36
  puts "Copying initializer..."
24
37
  copy_file "initializer.rb", "config/initializers/sendgrid_events.rb"
25
38
  end
26
39
 
27
- def add_routes
40
+ def routes
41
+ puts "Copying routes..."
28
42
  insert_into_file Rails.root.join('config', 'routes.rb'),
29
43
  File.open(File.join(File.expand_path("../install/templates", __FILE__), 'routes.rb')).read,
30
44
  :after => /Application\.routes\.draw do$/
31
45
  end
46
+
47
+ def middleware
48
+ puts "Copying config..."
49
+ insert_into_file Rails.root.join('config', 'application.rb'), :after => /class Application.*$/ do
50
+ "\n\t\tconfig.middleware.insert_after ActiveRecord::ParamsParser, SendgridEvents::Middleware"
51
+ end
52
+ end
32
53
  end
33
54
  end
34
55
  end
@@ -6,3 +6,4 @@ require 'sendgrid_events/handlers'
6
6
  require 'sendgrid_events/receiver'
7
7
  require 'sendgrid_events/action_mailer_override'
8
8
  require 'sendgrid_events/configure'
9
+ require 'sendgrid_events/middleware'
@@ -6,7 +6,7 @@ module SendgridEvents
6
6
  end
7
7
 
8
8
  def self.registered_handlers
9
- @registered_handlers ||= {}
9
+ @registered_handlers ||= {}.with_indifferent_access
10
10
  end
11
11
 
12
12
  def self.acceptable_handlers
@@ -21,13 +21,13 @@ module SendgridEvents
21
21
  spamreport
22
22
  unsubscribe
23
23
  ]
24
- Hash[array.zip([true] * array.size)].symbolize_keys
24
+ Hash[array.zip([true] * array.size)].with_indifferent_access
25
25
  end
26
26
  end
27
27
 
28
28
  def self.selected_handlers=(handler_list)
29
29
  raise ArgumentError, "Found an unaccetable handler" unless handler_list.all? { |x| acceptable_handlers[x.to_sym] }
30
- @selected_handlers = Hash[handler_list.zip([true] * handler_list.size)].symbolize_keys
30
+ @selected_handlers = Hash[handler_list.zip([true] * handler_list.size)].with_indifferent_access
31
31
  end
32
32
  end
33
33
  end
@@ -0,0 +1,22 @@
1
+ module SendgridEvents
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ scrub(env) if env["REQUEST_URI"].include? Configure.mount_at
9
+ @app.call(env)
10
+ end
11
+
12
+ private
13
+ def scrub(env)
14
+ rack_input = env["rack.input"].read
15
+ rack_input ="[#{rack_input.split("\r\n").join(",")}]"
16
+ env["rack.input"] = StringIO.new(rack_input)
17
+ rescue
18
+ ensure
19
+ env["rack.input"].rewind
20
+ end
21
+ end
22
+ end
@@ -1,18 +1,14 @@
1
1
  module SendgridEvents
2
2
  class Receiver
3
3
  def self.receive(params)
4
- if Configure.receive?
5
- extract_events_and_clean(params).each do |event|
6
- Handlers::Base.choose_and_handle event
4
+ if Configure.receive? and params['_json'] and params['_json'].is_a? Array
5
+ params['_json'].each do |event|
6
+ if event.is_a? Hash and event.has_key? "sendgrid_events_id"
7
+ event = event.with_indifferent_access
8
+ Handlers::Base.choose_and_handle(event)
9
+ end
7
10
  end
8
11
  end
9
12
  end
10
-
11
- private
12
- def self.extract_events_and_clean(params)
13
- params.first.first.split("\n").collect do |json_hash|
14
- JSON.parse(json_hash).symbolize_keys
15
- end
16
- end
17
13
  end
18
14
  end
@@ -1,3 +1,3 @@
1
1
  module SendgridEvents
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -17,7 +17,7 @@ module SendgridEvents
17
17
  } }
18
18
 
19
19
  it "returns the list of handlers" do
20
- subject.registered_handlers.symbolize_keys.keys.sort.should == Dispatch.acceptable_handlers.keys.sort
20
+ subject.registered_handlers.keys.sort.should == Dispatch.acceptable_handlers.keys.sort
21
21
  end
22
22
 
23
23
  it "chose a handler if handler is registered" do
@@ -3,30 +3,17 @@ module SendgridEvents
3
3
  describe Receiver do
4
4
  subject { Receiver }
5
5
  it { should respond_to(:receive) }
6
- it { should respond_to(:extract_events_and_clean) }
7
6
 
8
7
  let(:params) {
9
- {"{\"email\":\"foo@bar.com\",\"timestamp\":1322000095,\"sendgrid_events_id\":\"7\",\"event\":\"delivered\"}\n{\"email\":\"foo@bar.com\",\"timestamp\":1322000096,\"sendgrid_events_id\":\"4\",\"event\":\"delivered\"}" => nil}
8
+ { "_json" => [
9
+ {"email" => "foo@bar.com", "timestamp" => 1322000095,"sendgrid_events_id" => "7", "event" => "delivered"},
10
+ {"email" => "foo@bar.com", "timestamp" => 1322000096, "sendgrid_events_id" => "4", "event" => "delivered"}
11
+ ] }
10
12
  }
11
13
 
12
14
  it "sends the poorly composed params to be cleaned then handles" do
13
15
  Handlers::Base.should_receive(:choose_and_handle).exactly(2).times
14
16
  subject.receive params
15
17
  end
16
-
17
- it "extracts JSON objects" do
18
- subject.extract_events_and_clean(params).should == [{
19
- :email => "foo@bar.com",
20
- :timestamp => 1322000095,
21
- :sendgrid_events_id => '7',
22
- :event => "delivered"
23
- },
24
- {
25
- :email => "foo@bar.com",
26
- :timestamp => 1322000096,
27
- :sendgrid_events_id => "4",
28
- :event => "delivered"
29
- }]
30
- end
31
18
  end
32
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendgrid_events
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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: 2012-07-17 00:00:00.000000000 Z
12
+ date: 2012-07-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -209,6 +209,7 @@ files:
209
209
  - lib/sendgrid_events/handlers/processed.rb
210
210
  - lib/sendgrid_events/handlers/spam_reported.rb
211
211
  - lib/sendgrid_events/handlers/unsubscribed.rb
212
+ - lib/sendgrid_events/middleware.rb
212
213
  - lib/sendgrid_events/receiver.rb
213
214
  - lib/sendgrid_events/version.rb
214
215
  - script/rails
@@ -240,7 +241,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
240
241
  version: '0'
241
242
  segments:
242
243
  - 0
243
- hash: -1020654596744894138
244
+ hash: 143610152345397580
244
245
  required_rubygems_version: !ruby/object:Gem::Requirement
245
246
  none: false
246
247
  requirements:
@@ -249,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
250
  version: '0'
250
251
  segments:
251
252
  - 0
252
- hash: -1020654596744894138
253
+ hash: 143610152345397580
253
254
  requirements: []
254
255
  rubyforge_project:
255
256
  rubygems_version: 1.8.24