Sha256: 5484b2b0507c09735dc53700989137c1c096a7132a5baa5488b47f5096b9cf2c

Contents?: true

Size: 1.22 KB

Versions: 17

Compression:

Stored size: 1.22 KB

Contents

module Shipit
  class Delivery < ActiveRecord::Base
    STATUSES = %w(pending scheduled sent).freeze
    enum status: STATUSES.zip(STATUSES).to_h

    belongs_to :hook

    validates :url, presence: true, url: {no_local: true, allow_blank: true}
    validates :content_type, presence: true

    serialize :response_headers, JSON

    after_commit :purge_old_deliveries, on: :create

    def schedule!
      DeliverHookJob.perform_later(self)
      scheduled!
    end

    def send!
      update!(response: http.post(url, payload), status: 'sent', delivered_at: Time.now)
    end

    def response=(response)
      self.response_code = response.status
      self.response_headers = response.headers
      self.response_body = response.body
    end

    private

    def purge_old_deliveries
      PurgeOldDeliveriesJob.perform_later(hook)
    end

    def http
      Faraday::Connection.new do |connection|
        connection.headers = headers
        connection.adapter Faraday.default_adapter
      end
    end

    def headers
      {
        'User-Agent' => 'Shipit Webhook',
        'Content-Type' => content_type,
        'X-Shipit-Event' => event,
        'X-Shipit-Delivery' => id.to_s,
        'Accept' => '*/*',
      }
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shipit-engine-0.31.0 app/models/shipit/delivery.rb
shipit-engine-0.30.0 app/models/shipit/delivery.rb
shipit-engine-0.29.0 app/models/shipit/delivery.rb
shipit-engine-0.28.1 app/models/shipit/delivery.rb
shipit-engine-0.28.0 app/models/shipit/delivery.rb
shipit-engine-0.27.1 app/models/shipit/delivery.rb
shipit-engine-0.27.0 app/models/shipit/delivery.rb
shipit-engine-0.26.0 app/models/shipit/delivery.rb
shipit-engine-0.25.1 app/models/shipit/delivery.rb
shipit-engine-0.25.0 app/models/shipit/delivery.rb
shipit-engine-0.24.0 app/models/shipit/delivery.rb
shipit-engine-0.23.1 app/models/shipit/delivery.rb
shipit-engine-0.23.0 app/models/shipit/delivery.rb
shipit-engine-0.22.0 app/models/shipit/delivery.rb
shipit-engine-0.21.0 app/models/shipit/delivery.rb
shipit-engine-0.20.1 app/models/shipit/delivery.rb
shipit-engine-0.20.0 app/models/shipit/delivery.rb