Sha256: c1cc6e4b4cd0790f5f47c1ca9620c5c09af16758f679808902d16630e9ff6493

Contents?: true

Size: 843 Bytes

Versions: 1

Compression:

Stored size: 843 Bytes

Contents

require 'json'
require 'rack'

module RailsCloudTasks
  module Rack
    class Tasks
      class << self
        def call(env)
          request = ::Rack::Request.new(env)
          job = extract_job(request)

          ActiveJob::Base.execute(job)

          response(200, {})
        rescue Rack::InvalidPayloadError => e
          response(400, { error: e.cause.message })
        rescue StandardError => e
          response(500, { error: e.message })
        end

        private

        def extract_job(request)
          body = request.body.read
          JSON.parse(body).fetch('job')
        rescue JSON::ParserError, KeyError
          raise Rack::InvalidPayloadError
        end

        def response(status, body)
          [status, { 'Content-Type' => 'application/json' }, [body.to_json]]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-cloud-tasks-0.0.2 lib/rails_cloud_tasks/rack/tasks.rb