Class: Hominid::Webhook
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Expects a hash of POST data generated from Mailchimp:.
Instance Method Summary collapse
- #email ⇒ Object
- #email_type ⇒ Object
- #event ⇒ Object
- #fired_at ⇒ Object
- #first_name ⇒ Object
- #id ⇒ Object
-
#initialize(*args) ⇒ Webhook
constructor
A new instance of Webhook.
- #interests ⇒ Object
- #ip_opt ⇒ Object
- #ip_signup ⇒ Object
- #last_name ⇒ Object
- #list_id ⇒ Object
- #new_email ⇒ Object
- #old_email ⇒ Object
- #reason ⇒ Object
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.
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
#request ⇒ Object (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
#email ⇒ Object
56 57 58 |
# File 'lib/hominid/webhook.rb', line 56 def email self.request.data.email if self.request.data.email end |
#email_type ⇒ Object
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 |
#event ⇒ Object
64 65 66 |
# File 'lib/hominid/webhook.rb', line 64 def event self.request.event if self.request.event end |
#fired_at ⇒ Object
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_name ⇒ Object
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 |
#id ⇒ Object
80 81 82 |
# File 'lib/hominid/webhook.rb', line 80 def id self.request.data.id if self.request.data.id end |
#interests ⇒ Object
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_opt ⇒ Object
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_signup ⇒ Object
92 93 94 |
# File 'lib/hominid/webhook.rb', line 92 def ip_signup self.request.data.ip_signup if self.request.data.ip_signup end |
#last_name ⇒ Object
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_id ⇒ Object
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_email ⇒ Object
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_email ⇒ Object
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 |
#reason ⇒ Object
108 109 110 |
# File 'lib/hominid/webhook.rb', line 108 def reason self.request.data.reason if self.request.data.reason end |