Sha256: 98b091ea08dced6ba3f530e2ea723525241a1ff638968416c6523f4d19fbcd32

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

module WebhookSystem

  # This is the ActiveJob in charge of actually sending each event
  class Job < ActiveJob::Base

    def perform(subscription, event)
      payload = Encoder.encode(subscription.secret, event)
      client = HttpClient.new(subscription.url)
      client.post(payload)
    end

  end

  # Just a simple internal class to wrap around the http requests to the endpoints
  class HttpClient
    def initialize(endpoint)
      @endpoint = endpoint
    end

    def post(payload)
      client.post do |req|
        req.headers['Content-Type'] = 'application/json; base64+aes256'
        req.body = payload.to_s
      end
    end

    private

    attr_reader :endpoint, :client

    def client
      @client ||= Faraday.new(url: endpoint) do |faraday|
        # faraday.request :url_encoded # form-encode POST params
        faraday.response :logger # log requests to STDOUT
        faraday.adapter Faraday.default_adapter
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webhook_system-0.0.1 lib/webhook_system/job.rb