Sha256: 316381e087569a2139384b3c42b6371bb921896be77343840231078c236d59f5

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      module V1
        class RequestParams
          module Services
            class ExtractParamsFromBody
              include ConvenientService::Standard::V1::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
end

Version data entries

4 entries across 4 versions & 1 rubygems

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