Sha256: 126b38ee7c56df3c92af672705f05f3fb3ee63fc51ebfe83847c9f31d282819a

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module Fakeit
  module App
    module Routes
      class ConfigRoute
        attr_reader :options

        def initialize(options)
          @options = options
        end

        def call(request)
          case [request.request_method, request.media_type]
          in ['GET', _]
            Fakeit::App::Helpers::ResponseBuilder.ok(@options.to_hash)
          in ['PUT', 'application/json']
            update(request)
          in ['PUT', _]
            Fakeit::App::Helpers::ResponseBuilder.unsupported_media_type
          else
            Fakeit::App::Helpers::ResponseBuilder.method_not_allowed
          end
        end

        private

        def update(request)
          body = Fakeit::App::Helpers::BodyParser.parse(request)[:data]
          @options = Fakeit::App::Options.new(**body.transform_keys(&:to_sym))

          Fakeit::App::Helpers::ResponseBuilder.ok(@options.to_hash)
        rescue ArgumentError => e
          Logger.warn(Rainbow(e.message).red)
          Fakeit::App::Helpers::ResponseBuilder.error(422, e)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fakeit-0.7.1 lib/fakeit/app/routes/config_route.rb
fakeit-0.7.0 lib/fakeit/app/routes/config_route.rb