class LessonlyApi::Webhook

Attributes

env[R]
raw_webhook_data[R]

Public Class Methods

call(env) click to toggle source
# File lib/lessonly_api/webhook.rb, line 4
def call(env)
  new(env).call
end
new(env) click to toggle source
# File lib/lessonly_api/webhook.rb, line 11
def initialize(env)
  @env = env
end

Public Instance Methods

call() click to toggle source
# File lib/lessonly_api/webhook.rb, line 15
def call
  return [405, {"Content-Type" => "text/plain"}, ['Method Not Allowed']] unless webhook_handler
  return [401, {"Content-Type" => "text/plain"}, ['Unauthorized']] unless authorized?

  parse_webhook_data

  return [406, {"Content-Type" => "text/plain"}, ['Not Acceptable']] if raw_webhook_data.nil?

  webhook_handler.call(webhook_data)

  [200, {"Content-Type" => "text/plain"}, ['OK']]
end

Private Instance Methods

authorized?() click to toggle source
# File lib/lessonly_api/webhook.rb, line 32
def authorized?
  env['HTTP_AUTHORIZATION'] == LessonlyApi.configuration.webhook_authentication
end
parse_webhook_data() click to toggle source
# File lib/lessonly_api/webhook.rb, line 36
def parse_webhook_data
  @raw_webhook_data = JSON.parse(env['rack.input'].read)
rescue JSON::ParserError
  nil
end
webhook_data() click to toggle source
# File lib/lessonly_api/webhook.rb, line 42
def webhook_data
  LessonlyApi::ResourceType::Webhook.new(raw_webhook_data)
end
webhook_handler() click to toggle source
# File lib/lessonly_api/webhook.rb, line 46
def webhook_handler
  LessonlyApi.configuration.webhook_handler
end