class Gridhook::Event

Attributes

attributes[R]

The original Hash of attributes received from SendGrid.

Public Class Methods

new(attributes) click to toggle source
# File lib/gridhook/event.rb, line 28
def initialize(attributes)
  @attributes = attributes.with_indifferent_access
end
process(body, params = {}) click to toggle source

Process a String or stream of JSON and execute our event processor.

body - A String or stream for MultiJson to parse

Returns nothing.

# 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

Private Class Methods

process_event(event) click to toggle source
# File lib/gridhook/event.rb, line 63
def process_event(event)
  processor = Gridhook.config.event_processor
  if processor.respond_to?(:call)
    processor.call Event.new(event)
  else
    raise InvalidEventProcessor, "Your event processor is nil or "\
      "does not response to a `call' method."
  end
end
process_events(events) click to toggle source
# File lib/gridhook/event.rb, line 59
def process_events(events)
  events.each { |e| process_event e }
end

Public Instance Methods

[](key) click to toggle source

A helper for accessing the original values sent from SendGrid, ie

Example:

event = Event.new(event: 'sent', email: 'lee@example.com')
event[:event]  #=> 'sent'
event['email'] #=> 'lee@example.com' # indifferent access
# File lib/gridhook/event.rb, line 52
def [](key)
  attributes[key]
end
event()
Alias for: name
name() click to toggle source

An alias for returning the type of this event, ie: sent, delivered, bounced, etc

# File lib/gridhook/event.rb, line 34
def name
  attributes[:event]
end
Also aliased as: event
timestamp() click to toggle source

Returns a new Time object from the event timestamp.

# File lib/gridhook/event.rb, line 40
def timestamp
  Time.at((attributes[:timestamp] || Time.now).to_i)
end