Class: Polar::Webhooks::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/polar/webhooks.rb

Overview

Webhook event wrapper class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Event

Returns a new instance of Event.



134
135
136
137
138
139
140
141
142
# File 'lib/polar/webhooks.rb', line 134

def initialize(payload)
  @raw_payload = payload.is_a?(String) ? payload : payload.to_json
  parsed = payload.is_a?(String) ? JSON.parse(payload) : payload

  @id = parsed['id']
  @type = parsed['type'] || parsed['event_type']
  @created_at = parsed['created_at'] ? Time.parse(parsed['created_at']) : nil
  @data = parsed['data'] || parsed
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



132
133
134
# File 'lib/polar/webhooks.rb', line 132

def created_at
  @created_at
end

#dataObject (readonly)

Returns the value of attribute data.



132
133
134
# File 'lib/polar/webhooks.rb', line 132

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



132
133
134
# File 'lib/polar/webhooks.rb', line 132

def id
  @id
end

#raw_payloadObject (readonly)

Returns the value of attribute raw_payload.



132
133
134
# File 'lib/polar/webhooks.rb', line 132

def raw_payload
  @raw_payload
end

#typeObject (readonly)

Returns the value of attribute type.



132
133
134
# File 'lib/polar/webhooks.rb', line 132

def type
  @type
end

Instance Method Details

#benefit_event?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/polar/webhooks.rb', line 160

def benefit_event?
  type&.start_with?('benefit.')
end

#customer_event?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/polar/webhooks.rb', line 156

def customer_event?
  type&.start_with?('customer.')
end

#order_event?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/polar/webhooks.rb', line 148

def order_event?
  type&.start_with?('order.')
end

#payment_event?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/polar/webhooks.rb', line 152

def payment_event?
  type&.start_with?('payment.')
end

#subscription_event?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/polar/webhooks.rb', line 144

def subscription_event?
  type&.start_with?('subscription.')
end

#to_hObject



164
165
166
167
168
169
170
171
# File 'lib/polar/webhooks.rb', line 164

def to_h
  {
    id: id,
    type: type,
    created_at: created_at,
    data: data
  }
end