Class: Gridhook::Event
- Inherits:
-
Object
- Object
- Gridhook::Event
- Defined in:
- lib/gridhook/event.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
The original Hash of attributes received from SendGrid.
Class Method Summary collapse
-
.process(body, params = {}) ⇒ Object
Process a String or stream of JSON and execute our event processor.
Instance Method Summary collapse
-
#[](key) ⇒ Object
A helper for accessing the original values sent from SendGrid, ie.
-
#initialize(attributes) ⇒ Event
constructor
A new instance of Event.
-
#name ⇒ Object
(also: #event)
An alias for returning the type of this event, ie: sent, delivered, bounced, etc.
-
#timestamp ⇒ Object
Returns a new Time object from the event timestamp.
Constructor Details
#initialize(attributes) ⇒ Event
Returns a new instance of Event.
28 29 30 |
# File 'lib/gridhook/event.rb', line 28 def initialize(attributes) @attributes = attributes.with_indifferent_access end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
The original Hash of attributes received from SendGrid.
26 27 28 |
# File 'lib/gridhook/event.rb', line 26 def attributes @attributes end |
Class Method Details
.process(body, params = {}) ⇒ Object
Process a String or stream of JSON and execute our event processor.
body - A String or stream for MultiJson to parse
Returns nothing.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gridhook/event.rb', line 12 def self.process(body, params = {}) begin event = MultiJson.load(body) if event.is_a?(Array) process_events event else process_event event end rescue MultiJson::LoadError process_event params.except(:controller, :action) end end |
Instance Method Details
#[](key) ⇒ Object
A helper for accessing the original values sent from SendGrid, ie
Example:
event = Event.new(event: 'sent', email: '[email protected]')
event[:event] #=> 'sent'
event['email'] #=> '[email protected]' # indifferent access
52 53 54 |
# File 'lib/gridhook/event.rb', line 52 def [](key) attributes[key] end |
#name ⇒ Object Also known as: event
An alias for returning the type of this event, ie: sent, delivered, bounced, etc
34 35 36 |
# File 'lib/gridhook/event.rb', line 34 def name attributes[:event] end |
#timestamp ⇒ Object
Returns a new Time object from the event timestamp.
40 41 42 |
# File 'lib/gridhook/event.rb', line 40 def Time.at((attributes[:timestamp] || Time.now).to_i) end |