Class: Gridhook::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/gridhook/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject (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

#nameObject 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

#timestampObject

Returns a new Time object from the event timestamp.



40
41
42
# File 'lib/gridhook/event.rb', line 40

def timestamp
  Time.at((attributes[:timestamp] || Time.now).to_i)
end