Sha256: c45a260357b497092962e5ed332d14f47da98217b3c79c1a198c9d29577daf2a

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      module V1
        class RequestParams
          module Services
            class ExtractParamsFromPath
              include ConvenientService::Standard::V1::Config

              attr_reader :request, :pattern

              step :parse_path, in: :request, out: :path
              step :match_pattern, in: [:path, :pattern], out: :match_data
              step :extract_params, in: :match_data, out: :params

              def initialize(request:, pattern:)
                @request = request
                @pattern = pattern
              end

              private

              def parse_path
                path = Utils::HTTP::Request.parse_path(request.to_s)

                return success(path: path) if path

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

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

              def match_pattern
                match_data = path.match(pattern)

                return success(match_data: match_data) if match_data

                error("Path `#{path}` does NOT match pattern `#{pattern}`.")
              end

              def extract_params
                params = {
                  id: match_data[:id],
                  format: match_data[:format]
                }

                success(params: params)
              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_path.rb
convenient_service-0.16.0 lib/convenient_service/examples/standard/v1/request_params/services/extract_params_from_path.rb
convenient_service-0.15.0 lib/convenient_service/examples/standard/v1/request_params/services/extract_params_from_path.rb
convenient_service-0.14.0 lib/convenient_service/examples/standard/v1/request_params/services/extract_params_from_path.rb