Sha256: 44a22fc30e4490e5f491efab4cac45b0cc14945901acceb48214a5e291252018

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

module Fakeit
  module App
    module Routes
      class ConfigRoute
        attr_reader :options

        def initialize(options) = @options = options

        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

7 entries across 7 versions & 1 rubygems

Version Path
fakeit-0.11.0 lib/fakeit/app/routes/config_route.rb
fakeit-0.10.0 lib/fakeit/app/routes/config_route.rb
fakeit-0.9.2 lib/fakeit/app/routes/config_route.rb
fakeit-0.9.1 lib/fakeit/app/routes/config_route.rb
fakeit-0.9.0 lib/fakeit/app/routes/config_route.rb
fakeit-0.8.1 lib/fakeit/app/routes/config_route.rb
fakeit-0.8.0 lib/fakeit/app/routes/config_route.rb