pakyow-core 0.11.3 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +0 -0
  3. data/LICENSE +4 -0
  4. data/{pakyow-core/README.md → README.md} +3 -4
  5. data/commands/pakyow +13 -0
  6. data/lib/pakyow/app/connection/behavior/session.rb +33 -0
  7. data/lib/pakyow/app/connection/behavior/values.rb +42 -0
  8. data/lib/pakyow/app/connection/behavior/verifier.rb +34 -0
  9. data/lib/pakyow/app/connection/session/abstract.rb +24 -0
  10. data/lib/pakyow/app/connection/session/cookie.rb +56 -0
  11. data/lib/pakyow/app/connection.rb +57 -0
  12. data/lib/pakyow/app.rb +219 -0
  13. data/lib/pakyow/behavior/aspects.rb +45 -0
  14. data/lib/pakyow/behavior/config.rb +72 -0
  15. data/lib/pakyow/behavior/endpoints.rb +41 -0
  16. data/lib/pakyow/behavior/frameworks.rb +45 -0
  17. data/lib/pakyow/behavior/helpers.rb +107 -0
  18. data/lib/pakyow/behavior/initializers.rb +19 -0
  19. data/lib/pakyow/behavior/isolating.rb +69 -0
  20. data/lib/pakyow/behavior/operations.rb +46 -0
  21. data/lib/pakyow/behavior/pipeline.rb +58 -0
  22. data/lib/pakyow/behavior/plugins.rb +148 -0
  23. data/lib/pakyow/behavior/rescuing.rb +51 -0
  24. data/lib/pakyow/behavior/restarting.rb +68 -0
  25. data/lib/pakyow/behavior/sessions.rb +34 -0
  26. data/lib/pakyow/behavior/verification.rb +47 -0
  27. data/lib/pakyow/cli.rb +199 -0
  28. data/lib/pakyow/connection/multipart_input.rb +24 -0
  29. data/lib/pakyow/connection/multipart_parser.rb +112 -0
  30. data/lib/pakyow/connection/params.rb +29 -0
  31. data/lib/pakyow/connection/query_parser.rb +174 -0
  32. data/lib/pakyow/connection/statuses.rb +119 -0
  33. data/lib/pakyow/connection.rb +568 -0
  34. data/lib/pakyow/endpoints.rb +94 -0
  35. data/lib/pakyow/environment/actions/dispatch.rb +37 -0
  36. data/lib/pakyow/environment/actions/input_parser.rb +21 -0
  37. data/lib/pakyow/environment/actions/logger.rb +33 -0
  38. data/lib/pakyow/environment/actions/normalizer.rb +73 -0
  39. data/lib/pakyow/environment/behavior/config.rb +166 -0
  40. data/lib/pakyow/environment/behavior/initializers.rb +21 -0
  41. data/lib/pakyow/environment/behavior/input_parsing.rb +54 -0
  42. data/lib/pakyow/environment/behavior/plugins.rb +23 -0
  43. data/lib/pakyow/environment/behavior/restarting.rb +54 -0
  44. data/lib/pakyow/environment/behavior/running.rb +129 -0
  45. data/lib/pakyow/environment/behavior/silencing.rb +23 -0
  46. data/lib/pakyow/environment/behavior/timezone.rb +21 -0
  47. data/lib/pakyow/environment/behavior/watching.rb +75 -0
  48. data/lib/pakyow/environment.rb +381 -0
  49. data/lib/pakyow/error.rb +273 -0
  50. data/lib/pakyow/errors.rb +97 -0
  51. data/lib/pakyow/framework.rb +39 -0
  52. data/lib/pakyow/generator.rb +138 -0
  53. data/lib/pakyow/generators/project/default/%dot%env.erb +1 -0
  54. data/lib/pakyow/generators/project/default/%dot%gitignore +11 -0
  55. data/lib/pakyow/generators/project/default/%dot%ruby-version.erb +1 -0
  56. data/lib/pakyow/generators/project/default/Gemfile.erb +28 -0
  57. data/lib/pakyow/generators/project/default/README.md +14 -0
  58. data/lib/pakyow/generators/project/default/config/application.rb.erb +17 -0
  59. data/lib/pakyow/generators/project/default/config/environment.rb +11 -0
  60. data/lib/pakyow/generators/project/default/config/initializers/application/keep +0 -0
  61. data/lib/pakyow/generators/project/default/config/initializers/environment/keep +0 -0
  62. data/lib/pakyow/generators/project/default/database/keep +0 -0
  63. data/lib/pakyow/generators/project/default/frontend/assets/images/keep +0 -0
  64. data/lib/pakyow/generators/project/default/frontend/assets/packs/keep +0 -0
  65. data/lib/pakyow/generators/project/default/frontend/assets/styles/keep +0 -0
  66. data/lib/pakyow/generators/project/default/frontend/includes/keep +0 -0
  67. data/lib/pakyow/generators/project/default/frontend/layouts/default.html.erb +19 -0
  68. data/lib/pakyow/generators/project/default/frontend/pages/keep +0 -0
  69. data/lib/pakyow/generators/project/default/public/favicon.ico +0 -0
  70. data/lib/pakyow/generators/project/default/public/robots.txt +1 -0
  71. data/lib/pakyow/generators/project.rb +29 -0
  72. data/lib/pakyow/helpers/app.rb +28 -0
  73. data/lib/pakyow/helpers/connection.rb +45 -0
  74. data/lib/pakyow/info.rb +26 -0
  75. data/lib/pakyow/integrations/bootsnap.rb +18 -0
  76. data/lib/pakyow/integrations/bundler/require.rb +7 -0
  77. data/lib/pakyow/integrations/bundler/setup.rb +7 -0
  78. data/lib/pakyow/integrations/dotenv.rb +9 -0
  79. data/lib/pakyow/loader.rb +31 -0
  80. data/lib/pakyow/logger/colorizer.rb +38 -0
  81. data/lib/pakyow/logger/destination.rb +17 -0
  82. data/lib/pakyow/logger/formatter.rb +22 -0
  83. data/lib/pakyow/logger/formatters/human.rb +101 -0
  84. data/lib/pakyow/logger/formatters/json.rb +99 -0
  85. data/lib/pakyow/logger/formatters/logfmt.rb +50 -0
  86. data/lib/pakyow/logger/multiplexed.rb +19 -0
  87. data/lib/pakyow/logger/thread_local.rb +29 -0
  88. data/lib/pakyow/logger/timekeeper.rb +64 -0
  89. data/lib/pakyow/logger.rb +125 -0
  90. data/lib/pakyow/operation.rb +71 -0
  91. data/lib/pakyow/plugin/helper_caller.rb +20 -0
  92. data/lib/pakyow/plugin/lookup.rb +33 -0
  93. data/lib/pakyow/plugin/state.rb +68 -0
  94. data/lib/pakyow/plugin.rb +367 -0
  95. data/lib/pakyow/process_manager.rb +53 -0
  96. data/lib/pakyow/processes/proxy.rb +67 -0
  97. data/lib/pakyow/processes/server.rb +32 -0
  98. data/lib/pakyow/rack/compatibility.rb +68 -0
  99. data/lib/pakyow/task.rb +315 -0
  100. data/lib/pakyow/tasks/boot.rake +21 -0
  101. data/lib/pakyow/tasks/create.rake +32 -0
  102. data/lib/pakyow/tasks/help.rake +17 -0
  103. data/lib/pakyow/tasks/info/endpoints.rake +72 -0
  104. data/lib/pakyow/tasks/info.rake +31 -0
  105. data/lib/pakyow/tasks/irb.rake +11 -0
  106. data/lib/pakyow/tasks/prelaunch.rake +37 -0
  107. data/lib/pakyow/types.rb +32 -0
  108. data/lib/pakyow/validations/acceptance.rb +37 -0
  109. data/lib/pakyow/validations/email.rb +28 -0
  110. data/lib/pakyow/validations/inline.rb +32 -0
  111. data/lib/pakyow/validations/length.rb +44 -0
  112. data/lib/pakyow/validations/presence.rb +41 -0
  113. data/lib/pakyow/validations.rb +8 -0
  114. data/lib/pakyow/validator.rb +81 -0
  115. data/lib/pakyow/verifier.rb +177 -0
  116. data/lib/pakyow.rb +5 -0
  117. metadata +267 -65
  118. data/pakyow-core/CHANGELOG.md +0 -128
  119. data/pakyow-core/LICENSE +0 -20
  120. data/pakyow-core/lib/pakyow/core/app.rb +0 -82
  121. data/pakyow-core/lib/pakyow/core/app_context.rb +0 -10
  122. data/pakyow-core/lib/pakyow/core/base.rb +0 -61
  123. data/pakyow-core/lib/pakyow/core/call_context.rb +0 -171
  124. data/pakyow-core/lib/pakyow/core/config/app.rb +0 -49
  125. data/pakyow-core/lib/pakyow/core/config/cookies.rb +0 -4
  126. data/pakyow-core/lib/pakyow/core/config/logger.rb +0 -34
  127. data/pakyow-core/lib/pakyow/core/config/reloader.rb +0 -10
  128. data/pakyow-core/lib/pakyow/core/config/server.rb +0 -10
  129. data/pakyow-core/lib/pakyow/core/config/session.rb +0 -41
  130. data/pakyow-core/lib/pakyow/core/config.rb +0 -95
  131. data/pakyow-core/lib/pakyow/core/errors.rb +0 -16
  132. data/pakyow-core/lib/pakyow/core/helpers/configuring.rb +0 -142
  133. data/pakyow-core/lib/pakyow/core/helpers/hooks.rb +0 -106
  134. data/pakyow-core/lib/pakyow/core/helpers/running.rb +0 -124
  135. data/pakyow-core/lib/pakyow/core/helpers.rb +0 -61
  136. data/pakyow-core/lib/pakyow/core/loader.rb +0 -32
  137. data/pakyow-core/lib/pakyow/core/middleware/logger.rb +0 -146
  138. data/pakyow-core/lib/pakyow/core/middleware/override.rb +0 -3
  139. data/pakyow-core/lib/pakyow/core/middleware/reloader.rb +0 -23
  140. data/pakyow-core/lib/pakyow/core/middleware/req_path_normalizer.rb +0 -49
  141. data/pakyow-core/lib/pakyow/core/middleware/session.rb +0 -5
  142. data/pakyow-core/lib/pakyow/core/middleware/static.rb +0 -76
  143. data/pakyow-core/lib/pakyow/core/multilog.rb +0 -19
  144. data/pakyow-core/lib/pakyow/core/request.rb +0 -119
  145. data/pakyow-core/lib/pakyow/core/response.rb +0 -135
  146. data/pakyow-core/lib/pakyow/core/route_eval.rb +0 -254
  147. data/pakyow-core/lib/pakyow/core/route_expansion_eval.rb +0 -115
  148. data/pakyow-core/lib/pakyow/core/route_lookup.rb +0 -41
  149. data/pakyow-core/lib/pakyow/core/route_merger.rb +0 -76
  150. data/pakyow-core/lib/pakyow/core/route_module.rb +0 -21
  151. data/pakyow-core/lib/pakyow/core/route_set.rb +0 -74
  152. data/pakyow-core/lib/pakyow/core/route_template_defaults.rb +0 -43
  153. data/pakyow-core/lib/pakyow/core/route_template_eval.rb +0 -72
  154. data/pakyow-core/lib/pakyow/core/router.rb +0 -181
  155. data/pakyow-core/lib/pakyow/core.rb +0 -8
  156. data/pakyow-core/lib/pakyow-core.rb +0 -1
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/validator"
4
+
5
+ module Pakyow
6
+ module Validations
7
+ # Validates that the value is of an expected length.
8
+ #
9
+ # @api public
10
+ module Length
11
+ def self.name
12
+ :length
13
+ end
14
+
15
+ def self.message(minimum: nil, maximum: nil, **)
16
+ if minimum && maximum
17
+ "must have between #{minimum} and #{maximum} characters"
18
+ elsif minimum
19
+ "must have more than #{minimum} #{wording(minimum)}"
20
+ elsif maximum
21
+ "must have less than #{maximum} #{wording(maximum)}"
22
+ end
23
+ end
24
+
25
+ def self.valid?(value, minimum: nil, maximum: nil, **)
26
+ unless minimum.nil?
27
+ return false if value.length < minimum
28
+ end
29
+
30
+ unless maximum.nil?
31
+ return false if value.length > maximum
32
+ end
33
+
34
+ true
35
+ end
36
+
37
+ def self.wording(n)
38
+ n == 1 ? "character" : "characters"
39
+ end
40
+ end
41
+
42
+ Validator.register_validation(Length)
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/validator"
4
+
5
+ module Pakyow
6
+ module Validations
7
+ # Validates that the value is present, or that a value is non-empty, non-nil, and not a string
8
+ # consisting of whitespace only characters. Example values that will not pass this validation:
9
+ #
10
+ # * +nil+
11
+ # * +""+
12
+ # * +" "+
13
+ # * +[]+
14
+ # * +{}+
15
+ #
16
+ # @api public
17
+ module Presence
18
+ WHITESPACE_ONLY = /^\s*$/
19
+
20
+ def self.name
21
+ :presence
22
+ end
23
+
24
+ def self.message(**)
25
+ "cannot be blank"
26
+ end
27
+
28
+ def self.valid?(value, **)
29
+ if value.is_a?(String)
30
+ !value.match?(WHITESPACE_ONLY)
31
+ elsif value.respond_to?(:empty?)
32
+ !value.empty?
33
+ else
34
+ !!value
35
+ end
36
+ end
37
+ end
38
+
39
+ Validator.register_validation(Presence)
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/validations/inline"
4
+
5
+ require "pakyow/validations/acceptance"
6
+ require "pakyow/validations/email"
7
+ require "pakyow/validations/length"
8
+ require "pakyow/validations/presence"
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/support/class_state"
4
+
5
+ require "pakyow/errors"
6
+
7
+ module Pakyow
8
+ class Validator
9
+ class Result
10
+ def initialize
11
+ @errors = []
12
+ end
13
+
14
+ def error(validation, options)
15
+ @errors << [validation, options]
16
+ end
17
+
18
+ def valid?
19
+ @errors.empty?
20
+ end
21
+
22
+ def messages
23
+ @errors.map { |validation, options|
24
+ options[:message] || validation.message(**options)
25
+ }
26
+ end
27
+ end
28
+
29
+ extend Support::ClassState
30
+ class_state :validation_objects, default: {}
31
+
32
+ class << self
33
+ def register_validation(validation_object)
34
+ @validation_objects[validation_object.name] = validation_object
35
+ end
36
+
37
+ def validation_object_for(validation)
38
+ @validation_objects[validation] || raise(
39
+ UnknownValidationError.new_with_message(validation: validation)
40
+ )
41
+ end
42
+ end
43
+
44
+ require "pakyow/validations"
45
+
46
+ def initialize(key = nil, &block)
47
+ @key = key
48
+ @validations = []
49
+
50
+ if block
51
+ instance_eval(&block)
52
+ end
53
+ end
54
+
55
+ def any?
56
+ @validations.any?
57
+ end
58
+
59
+ def validate(validation_name = nil, **options)
60
+ validation_object = if block_given?
61
+ Validations::Inline.new(validation_name, Proc.new)
62
+ else
63
+ self.class.validation_object_for(validation_name)
64
+ end
65
+
66
+ @validations << [validation_object, options]
67
+ end
68
+
69
+ def call(values, context: nil)
70
+ result = Result.new
71
+
72
+ @validations.each do |validation, options|
73
+ unless validation.valid?(values, key: @key, context: context, **options)
74
+ result.error(validation, options)
75
+ end
76
+ end
77
+
78
+ result
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ require "pakyow/support/core_refinements/array/ensurable"
6
+
7
+ require "pakyow/types"
8
+ require "pakyow/validator"
9
+
10
+ module Pakyow
11
+ class Verifier
12
+ class Result
13
+ def initialize
14
+ @errors = {}
15
+ @nested = {}
16
+ @validation = nil
17
+ end
18
+
19
+ def error(key, message)
20
+ (@errors[key] ||= []) << message
21
+ end
22
+
23
+ def nested(key, result)
24
+ @nested[key] = result
25
+ end
26
+
27
+ def validation(result)
28
+ @validation = result
29
+ end
30
+
31
+ def verified?
32
+ @errors.empty? && (!validating? || @validation.valid?) && @nested.all? { |_, result|
33
+ result.verified?
34
+ }
35
+ end
36
+
37
+ def validating?
38
+ !@validation.nil?
39
+ end
40
+
41
+ def messages
42
+ if validating?
43
+ messages = @validation.messages
44
+ else
45
+ messages = {}
46
+
47
+ @errors.each_pair do |key, value|
48
+ messages[key] = value
49
+ end
50
+
51
+ @nested.each_pair do |key, verifier|
52
+ nested_messages = verifier.messages
53
+
54
+ unless nested_messages.empty?
55
+ messages[key] = nested_messages
56
+ end
57
+ end
58
+ end
59
+
60
+ messages
61
+ end
62
+ end
63
+
64
+ using Support::Refinements::Array::Ensurable
65
+
66
+ extend Forwardable
67
+ def_delegators :@validator, :validate
68
+
69
+ def initialize(key = nil, &block)
70
+ @key = key
71
+ @types = {}
72
+ @messages = {}
73
+ @required_keys = []
74
+ @optional_keys = []
75
+ @allowable_keys = []
76
+ @verifiers_by_key = {}
77
+ @validator = Validator.new(key)
78
+
79
+ if block
80
+ instance_eval(&block)
81
+ end
82
+ end
83
+
84
+ def required(key, type = nil, message: "is required", &block)
85
+ key = key.to_sym
86
+ @required_keys.push(key).uniq!
87
+ @allowable_keys.push(key).uniq!
88
+ @messages[key] = message
89
+
90
+ if type
91
+ @types[key] = Types.type_for(type)
92
+ end
93
+
94
+ if block
95
+ @verifiers_by_key[key] = self.class.new(key, &block)
96
+ end
97
+ end
98
+
99
+ def optional(key, type = nil, &block)
100
+ key = key.to_sym
101
+ @optional_keys.push(key).uniq!
102
+ @allowable_keys.push(key).uniq!
103
+
104
+ if type
105
+ @types[key] = Types.type_for(type)
106
+ end
107
+
108
+ if block
109
+ @verifiers_by_key[key] = self.class.new(key, &block)
110
+ end
111
+ end
112
+
113
+ def call(values, context: nil)
114
+ values ||= {}
115
+
116
+ if should_sanitize?(values)
117
+ values = sanitize(values)
118
+ end
119
+
120
+ result = Result.new
121
+
122
+ if should_validate?(values)
123
+ result.validation(@validator.call(values, context: context))
124
+ end
125
+
126
+ @allowable_keys.each do |allowable_key|
127
+ if values[allowable_key].nil?
128
+ if @required_keys.include?(allowable_key)
129
+ result.error(allowable_key, @messages[allowable_key])
130
+ else
131
+ next
132
+ end
133
+ end
134
+
135
+ if verifier_for_key = @verifiers_by_key[allowable_key]
136
+ Array.ensure(values[allowable_key]).each do |values_for_key|
137
+ result.nested(allowable_key, verifier_for_key.call(values_for_key, context: context))
138
+ end
139
+ end
140
+ end
141
+
142
+ result
143
+ end
144
+
145
+ private
146
+
147
+ def sanitize(values)
148
+ values.select! do |key, _|
149
+ @allowable_keys.include?(key.to_sym)
150
+ end
151
+
152
+ @allowable_keys.each do |key|
153
+ key = key.to_sym
154
+
155
+ if values.key?(key)
156
+ value = values[key]
157
+
158
+ if type = @types[key]
159
+ value = type[value]
160
+ end
161
+
162
+ values[key] = value
163
+ end
164
+ end
165
+
166
+ values
167
+ end
168
+
169
+ def should_sanitize?(values)
170
+ values.is_a?(Pakyow::Support::IndifferentHash) || values.is_a?(Hash) || values.is_a?(Connection::Params)
171
+ end
172
+
173
+ def should_validate?(values)
174
+ @validator.any? && !values.nil?
175
+ end
176
+ end
177
+ end
data/lib/pakyow.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/version"
4
+ require "pakyow/error"
5
+ require "pakyow/environment"