hobby-json-keys 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hobby/json/keys.rb +188 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02e55dc01e1bd2d2c117ea0e9bc52157c6af771e970c5a968a2b715b819bbb10
4
+ data.tar.gz: 5fbe81d8c15f9246caaeea89616d87b19ad93be80ff87afd99e10d1152e77539
5
+ SHA512:
6
+ metadata.gz: 88f9d00be653d7fd77a6d662ce0aca056ea8975cecb4c283c49d1f4c9e348f9e5c7cd9a2ee7031341c39747311fe47f27933e3a5dceddcb8e1d47ed978c94517
7
+ data.tar.gz: d89a3ca4c4200087048dc14dea6e3e0cad5463dea442104288937fa32d9d5576277795881a4c26bc3ab1e2a2acd7962e71ec11826df43f9291f7e4641fc7940e
@@ -0,0 +1,188 @@
1
+ module Hobby
2
+ module JSON
3
+ module Keys
4
+ class KeyParser
5
+ def initialize &block
6
+ @block = -> json, key {
7
+ key = key.to_s if key.is_a? Symbol
8
+ block.call json, key
9
+ }
10
+ end
11
+
12
+ def [] key
13
+ -> json {
14
+ @block.call json, key
15
+ }
16
+ end
17
+ end
18
+
19
+ class ValueParser
20
+ def initialize &block
21
+ @block = block
22
+ end
23
+
24
+ def [] type
25
+ -> value {
26
+ @block.call value, type
27
+ }
28
+ end
29
+ end
30
+
31
+ require 'hobby/json'
32
+
33
+ def self.included app
34
+ app.include JSON
35
+ app.extend Singleton
36
+
37
+ %i[
38
+ String
39
+ Array
40
+ Hash
41
+ ].each do |symbol|
42
+ app.type symbol do
43
+ is_a Object.const_get symbol
44
+ is_not_empty
45
+ end
46
+ end
47
+ end
48
+
49
+ def keys
50
+ @keys ||= begin
51
+ self.class.keys
52
+ .transform_values { |parser| parser[json] }
53
+ .compact
54
+ rescue
55
+ response.status = 400
56
+ halt
57
+ end
58
+ end
59
+
60
+ module Singleton
61
+ def keys
62
+ @keys ||= {}
63
+ end
64
+
65
+ def key key, type = nil
66
+ parser = if type
67
+ Both ValueMustExistFor[key], TypeOfValueMustBe[type]
68
+ else
69
+ ValueMustExistFor[key]
70
+ end
71
+
72
+ self.keys[key] = parser
73
+ end
74
+
75
+ def optional_key key, type = nil
76
+ parser = if type
77
+ Both ValueMayExistFor[key], TypeOfValueMustBe[type]
78
+ else
79
+ ValueMayExistFor[key]
80
+ end
81
+
82
+ self.keys[key] = parser
83
+ end
84
+
85
+
86
+ def Both key_parser, value_parser
87
+ -> json {
88
+ if value = key_parser[json]
89
+ value_parser[value]
90
+ end
91
+ }
92
+ end
93
+
94
+
95
+ ValueMustExistFor = KeyParser.new do |json, key|
96
+ value = json[key]
97
+ fail unless value
98
+ value
99
+ end
100
+
101
+ ValueMayExistFor = KeyParser.new do |json, key|
102
+ json[key]
103
+ end
104
+
105
+ TypeOfValueMustBe = ValueParser.new do |value, type|
106
+ fail unless type === value
107
+ if [String, Array, Hash].include? type
108
+ fail if value.empty?
109
+ end
110
+ value
111
+ end
112
+
113
+
114
+ def optional &block
115
+ new_class = dup
116
+ new_class.define_singleton_method :key, &method(:optional_key)
117
+ new_class.instance_exec &block
118
+ self.keys.merge! new_class.keys
119
+ end
120
+
121
+ def initialize_copy
122
+ super
123
+ @keys = {}
124
+ end
125
+
126
+
127
+ def types
128
+ @types ||= {}
129
+ end
130
+
131
+ def type symbol, &default
132
+ types[symbol] = Type.new &default
133
+ define_singleton_method symbol do |&custom|
134
+ types[symbol].expand &custom
135
+ end
136
+ end
137
+ end
138
+
139
+ class Type
140
+ def initialize &block
141
+ @gates = {}
142
+ instance_exec &block
143
+ end
144
+
145
+ attr_reader :gates
146
+
147
+ def expand &block
148
+ new_type = dup
149
+ new_type.instance_exec &block
150
+ new_type
151
+ end
152
+
153
+ def initialize_copy original
154
+ super
155
+ @gates = original.gates.dup
156
+ end
157
+
158
+ def is_a type
159
+ @gates[__callee__] = -> it {
160
+ it.is_a? type
161
+ }
162
+ end
163
+
164
+ def is_not_empty
165
+ @gates[__callee__] = -> it {
166
+ not it.empty?
167
+ }
168
+ end
169
+
170
+ def may_be_empty
171
+ @gates.delete :is_not_empty
172
+ end
173
+
174
+ def size value
175
+ @gates[__callee__] = -> it {
176
+ value === it.size
177
+ }
178
+ end
179
+
180
+ def === it
181
+ @gates.values.each do |gate|
182
+ fail unless gate === it
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
188
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hobby-json-keys
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Chernov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hobby-json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.6
27
+ description:
28
+ email: chertoly@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/hobby/json/keys.rb
34
+ homepage: https://github.com/ch1c0t/hobby-json-keys
35
+ licenses:
36
+ - ISC
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.1.6
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A Hobby extension to parse JSON requests.
57
+ test_files: []