Sha256: 9bfaec2fce5c7b5fbf2f352f0c06cc60e0e3f607f15150f195f68fe94cd6f98b

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      module RequestParams
        module Services
          class ExtractParamsFromBody
            include ConvenientService::Standard::Config

            attr_reader :request

            step :parse_body, in: :request, out: :body
            step :parse_json, in: :body, out: :json
            step :extract_params, in: :json, out: :params
            step :symbolize_keys, in: :params, out: reassign(:params)

            def initialize(request:)
              @request = request
            end

            private

            def parse_body
              body = Utils::HTTP::Request.parse_body(request.to_s)

              return success(body: body) if body

              error(
                <<~MESSAGE
                  Failed to resolve body since request is NOT HTTP parsable.

                  Request:
                  ---
                  #{request}
                  ---
                MESSAGE
              )
            end

            def parse_json
              json = Utils::JSON.safe_parse(body)

              return success(json: json) if json

              error(
                <<~MESSAGE
                  Request body contains invalid json.

                  Request:
                  ---
                  #{request}
                  ---
                MESSAGE
              )
            end

            def extract_params
              success(params: json)
            end

            def symbolize_keys
              success(params: params.transform_keys(&:to_sym))
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.10.1 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.10.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.9.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.8.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.7.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.6.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb