convenient_service 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ROADMAP.md +14 -3
  6. data/Taskfile.yml +30 -0
  7. data/convenient_service.gemspec +3 -1
  8. data/lib/convenient_service/aliases.rb +1 -0
  9. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_active_model_attribute_assignment/concern.rb +0 -2
  10. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_dry_initializer/concern.rb +0 -2
  11. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/container/commands/resolve_methods_middlewares_callers.rb +1 -24
  12. data/lib/convenient_service/dependencies.rb +7 -0
  13. data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +3 -1
  14. data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +3 -1
  15. data/lib/convenient_service/examples/standard/request_params/constants.rb +15 -0
  16. data/lib/convenient_service/examples/standard/request_params/entities/description.rb +40 -0
  17. data/lib/convenient_service/examples/standard/request_params/entities/format.rb +40 -0
  18. data/lib/convenient_service/examples/standard/request_params/entities/id.rb +47 -0
  19. data/lib/convenient_service/examples/standard/request_params/entities/logger.rb +21 -0
  20. data/lib/convenient_service/examples/standard/request_params/entities/request.rb +23 -0
  21. data/lib/convenient_service/examples/standard/request_params/entities/source.rb +40 -0
  22. data/lib/convenient_service/examples/standard/request_params/entities/tag.rb +40 -0
  23. data/lib/convenient_service/examples/standard/request_params/entities/title.rb +40 -0
  24. data/lib/convenient_service/examples/standard/request_params/entities.rb +11 -0
  25. data/lib/convenient_service/examples/standard/request_params/services/apply_default_param_values.rb +26 -0
  26. data/lib/convenient_service/examples/standard/request_params/services/cast_params.rb +38 -0
  27. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb +70 -0
  28. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb +62 -0
  29. data/lib/convenient_service/examples/standard/request_params/services/filter_out_unpermitted_params.rb +26 -0
  30. data/lib/convenient_service/examples/standard/request_params/services/log_request_params.rb +54 -0
  31. data/lib/convenient_service/examples/standard/request_params/services/merge_params.rb +26 -0
  32. data/lib/convenient_service/examples/standard/request_params/services/prepare.rb +65 -0
  33. data/lib/convenient_service/examples/standard/request_params/services/validate_casted_params.rb +94 -0
  34. data/lib/convenient_service/examples/standard/request_params/services/validate_uncasted_params.rb +72 -0
  35. data/lib/convenient_service/examples/standard/request_params/services.rb +13 -0
  36. data/lib/convenient_service/examples/standard/request_params/utils/array/wrap.rb +46 -0
  37. data/lib/convenient_service/examples/standard/request_params/utils/array.rb +21 -0
  38. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_body.rb +42 -0
  39. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_path.rb +40 -0
  40. data/lib/convenient_service/examples/standard/request_params/utils/http/request.rb +28 -0
  41. data/lib/convenient_service/examples/standard/request_params/utils/http.rb +3 -0
  42. data/lib/convenient_service/examples/standard/request_params/utils/integer/safe_parse.rb +31 -0
  43. data/lib/convenient_service/examples/standard/request_params/utils/integer.rb +25 -0
  44. data/lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb +40 -0
  45. data/lib/convenient_service/examples/standard/request_params/utils/json.rb +21 -0
  46. data/lib/convenient_service/examples/standard/request_params/utils/object/blank.rb +34 -0
  47. data/lib/convenient_service/examples/standard/request_params/utils/object/present.rb +31 -0
  48. data/lib/convenient_service/examples/standard/request_params/utils/object.rb +26 -0
  49. data/lib/convenient_service/examples/standard/request_params/utils.rb +7 -0
  50. data/lib/convenient_service/examples/standard/request_params.rb +48 -0
  51. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands/generate_printable_method.rb +50 -0
  52. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands.rb +3 -0
  53. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb +78 -0
  54. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/generate_printable_arguments.rb +100 -0
  55. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands.rb +4 -0
  56. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments.rb +95 -0
  57. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/base.rb +87 -0
  58. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/return_its_value.rb +129 -0
  59. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_any_arguments.rb +37 -0
  60. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_concrete_arguments.rb +37 -0
  61. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/without_arguments.rb +37 -0
  62. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/base.rb +41 -0
  63. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/with_calling_original.rb +30 -0
  64. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/without_calling_original.rb +30 -0
  65. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings.rb +12 -0
  66. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection/errors.rb +57 -0
  67. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection.rb +171 -0
  68. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/delegation.rb +79 -0
  69. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities.rb +5 -0
  70. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher.rb +276 -0
  71. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities.rb +3 -0
  72. data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +58 -234
  73. data/lib/convenient_service/rspec/matchers/custom/singleton_prepend_module.rb +79 -0
  74. data/lib/convenient_service/rspec/matchers/custom.rb +1 -0
  75. data/lib/convenient_service/rspec/matchers/singleton_prepend_module.rb +13 -0
  76. data/lib/convenient_service/rspec/matchers.rb +2 -0
  77. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +25 -0
  78. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method.rb +3 -0
  79. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_caller.rb +8 -1
  80. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_direction.rb +3 -0
  81. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_key.rb +8 -1
  82. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_name.rb +9 -5
  83. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/concern/instance_methods.rb +4 -0
  84. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/base.rb +4 -0
  85. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +74 -0
  86. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands.rb +3 -0
  87. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment.rb +50 -0
  88. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers.rb +1 -0
  89. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values/reassignment.rb +43 -0
  90. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values.rb +3 -0
  91. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities.rb +1 -0
  92. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/errors.rb +22 -0
  93. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +13 -2
  94. data/lib/convenient_service/support/arguments/null_arguments.rb +28 -0
  95. data/lib/convenient_service/support/arguments.rb +87 -0
  96. data/lib/convenient_service/support/dependency_container/commands/assert_valid_scope.rb +32 -0
  97. data/lib/convenient_service/support/dependency_container/commands/import_method.rb +13 -23
  98. data/lib/convenient_service/support/dependency_container/commands.rb +1 -0
  99. data/lib/convenient_service/support/dependency_container/constants.rb +4 -2
  100. data/lib/convenient_service/support/dependency_container/entities/method.rb +2 -9
  101. data/lib/convenient_service/support/dependency_container/entities/namespace_collection.rb +0 -9
  102. data/lib/convenient_service/support/dependency_container/errors.rb +25 -0
  103. data/lib/convenient_service/support/dependency_container/export.rb +8 -0
  104. data/lib/convenient_service/support/dependency_container/import.rb +3 -1
  105. data/lib/convenient_service/support/version/null_version.rb +7 -0
  106. data/lib/convenient_service/support/version.rb +11 -1
  107. data/lib/convenient_service/support.rb +1 -0
  108. data/lib/convenient_service/utils/object/instance_variable_delete.rb +41 -0
  109. data/lib/convenient_service/utils/object/instance_variable_fetch.rb +4 -0
  110. data/lib/convenient_service/utils/object.rb +9 -0
  111. data/lib/convenient_service/utils/proc/display.rb +43 -0
  112. data/lib/convenient_service/utils/proc.rb +5 -0
  113. data/lib/convenient_service/version.rb +1 -1
  114. metadata +101 -4
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class ExtractParamsFromBody
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :request
12
+
13
+ step :parse_body, in: :request, out: :body
14
+ step :parse_json, in: :body, out: :json
15
+ step :extract_params, in: :json, out: :params
16
+ step :symbolize_keys, in: :params, out: reassign(:params)
17
+
18
+ def initialize(request:)
19
+ @request = request
20
+ end
21
+
22
+ private
23
+
24
+ def parse_body
25
+ body = Utils::HTTP::Request.parse_body(request.to_s)
26
+
27
+ return success(body: body) if body
28
+
29
+ error(
30
+ <<~MESSAGE
31
+ Failed to resolve body since request is NOT HTTP parsable.
32
+
33
+ Request:
34
+ ---
35
+ #{request}
36
+ ---
37
+ MESSAGE
38
+ )
39
+ end
40
+
41
+ def parse_json
42
+ json = Utils::JSON.safe_parse(body)
43
+
44
+ return success(json: json) if json
45
+
46
+ error(
47
+ <<~MESSAGE
48
+ Request body contains invalid json.
49
+
50
+ Request:
51
+ ---
52
+ #{request}
53
+ ---
54
+ MESSAGE
55
+ )
56
+ end
57
+
58
+ def extract_params
59
+ success(params: json)
60
+ end
61
+
62
+ def symbolize_keys
63
+ success(params: params.transform_keys(&:to_sym))
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class ExtractParamsFromPath
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :request, :pattern
12
+
13
+ step :parse_path, in: :request, out: :path
14
+ step :match_pattern, in: [:path, :pattern], out: :match_data
15
+ step :extract_params, in: :match_data, out: :params
16
+
17
+ def initialize(request:, pattern:)
18
+ @request = request
19
+ @pattern = pattern
20
+ end
21
+
22
+ private
23
+
24
+ def parse_path
25
+ path = Utils::HTTP::Request.parse_path(request.to_s)
26
+
27
+ return success(path: path) if path
28
+
29
+ error(
30
+ <<~MESSAGE
31
+ Failed to resolve path since request is NOT HTTP parsable.
32
+
33
+ Request:
34
+ ---
35
+ #{request}
36
+ ---
37
+ MESSAGE
38
+ )
39
+ end
40
+
41
+ def match_pattern
42
+ match_data = path.match(pattern)
43
+
44
+ return success(match_data: match_data) if match_data
45
+
46
+ error("Path `#{path}` does NOT match pattern `#{pattern}`.")
47
+ end
48
+
49
+ def extract_params
50
+ params = {
51
+ id: match_data[:id],
52
+ format: match_data[:format]
53
+ }
54
+
55
+ success(params: params)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class FilterOutUnpermittedParams
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :params, :permitted_keys
12
+
13
+ def initialize(params:, permitted_keys:)
14
+ @params = params
15
+ @permitted_keys = permitted_keys
16
+ end
17
+
18
+ def result
19
+ success(params: params.slice(*permitted_keys))
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class LogRequestParams
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :request, :params, :tag
12
+
13
+ def initialize(request:, params:, tag: Constants::Tags::EMPTY)
14
+ @request = request
15
+ @params = params
16
+ @tag = tag
17
+ end
18
+
19
+ def result
20
+ Entities::Logger.log(message)
21
+
22
+ success
23
+ end
24
+
25
+ private
26
+
27
+ def message
28
+ <<~MESSAGE
29
+ #{prefix}:
30
+ {
31
+ #{content}
32
+ }
33
+ MESSAGE
34
+ end
35
+
36
+ def prefix
37
+ text = "[Thread##{Thread.current.object_id}]"
38
+
39
+ text += " [Request##{request.object_id}]"
40
+ text += " [Params]"
41
+ text += " [#{tag}]" unless tag.empty?
42
+
43
+ text
44
+ end
45
+
46
+ def content
47
+ params.map { |key, value| " #{key}: #{value.inspect}" }.join(",\n")
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class MergeParams
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :params_from_path, :params_from_body
12
+
13
+ def initialize(params_from_path:, params_from_body:)
14
+ @params_from_path = params_from_path
15
+ @params_from_body = params_from_body
16
+ end
17
+
18
+ def result
19
+ success(params: params_from_path.merge(params_from_body))
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class Prepare
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :request
12
+
13
+ step Services::ExtractParamsFromPath, \
14
+ in: [:request, {pattern: raw(/^\/rules\/(?<id>\d+)\.(?<format>\w+)$/)}],
15
+ out: {params: :params_from_path}
16
+
17
+ step Services::ExtractParamsFromBody, \
18
+ in: :request,
19
+ out: {params: :params_from_body}
20
+
21
+ step Services::MergeParams, \
22
+ in: [:params_from_path, :params_from_body],
23
+ out: :params
24
+
25
+ step Services::LogRequestParams, \
26
+ in: [:request, :params, tag: raw("Uncasted")]
27
+
28
+ step Services::FilterOutUnpermittedParams, \
29
+ in: [:params, {permitted_keys: raw([:id, :format, :title, :description, :tags, :sources])}],
30
+ out: reassign(:params)
31
+
32
+ step Services::ApplyDefaultParamValues, \
33
+ in: [:params, defaults: raw({format: "json", tags: [], sources: []})],
34
+ out: reassign(:params)
35
+
36
+ step Services::ValidateUncastedParams, \
37
+ in: :params
38
+
39
+ step Services::CastParams, \
40
+ in: :params,
41
+ out: [:original_params, {casted_params: reassign(:params)}]
42
+
43
+ step Services::LogRequestParams, \
44
+ in: [:request, :params, tag: raw("Casted")]
45
+
46
+ step Services::ValidateCastedParams, \
47
+ in: [:original_params, {casted_params: :params}]
48
+
49
+ step :result, \
50
+ in: :params,
51
+ out: reassign(:params)
52
+
53
+ def initialize(request:)
54
+ @request = request
55
+ end
56
+
57
+ def result
58
+ success(params: params)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class ValidateCastedParams
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader \
12
+ :original_id,
13
+ :original_format,
14
+ :original_title,
15
+ :original_description,
16
+ :original_tags,
17
+ :original_sources
18
+
19
+ attr_reader \
20
+ :casted_id,
21
+ :casted_format,
22
+ :casted_title,
23
+ :casted_description,
24
+ :casted_tags,
25
+ :casted_sources
26
+
27
+ step :validate_id, in: [:original_id, :casted_id]
28
+ step :validate_format, in: [:original_format, :casted_format]
29
+ step :validate_title, in: [:original_title, :casted_title]
30
+ step :validate_description, in: [:original_description, :casted_description]
31
+ step :validate_tags, in: [:original_tags, :casted_tags]
32
+ step :validate_sources, in: [:original_sources, :casted_sources]
33
+
34
+ def initialize(original_params:, casted_params:)
35
+ @original_id = original_params[:id]
36
+ @original_format = original_params[:format]
37
+ @original_title = original_params[:title]
38
+ @original_description = original_params[:description]
39
+ @original_tags = original_params[:tags]
40
+ @original_sources = original_params[:sources]
41
+
42
+ @casted_id = casted_params[:id]
43
+ @casted_format = casted_params[:format]
44
+ @casted_title = casted_params[:title]
45
+ @casted_description = casted_params[:description]
46
+ @casted_tags = casted_params[:tags]
47
+ @casted_sources = casted_params[:sources]
48
+ end
49
+
50
+ private
51
+
52
+ def validate_id
53
+ return error("Failed to cast `#{original_id.inspect}` into `ID`") unless casted_id
54
+
55
+ success
56
+ end
57
+
58
+ def validate_format
59
+ return error("Failed to cast `#{original_format.inspect}` into `Format`") unless casted_format
60
+
61
+ success
62
+ end
63
+
64
+ def validate_title
65
+ return error("Failed to cast `#{original_title.inspect}` into `Title`") unless casted_title
66
+
67
+ success
68
+ end
69
+
70
+ def validate_description
71
+ return error("Failed to cast `#{original_description.inspect}` into `Description`") unless casted_description
72
+
73
+ success
74
+ end
75
+
76
+ ##
77
+ # TODO:
78
+ #
79
+ def validate_tags
80
+ success
81
+ end
82
+
83
+ ##
84
+ # TODO:
85
+ #
86
+ def validate_sources
87
+ success
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Services
8
+ class ValidateUncastedParams
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :id, :format, :title, :description, :tags, :sources
12
+
13
+ step :validate_id, in: :id
14
+ step :validate_format, in: :format
15
+ step :validate_title, in: :title
16
+ step :validate_description, in: :description
17
+
18
+ def initialize(params:)
19
+ @id = params[:id]
20
+ @format = params[:format]
21
+ @title = params[:title]
22
+ @description = params[:description]
23
+ @tags = params[:tags]
24
+ @sources = params[:sources]
25
+ end
26
+
27
+ private
28
+
29
+ def validate_id
30
+ return error("ID is NOT present") if Utils::Object.present?(id)
31
+ return error("ID `#{id}` is NOT a valid integer") unless Utils::Integer.safe_parse(id)
32
+
33
+ success
34
+ end
35
+
36
+ def validate_format
37
+ return error("Format `#{format}` is NOT supported, only JSON is allowed") if format != "json"
38
+
39
+ success
40
+ end
41
+
42
+ def validate_title
43
+ return error("Title is NOT present") if Utils::Object.present?(title)
44
+
45
+ success
46
+ end
47
+
48
+ def validate_description
49
+ return error("Description is NOT present") if Utils::Object.present?(description)
50
+
51
+ success
52
+ end
53
+
54
+ ##
55
+ # TODO:
56
+ #
57
+ def validate_tags
58
+ success
59
+ end
60
+
61
+ ##
62
+ # TODO:
63
+ #
64
+ def validate_sources
65
+ success
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "services/apply_default_param_values"
4
+ require_relative "services/cast_params"
5
+ require_relative "services/extract_params_from_body"
6
+ require_relative "services/extract_params_from_path"
7
+ require_relative "services/filter_out_unpermitted_params"
8
+ require_relative "services/log_request_params"
9
+ require_relative "services/merge_params"
10
+ require_relative "services/validate_casted_params"
11
+ require_relative "services/validate_uncasted_params"
12
+
13
+ require_relative "services/prepare"
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Utils
8
+ module Array
9
+ class Wrap < Support::Command
10
+ ##
11
+ # @!attribute [r] pad
12
+ # @return [Object] Can be any type.
13
+ #
14
+ attr_reader :object
15
+
16
+ ##
17
+ # @param object [Object] Can be any type.
18
+ # @return [void]
19
+ #
20
+ def initialize(object)
21
+ @object = object
22
+ end
23
+
24
+ ##
25
+ # @return [Array]
26
+ #
27
+ # @internal
28
+ # Copied without any modifications from:
29
+ # https://api.rubyonrails.org/classes/Array.html#method-c-wrap
30
+ #
31
+ def call
32
+ if object.nil?
33
+ []
34
+ elsif object.respond_to?(:to_ary)
35
+ object.to_ary || [object]
36
+ else
37
+ [object]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "array/wrap"
4
+
5
+ module ConvenientService
6
+ module Examples
7
+ module Standard
8
+ module RequestParams
9
+ module Utils
10
+ module Array
11
+ class << self
12
+ def wrap(object)
13
+ Wrap.call(object)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Utils
8
+ module HTTP
9
+ module Request
10
+ ##
11
+ # TODO: Specs.
12
+ #
13
+ class ParseBody < Support::Command
14
+ attr_reader :http_string
15
+
16
+ def initialize(http_string:)
17
+ @http_string = http_string
18
+ end
19
+
20
+ ##
21
+ # IMPORTANT: Make sure length is set in `http_string`.
22
+ #
23
+ # - https://stackoverflow.com/a/17599778/12201472
24
+ # - https://github.com/ruby/webrick/blob/v1.7.0/lib/webrick/httprequest.rb
25
+ #
26
+ def call
27
+ webrick_request = ::WEBrick::HTTPRequest.new(::WEBrick::Config::HTTP)
28
+
29
+ webrick_request.parse(::StringIO.new(http_string))
30
+
31
+ webrick_request.body
32
+ rescue ::WEBrick::HTTPStatus::BadRequest
33
+ nil
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module RequestParams
7
+ module Utils
8
+ module HTTP
9
+ module Request
10
+ ##
11
+ # TODO: Specs.
12
+ #
13
+ class ParsePath < Support::Command
14
+ attr_reader :http_string
15
+
16
+ def initialize(http_string:)
17
+ @http_string = http_string
18
+ end
19
+
20
+ ##
21
+ # - https://stackoverflow.com/a/17599778/12201472
22
+ # - https://github.com/ruby/webrick/blob/v1.7.0/lib/webrick/httprequest.rb
23
+ #
24
+ def call
25
+ webrick_request = ::WEBrick::HTTPRequest.new(::WEBrick::Config::HTTP)
26
+
27
+ webrick_request.parse(::StringIO.new(http_string))
28
+
29
+ webrick_request.path
30
+ rescue ::WEBrick::HTTPStatus::BadRequest
31
+ nil
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "request/parse_body"
4
+ require_relative "request/parse_path"
5
+
6
+ module ConvenientService
7
+ module Examples
8
+ module Standard
9
+ module RequestParams
10
+ module Utils
11
+ module HTTP
12
+ module Request
13
+ class << self
14
+ def parse_body(http_string)
15
+ ParseBody.call(http_string: http_string)
16
+ end
17
+
18
+ def parse_path(http_string)
19
+ ParsePath.call(http_string: http_string)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "http/request"