Class: Polar::Webhook
- Inherits:
-
Object
- Object
- Polar::Webhook
- Defined in:
- lib/polar/webhook.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #cast_object ⇒ Object
-
#initialize(request, secret: nil) ⇒ Webhook
constructor
A new instance of Webhook.
- #verify ⇒ Object
Constructor Details
#initialize(request, secret: nil) ⇒ Webhook
Returns a new instance of Webhook.
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/polar/webhook.rb', line 3 def initialize(request, secret: nil) unless (unencoded_secret = secret || Polar.config.webhook_secret) && unencoded_secret != "" raise ArgumentError, "No webhook secret provided, set Polar.config.webhook_secret" end @secret = Base64.encode64(unencoded_secret) @request = request @payload = JSON.parse(request.raw_post, symbolize_names: true) @type = @payload[:type] @object = cast_object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
15 16 17 |
# File 'lib/polar/webhook.rb', line 15 def object @object end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
15 16 17 |
# File 'lib/polar/webhook.rb', line 15 def payload @payload end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
15 16 17 |
# File 'lib/polar/webhook.rb', line 15 def request @request end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
15 16 17 |
# File 'lib/polar/webhook.rb', line 15 def secret @secret end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/polar/webhook.rb', line 15 def type @type end |
Class Method Details
.verify(request, secret: nil) ⇒ Object
45 46 47 |
# File 'lib/polar/webhook.rb', line 45 def self.verify(request, secret: nil) new(request, secret: secret).verify end |
Instance Method Details
#cast_object ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/polar/webhook.rb', line 22 def cast_object case type when /^checkout\./ Checkout::Custom.handle_one(payload[:data]) when /^order\./ Order.handle_one(payload[:data]) when /^subscription\./ Subscription.handle_one(payload[:data]) when /^refund\./ Refund.handle_one(payload[:data]) when /^product\./ Product.handle_one(payload[:data]) when /^pledge\./ Pledge.handle_one(payload[:data]) when /^organization\./ Organization.handle_one(payload[:data]) when /^benefit\./ Benefit.handle_one(payload[:data]) when /^benefit_grant\./ BenefitGrant.handle_one(payload[:data]) end end |
#verify ⇒ Object
17 18 19 20 |
# File 'lib/polar/webhook.rb', line 17 def verify StandardWebhooks::Webhook.new(@secret).verify(request.raw_post, request.headers) self end |