Sha256: cc070cd1ed3c0bc334925185d58c6f075e97c7b26330302ccbc7b1b878c37d30

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module ActiveHook
  module Client
    module Recieve

      REQUEST_HEADERS = {
        "Content-Type" => "application/json",
        "Accept"       => "application/json",
        "User-Agent"   => "ActiveHook/#{ActiveHook::VERSION}"
      }.freeze

      attr_accessor :hook_id, :hook_key
      attr_reader :payload

      def hook_valid?
        @hook_valid ||= validate_hook
      end

      def payload=(payload)
        @payload = JSON.parse(payload)
      rescue
        nil
      end

      def validated_payload
        raise StandardError, 'Webhook is invalid.' unless hook_valid?
        @payload
      end

      private

      def hook_uri
        @hook_uri ||= URI.parse(self.class::VALIDATION_URI)
      end

      def validate_hook
        http = Net::HTTP.new(hook_uri.host, hook_uri.port)
        response = http.post(hook_uri.path, hook_json, REQUEST_HEADERS)
        response.code.to_i == 200 ? true : false
      rescue
        false
      end

      def hook_json
        { id: @hook_id,
          key: @hook_key }.to_json
      end
    end
  end

  class Recieve
    include ActiveHook::Client::Recieve

    VALIDATION_URI = (ActiveHook.config.validation_uri).freeze
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activehook-0.1.4 lib/activehook/client/recieve.rb
activehook-0.1.3 lib/activehook/client/recieve.rb