Sha256: d0e97d2f0321e14f40213ca2c44a53a8cc917c4710f6342008acf63e369757fc

Contents?: true

Size: 745 Bytes

Versions: 5

Compression:

Stored size: 745 Bytes

Contents

# frozen_string_literal: true

module Metatron
  # Base class for API services
  class Controller
    class << self
      def call(env)
        new(env).call
      end
    end

    attr_accessor :params

    def initialize(env)
      @env = env
      @request = Rack::Request.new(env)
    end

    def call
      begin
        if request&.content_type&.include?("json")
          body = request.body.read
          request.body.rewind if request.body.respond_to?(:rewind)

          self.params = JSON.parse(body) unless body.empty?
        end
      rescue JSON::ParserError => e
        return [400, {}, [{ error: "Request must be JSON: #{e.message}" }.to_json]]
      end

      _call
    end

    private

    attr_reader :request
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
metatron-0.8.2 lib/metatron/controller.rb
metatron-0.8.0 lib/metatron/controller.rb
metatron-0.7.0 lib/metatron/controller.rb
metatron-0.6.1 lib/metatron/controller.rb
metatron-0.6.0 lib/metatron/controller.rb