api_recipes 2.10.3 → 3.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1276 -28
- data/UPGRADING.md +473 -0
- data/exe/api_recipes +6 -0
- data/lib/api_recipes/api.rb +71 -49
- data/lib/api_recipes/cli/upgrade.rb +179 -0
- data/lib/api_recipes/cli.rb +70 -0
- data/lib/api_recipes/configuration.rb +185 -36
- data/lib/api_recipes/endpoint.rb +68 -118
- data/lib/api_recipes/errors.rb +137 -0
- data/lib/api_recipes/internals/compiler.rb +680 -0
- data/lib/api_recipes/internals/context.rb +237 -0
- data/lib/api_recipes/internals/credential.rb +86 -0
- data/lib/api_recipes/internals/declaration.rb +225 -0
- data/lib/api_recipes/internals/definition.rb +179 -0
- data/lib/api_recipes/internals/handles.rb +81 -0
- data/lib/api_recipes/internals/level.rb +192 -0
- data/lib/api_recipes/internals/path_template.rb +131 -0
- data/lib/api_recipes/internals/registry.rb +146 -0
- data/lib/api_recipes/internals/request.rb +285 -0
- data/lib/api_recipes/internals/template.rb +73 -0
- data/lib/api_recipes/response.rb +90 -9
- data/lib/api_recipes/version.rb +3 -1
- data/lib/api_recipes.rb +44 -143
- metadata +43 -13
- data/lib/api_recipes/exceptions.rb +0 -91
- data/lib/api_recipes/route.rb +0 -165
- data/lib/api_recipes/settings.rb +0 -30
- data/lib/api_recipes/utils.rb +0 -195
data/lib/api_recipes/utils.rb
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
class Hash
|
|
2
|
-
# Hash#transform_keys and #transform_keys! are Ruby's own (since 2.5): the helpers
|
|
3
|
-
# below build on them. Redefining them here broke Hash subclasses whose constructor
|
|
4
|
-
# needs arguments and dropped the hash argument added in Ruby 3.0.
|
|
5
|
-
|
|
6
|
-
# Returns a new hash with all keys converted to strings.
|
|
7
|
-
#
|
|
8
|
-
# hash = { name: 'Rob', age: '28' }
|
|
9
|
-
#
|
|
10
|
-
# hash.stringify_keys
|
|
11
|
-
# # => {"name"=>"Rob", "age"=>"28"}
|
|
12
|
-
def stringify_keys
|
|
13
|
-
transform_keys{ |key| key.to_s }
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
# Destructively convert all keys to strings. Same as
|
|
17
|
-
# +stringify_keys+, but modifies +self+.
|
|
18
|
-
def stringify_keys!
|
|
19
|
-
transform_keys!{ |key| key.to_s }
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
# Returns a new hash with all keys converted to symbols, as long as
|
|
23
|
-
# they respond to +to_sym+.
|
|
24
|
-
#
|
|
25
|
-
# hash = { 'name' => 'Rob', 'age' => '28' }
|
|
26
|
-
#
|
|
27
|
-
# hash.symbolize_keys
|
|
28
|
-
# # => {:name=>"Rob", :age=>"28"}
|
|
29
|
-
def symbolize_keys
|
|
30
|
-
transform_keys{ |key| key.to_sym rescue key }
|
|
31
|
-
end
|
|
32
|
-
alias_method :to_options, :symbolize_keys
|
|
33
|
-
|
|
34
|
-
# Destructively convert all keys to symbols, as long as they respond
|
|
35
|
-
# to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
|
|
36
|
-
def symbolize_keys!
|
|
37
|
-
transform_keys!{ |key| key.to_sym rescue key }
|
|
38
|
-
end
|
|
39
|
-
alias_method :to_options!, :symbolize_keys!
|
|
40
|
-
|
|
41
|
-
# Validate all keys in a hash match <tt>*valid_keys</tt>, raising
|
|
42
|
-
# ArgumentError on a mismatch.
|
|
43
|
-
#
|
|
44
|
-
# Note that keys are treated differently than HashWithIndifferentAccess,
|
|
45
|
-
# meaning that string and symbol keys will not match.
|
|
46
|
-
#
|
|
47
|
-
# { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
|
|
48
|
-
# { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
|
|
49
|
-
# { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
|
|
50
|
-
def assert_valid_keys(*valid_keys)
|
|
51
|
-
valid_keys.flatten!
|
|
52
|
-
each_key do |k|
|
|
53
|
-
unless valid_keys.include?(k)
|
|
54
|
-
raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
|
60
|
-
#
|
|
61
|
-
# h1 = { a: true, b: { c: [1, 2, 3] } }
|
|
62
|
-
# h2 = { a: false, b: { x: [3, 4, 5] } }
|
|
63
|
-
#
|
|
64
|
-
# h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
|
65
|
-
#
|
|
66
|
-
# Like with Hash#merge in the standard library, a block can be provided
|
|
67
|
-
# to merge values:
|
|
68
|
-
#
|
|
69
|
-
# h1 = { a: 100, b: 200, c: { c1: 100 } }
|
|
70
|
-
# h2 = { b: 250, c: { c1: 200 } }
|
|
71
|
-
# h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
|
|
72
|
-
# # => { a: 100, b: 450, c: { c1: 300 } }
|
|
73
|
-
def deep_merge(other_hash, &block)
|
|
74
|
-
dup.deep_merge!(other_hash, &block)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# Same as +deep_merge+, but modifies +self+.
|
|
78
|
-
def deep_merge!(other_hash, &block)
|
|
79
|
-
merge!(other_hash) do |key, this_val, other_val|
|
|
80
|
-
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
|
81
|
-
this_val.deep_merge(other_val, &block)
|
|
82
|
-
elsif block_given?
|
|
83
|
-
block.call(key, this_val, other_val)
|
|
84
|
-
else
|
|
85
|
-
other_val
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
# Returns a new hash with all keys converted by the block operation.
|
|
91
|
-
# This includes the keys from the root hash and from all
|
|
92
|
-
# nested hashes and arrays.
|
|
93
|
-
#
|
|
94
|
-
# hash = { person: { name: 'Rob', age: '28' } }
|
|
95
|
-
#
|
|
96
|
-
# hash.deep_transform_keys{ |key| key.to_s.upcase }
|
|
97
|
-
# # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
|
|
98
|
-
def deep_transform_keys(&block)
|
|
99
|
-
_deep_transform_keys_in_object(self, &block)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
# Destructively convert all keys by using the block operation.
|
|
103
|
-
# This includes the keys from the root hash and from all
|
|
104
|
-
# nested hashes and arrays.
|
|
105
|
-
def deep_transform_keys!(&block)
|
|
106
|
-
_deep_transform_keys_in_object!(self, &block)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
# Returns a new hash with all keys converted to strings.
|
|
110
|
-
# This includes the keys from the root hash and from all
|
|
111
|
-
# nested hashes and arrays.
|
|
112
|
-
#
|
|
113
|
-
# hash = { person: { name: 'Rob', age: '28' } }
|
|
114
|
-
#
|
|
115
|
-
# hash.deep_stringify_keys
|
|
116
|
-
# # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
|
|
117
|
-
def deep_stringify_keys
|
|
118
|
-
deep_transform_keys{ |key| key.to_s }
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
# Destructively convert all keys to strings.
|
|
122
|
-
# This includes the keys from the root hash and from all
|
|
123
|
-
# nested hashes and arrays.
|
|
124
|
-
def deep_stringify_keys!
|
|
125
|
-
deep_transform_keys!{ |key| key.to_s }
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
# Returns a new hash with all keys converted to symbols, as long as
|
|
129
|
-
# they respond to +to_sym+. This includes the keys from the root hash
|
|
130
|
-
# and from all nested hashes and arrays.
|
|
131
|
-
#
|
|
132
|
-
# hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
|
|
133
|
-
#
|
|
134
|
-
# hash.deep_symbolize_keys
|
|
135
|
-
# # => {:person=>{:name=>"Rob", :age=>"28"}}
|
|
136
|
-
def deep_symbolize_keys
|
|
137
|
-
deep_transform_keys{ |key| key.to_sym rescue key }
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# Destructively convert all keys to symbols, as long as they respond
|
|
141
|
-
# to +to_sym+. This includes the keys from the root hash and from all
|
|
142
|
-
# nested hashes and arrays.
|
|
143
|
-
def deep_symbolize_keys!
|
|
144
|
-
deep_transform_keys!{ |key| key.to_sym rescue key }
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
private
|
|
148
|
-
# support methods for deep transforming nested hashes and arrays
|
|
149
|
-
def _deep_transform_keys_in_object(object, &block)
|
|
150
|
-
case object
|
|
151
|
-
when Hash
|
|
152
|
-
object.each_with_object({}) do |(key, value), result|
|
|
153
|
-
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
|
|
154
|
-
end
|
|
155
|
-
when Array
|
|
156
|
-
object.map {|e| _deep_transform_keys_in_object(e, &block) }
|
|
157
|
-
else
|
|
158
|
-
object
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
def _deep_transform_keys_in_object!(object, &block)
|
|
163
|
-
case object
|
|
164
|
-
when Hash
|
|
165
|
-
object.keys.each do |key|
|
|
166
|
-
value = object.delete(key)
|
|
167
|
-
object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
|
|
168
|
-
end
|
|
169
|
-
object
|
|
170
|
-
when Array
|
|
171
|
-
object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
|
|
172
|
-
else
|
|
173
|
-
object
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
class Array
|
|
179
|
-
# Extracts options from a set of arguments. Removes and returns the last
|
|
180
|
-
# element in the array if it's a hash, otherwise returns a blank hash.
|
|
181
|
-
#
|
|
182
|
-
# def options(*args)
|
|
183
|
-
# args.extract_options!
|
|
184
|
-
# end
|
|
185
|
-
#
|
|
186
|
-
# options(1, 2) # => {}
|
|
187
|
-
# options(1, 2, a: :b) # => {:a=>:b}
|
|
188
|
-
def extract_options!
|
|
189
|
-
if last.is_a?(Hash)
|
|
190
|
-
pop
|
|
191
|
-
else
|
|
192
|
-
{}
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
end
|