Sha256: 660e00886afa48fee97fcc9d965eea41d4816a5217c0d084e79d15ec32becaf0

Contents?: true

Size: 1.08 KB

Versions: 23

Compression:

Stored size: 1.08 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

    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 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

23 entries across 23 versions & 1 rubygems

Version Path
shipit-engine-0.6.2 app/models/shipit/delivery.rb
shipit-engine-0.6.1 app/models/shipit/delivery.rb
shipit-engine-0.6.0 app/models/shipit/delivery.rb