Sha256: 25946180e69b8654e198adf0cbfc061f9a85c2172669e95d94fa80f2531dff8b

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

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

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_path.rb
convenient_service-0.16.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb
convenient_service-0.15.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb
convenient_service-0.14.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb
convenient_service-0.13.0 lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb