Sha256: 588efc9a818bdcf41db3e6a3bc440f007b32b5cf71718148a78ab70d4e1f0a03

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Webhookr
  class Service
    attr_reader :service_name

    def initialize(service_name, options = {})
      @service_name = (service_name || "").downcase
      @raw_payload = options[:payload]
      available?
      validate_security_token(options[:security_token]) if configured_security_token
    end

    def process!
      Array.wrap(service_adapter.send(:process, @raw_payload)).each do |payload|
        callback(callback_class, payload)
      end
    end

    private

    def callback(object, payload)
      method = method_for(payload)
      object.send(method, payload) if object.respond_to?(method)
    end

    def method_for(payload)
      "on_" + payload.event_type
    end

    def callback_class
      callback = Webhookr.config[service_name].try(:callback)
      raise "No callback is configured for the service '#{service_name}'." if callback.nil?
      @call_back_class || callback.new
    end

    def configured_security_token
      Webhookr.config[service_name].try(:security_token)
    end

    def validate_security_token(token)
      raise Webhookr::InvalidSecurityTokenError if token.nil? || !SecureCompare.compare(token, configured_security_token)
    end

    def service_adapter
      raise NameError.new(%{Bad service name "#{service_name}"}) unless Webhookr.adapters[service_name]
      @service_adapter ||= Webhookr.adapters[service_name]
    end

    alias_method :available?, :service_adapter

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webhookr-0.2.0 lib/webhookr/service.rb