Sha256: 41e2e8af6312aa476748559a055ed798bbf1c7356f2ca97e4f33b61cfd325c68

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      class 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

5 entries across 5 versions & 1 rubygems

Version Path
convenient_service-0.17.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.16.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.15.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.14.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb
convenient_service-0.13.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb