# frozen_string_literal: true module ConvenientService module Examples module Standard module RequestParams module Services class ExtractParamsFromPath include ConvenientService::Standard::Config ## # IMPORTANT: # - `CanHaveMethodSteps` is disabled in the Standard config since it causes race conditions in combination with `CanHaveStubbedResult`. # - It will be reenabled after the introduction of thread-safety specs. # - Do not use it in production yet. # middlewares :step, scope: :class do use ConvenientService::Plugins::Service::CanHaveMethodSteps::Middleware end 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