Class: Hominid::Webhook

Inherits:
Base
  • Object
show all
Defined in:
lib/hominid/webhook.rb

Constant Summary

Constants inherited from Base

Base::MAILCHIMP_API_VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_api_key, #api_keys, #apply_defaults_to, #call, #clean_merge_tags, #expire_api_key

Constructor Details

#initialize(*args) ⇒ Webhook

Returns a new instance of Webhook.

Raises:

  • (HominidError)


48
49
50
51
52
53
# File 'lib/hominid/webhook.rb', line 48

def initialize(*args)
  post_data = args.last
  raise HominidError.new('Please provide the POST data from a Mailchimp webhook request.') unless post_data.is_a?(Hash)
  post_data.merge!({"event" => "#{post_data.delete('type')}"})
  @request = hash_to_object(post_data)
end

Instance Attribute Details

#requestObject (readonly)

Expects a hash of POST data generated from Mailchimp:

“type”: “unsubscribe”, “fired_at”: “2009-03-26 21:54:00”, “data”: “[email protected]

Simple Usage:

h = Hominid::Webhook.new(params)

Sample params from Mailchimp webhook: params => { “type” => “subscribe”,

"fired_at" => "2009-03-26 21:35:57",
"data" => { "id" => "8a25ff1d98",
            "list_id" => "8a25ff1d98",
            "email" => "[email protected]",
            "email_type" => "html",
            "merges" => {"EMAIL" => "[email protected]",
                        "FNAME" => "Brian",
                        "LNAME" => "Getting",
                        "INTERESTS" => "Group1,Group2"},
            "ip_opt" => "10.20.10.30",
            "ip_signup" => "10.20.10.30" }}

Returns an object with the following methods (NOTE: Not all methods are available for all event types. Refer to www.mailchimp.com/api/webhooks/ for information on what data will be available for each event):

h.event <= (String) The event that fired the request. Possible events are:

"subscribe", "unsubscribe", "profile", "upemail", "cleaned"

h.fired_at <= (Datetime) When the webhook request was fired. h.id <= (String) The ID of the webhook request. h.list_id <= (String) The ID of the list that generated the request. h.email <= (String) The email address of the subscriber that generated the request. h.email_type <= (String) The email type of the subscriber that generated the request. h.first_name <= (String) The first name of the subscriber (if available). h.last_name <= (String) The first name of the subscriber (if available). h.interests <= (Array) An array of the interest groups. h.ip_opt <= (String) The opt in IP address. h.ip_signup <= (String) The signup IP address.



46
47
48
# File 'lib/hominid/webhook.rb', line 46

def request
  @request
end

Instance Method Details

#emailObject



56
57
58
# File 'lib/hominid/webhook.rb', line 56

def email
  self.request.data.email if self.request.data.email
end

#email_typeObject



60
61
62
# File 'lib/hominid/webhook.rb', line 60

def email_type
  self.request.data.email_type if self.request.data.email_type
end

#eventObject



64
65
66
# File 'lib/hominid/webhook.rb', line 64

def event
  self.request.event if self.request.event
end

#fired_atObject



68
69
70
# File 'lib/hominid/webhook.rb', line 68

def fired_at
  self.request.fired_at.to_datetime if self.request.fired_at
end

#first_nameObject



72
73
74
# File 'lib/hominid/webhook.rb', line 72

def first_name
  self.request.data.merges.fname if self.request.data.merges.fname
end

#idObject



80
81
82
# File 'lib/hominid/webhook.rb', line 80

def id
  self.request.data.id if self.request.data.id
end

#interestsObject



84
85
86
# File 'lib/hominid/webhook.rb', line 84

def interests
  self.request.data.merges.interests.split(',') if self.request.data.merges.interests
end

#ip_optObject



88
89
90
# File 'lib/hominid/webhook.rb', line 88

def ip_opt
  self.request.data.ip_opt if self.request.data.ip_opt
end

#ip_signupObject



92
93
94
# File 'lib/hominid/webhook.rb', line 92

def 
  self.request.data. if self.request.data.
end

#last_nameObject



76
77
78
# File 'lib/hominid/webhook.rb', line 76

def last_name
  self.request.data.merges.lname if self.request.data.merges.lname
end

#list_idObject



96
97
98
# File 'lib/hominid/webhook.rb', line 96

def list_id
  self.request.data.list_id if self.request.data.list_id
end

#new_emailObject



100
101
102
# File 'lib/hominid/webhook.rb', line 100

def new_email
  self.request.data.new_email if self.request.data.new_email
end

#old_emailObject



104
105
106
# File 'lib/hominid/webhook.rb', line 104

def old_email
  self.request.data.old_email if self.request.data.old_email
end

#reasonObject



108
109
110
# File 'lib/hominid/webhook.rb', line 108

def reason
  self.request.data.reason if self.request.data.reason
end