croesus 0.1.3
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 +7 -0
- data/.gitignore +121 -0
- data/.ruby-version +1 -0
- data/API_operation.txt +197 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +0 -0
- data/README.md +146 -0
- data/Rakefile +26 -0
- data/bin/console_cmd.rb +133 -0
- data/croesus.gemspec +39 -0
- data/lib/croesus/associations.rb +46 -0
- data/lib/croesus/attribute.rb +41 -0
- data/lib/croesus/attributes.rb +93 -0
- data/lib/croesus/coerce.rb +110 -0
- data/lib/croesus/coercions/boolean_definitions.rb +32 -0
- data/lib/croesus/coercions/date_definitions.rb +30 -0
- data/lib/croesus/coercions/date_time_definitions.rb +30 -0
- data/lib/croesus/coercions/fixnum_definitions.rb +32 -0
- data/lib/croesus/coercions/float_definitions.rb +30 -0
- data/lib/croesus/coercions/hash_definitions.rb +27 -0
- data/lib/croesus/coercions/integer_definitions.rb +29 -0
- data/lib/croesus/coercions/string_definitions.rb +43 -0
- data/lib/croesus/coercions/time_definitions.rb +30 -0
- data/lib/croesus/core_ext/blank.rb +123 -0
- data/lib/croesus/core_ext/hash.rb +185 -0
- data/lib/croesus/dsl/dsl.rb +35 -0
- data/lib/croesus/dsl/helpers.rb +43 -0
- data/lib/croesus/dsl/mod_factory.rb +191 -0
- data/lib/croesus/dsl/resource_dsl.rb +59 -0
- data/lib/croesus/dsl/route_dsl.rb +42 -0
- data/lib/croesus/identity_map.rb +98 -0
- data/lib/croesus/platform.rb +47 -0
- data/lib/croesus/querying.rb +63 -0
- data/lib/croesus/resources/about.rb +15 -0
- data/lib/croesus/resources/basic_methods.rb +36 -0
- data/lib/croesus/resources/connectivity.rb +42 -0
- data/lib/croesus/resources/container.rb +135 -0
- data/lib/croesus/resources/fault.rb +38 -0
- data/lib/croesus/resources/fault_effect.rb +24 -0
- data/lib/croesus/resources/group.rb +37 -0
- data/lib/croesus/resources/host.rb +26 -0
- data/lib/croesus/resources/job.rb +27 -0
- data/lib/croesus/resources/namespace.rb +39 -0
- data/lib/croesus/resources/policy.rb +38 -0
- data/lib/croesus/resources/source.rb +86 -0
- data/lib/croesus/resources/source_config.rb +54 -0
- data/lib/croesus/resources/source_environment.rb +58 -0
- data/lib/croesus/resources/source_repository.rb +14 -0
- data/lib/croesus/resources/system_info.rb +21 -0
- data/lib/croesus/resources/timeflow.rb +40 -0
- data/lib/croesus/resources/timeflow_snapshot.rb +38 -0
- data/lib/croesus/utils.rb +262 -0
- data/lib/croesus/validations/many.rb +27 -0
- data/lib/croesus/validations/optional.rb +27 -0
- data/lib/croesus/validations.rb +91 -0
- data/lib/croesus/validators/base.rb +47 -0
- data/lib/croesus/validators/boolean_validator.rb +32 -0
- data/lib/croesus/validators/email_validator.rb +36 -0
- data/lib/croesus/validators/enumerable_validator.rb +40 -0
- data/lib/croesus/validators/hash_validator.rb +50 -0
- data/lib/croesus/validators/lambda_validator.rb +54 -0
- data/lib/croesus/validators/many_validator.rb +57 -0
- data/lib/croesus/validators/optional_validator.rb +41 -0
- data/lib/croesus/validators/presence_validator.rb +36 -0
- data/lib/croesus/validators/simple_type_validators.rb +38 -0
- data/lib/croesus/validators/simple_validator.rb +40 -0
- data/lib/croesus/version.rb +43 -0
- data/lib/croesus/web_client/web_client.rb +153 -0
- data/lib/croesus/web_client/web_request.rb +77 -0
- data/lib/croesus/web_client/web_response.rb +70 -0
- data/lib/croesus.rb +250 -0
- metadata +325 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
# Add #blank? and #present? methods to Object class.
|
21
|
+
class Object
|
22
|
+
# Returns true if the object is nil or empty (if applicable)
|
23
|
+
#
|
24
|
+
# [].blank? #=> true
|
25
|
+
# [1].blank? #=> false
|
26
|
+
# [nil].blank? #=> false
|
27
|
+
#
|
28
|
+
# @return [TrueClass, FalseClass]
|
29
|
+
#
|
30
|
+
def blank?
|
31
|
+
nil? || (respond_to?(:empty?) && empty?)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns true if the object is NOT nil or empty
|
35
|
+
#
|
36
|
+
# [].present? #=> false
|
37
|
+
# [1].present? #=> true
|
38
|
+
# [nil].present? #=> true
|
39
|
+
#
|
40
|
+
# @return [TrueClass, FalseClass]
|
41
|
+
#
|
42
|
+
def present?
|
43
|
+
!blank?
|
44
|
+
end
|
45
|
+
end # class Object
|
46
|
+
|
47
|
+
# Add #blank? method to NilClass class.
|
48
|
+
class NilClass
|
49
|
+
# Nil is always blank
|
50
|
+
#
|
51
|
+
# nil.blank? #=> true
|
52
|
+
#
|
53
|
+
# @return [TrueClass]
|
54
|
+
#
|
55
|
+
def blank?
|
56
|
+
true
|
57
|
+
end
|
58
|
+
end # class NilClass
|
59
|
+
|
60
|
+
# Add #blank? method to TrueClass class.
|
61
|
+
class TrueClass
|
62
|
+
# True is never blank.
|
63
|
+
#
|
64
|
+
# true.blank? #=> false
|
65
|
+
#
|
66
|
+
# @return [FalseClass]
|
67
|
+
#
|
68
|
+
def blank?
|
69
|
+
false
|
70
|
+
end
|
71
|
+
end # class TrueClass
|
72
|
+
|
73
|
+
# Add #blank? method to FalseClass class.
|
74
|
+
class FalseClass
|
75
|
+
# False is always blank.
|
76
|
+
#
|
77
|
+
# false.blank? #=> true
|
78
|
+
#
|
79
|
+
# @return [TrueClass]
|
80
|
+
#
|
81
|
+
def blank?
|
82
|
+
true
|
83
|
+
end
|
84
|
+
end # class FalseClass
|
85
|
+
|
86
|
+
# Add #blank? method to Hash class.
|
87
|
+
class Hash
|
88
|
+
# A hash is blank if it's empty:
|
89
|
+
#
|
90
|
+
# {}.blank? # => true
|
91
|
+
# { key: 'value' }.blank? # => false
|
92
|
+
alias_method :blank?, :empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
# Add #blank? method to String class.
|
96
|
+
class String
|
97
|
+
# Strips out whitespace then tests if the string is empty.
|
98
|
+
#
|
99
|
+
# "".blank? #=> true
|
100
|
+
# " ".blank? #=> true
|
101
|
+
# " hey ho ".blank? #=> false
|
102
|
+
#
|
103
|
+
# @return [TrueClass, FalseClass]
|
104
|
+
#
|
105
|
+
def blank?
|
106
|
+
strip.empty?
|
107
|
+
end
|
108
|
+
end # class String
|
109
|
+
|
110
|
+
# Add #blank? method to Numeric class.
|
111
|
+
class Numeric
|
112
|
+
# Numerics are never blank
|
113
|
+
#
|
114
|
+
# 0.blank? #=> false
|
115
|
+
# 1.blank? #=> false
|
116
|
+
# 6.54321.blank? #=> false
|
117
|
+
#
|
118
|
+
# @return [FalseClass]
|
119
|
+
#
|
120
|
+
def blank?
|
121
|
+
false
|
122
|
+
end
|
123
|
+
end # class Numeric
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Hash
|
21
|
+
# Returns a compacted copy (contains no key/value pairs having
|
22
|
+
# nil? values)
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# hash = { a: 100, b: nil, c: false, d: '' }
|
26
|
+
# hash.compact # => { a: 100, c: false, d: '' }
|
27
|
+
# hash # => { a: 100, b: nil, c: false, d: '' }
|
28
|
+
#
|
29
|
+
# @return [Hash]
|
30
|
+
#
|
31
|
+
def compact
|
32
|
+
select { |_, value| !value.nil? }
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns a new hash with all keys converted using the block operation.
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# hash = { name: 'Tiggy', age: '15' }
|
39
|
+
#
|
40
|
+
# hash.transform_keys{ |key| key.to_s.upcase }
|
41
|
+
# # => { "AGE" => "15", "NAME" => "Tiggy" }
|
42
|
+
#
|
43
|
+
# @return [Hash]
|
44
|
+
#
|
45
|
+
def transform_keys
|
46
|
+
enum_for(:transform_keys) unless block_given?
|
47
|
+
result = self.class.new
|
48
|
+
each_key do |key|
|
49
|
+
result[yield(key)] = self[key]
|
50
|
+
end
|
51
|
+
result
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns a new hash, recursively converting all keys by the
|
55
|
+
# block operation.
|
56
|
+
#
|
57
|
+
# @return [Hash]
|
58
|
+
#
|
59
|
+
def recursively_transform_keys(&block)
|
60
|
+
_recursively_transform_keys_in_object(self, &block)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns a new hash with all keys downcased and converted
|
64
|
+
# to symbols.
|
65
|
+
#
|
66
|
+
# @return [Hash]
|
67
|
+
#
|
68
|
+
def normalize_keys
|
69
|
+
transform_keys { |key| key.downcase.to_sym rescue key }
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns a new Hash, recursively downcasing and converting all
|
73
|
+
# keys to symbols.
|
74
|
+
#
|
75
|
+
# @return [Hash]
|
76
|
+
#
|
77
|
+
def recursively_normalize_keys
|
78
|
+
recursively_transform_keys { |key| key.downcase.to_sym rescue key }
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns a new hash with all keys converted to symbols.
|
82
|
+
#
|
83
|
+
# @return [Hash]
|
84
|
+
#
|
85
|
+
def symbolize_keys
|
86
|
+
transform_keys { |key| key.to_sym rescue key }
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns a new Hash, recursively converting all keys to symbols.
|
90
|
+
#
|
91
|
+
# @return [Hash]
|
92
|
+
#
|
93
|
+
def recursively_symbolize_keys
|
94
|
+
recursively_transform_keys { |key| key.to_sym rescue key }
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns a new hash with all keys converted to strings.
|
98
|
+
#
|
99
|
+
# @return [Hash]
|
100
|
+
#
|
101
|
+
def stringify_keys
|
102
|
+
transform_keys { |key| key.to_s rescue key }
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns a new Hash, recursively converting all keys to strings.
|
106
|
+
#
|
107
|
+
# @return [Hash]
|
108
|
+
#
|
109
|
+
def recursively_stringify_key
|
110
|
+
recursively_transform_keys { |key| key.to_s rescue key }
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns a new hash with all keys converted to strings and the
|
114
|
+
# first letter capitalized.
|
115
|
+
#
|
116
|
+
# @return [Hash]
|
117
|
+
#
|
118
|
+
def capitalize_keys
|
119
|
+
transform_keys { |key| key.Upcase.to_s.capitalize rescue key }
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns a new Hash, recursively converting all keys to strings
|
123
|
+
# and the first letter capitalized.
|
124
|
+
#
|
125
|
+
# @return [Hash]
|
126
|
+
#
|
127
|
+
def recursively_capitalize_key
|
128
|
+
recursively_transform_keys { |key| key.to_s.capitalize rescue key }
|
129
|
+
end
|
130
|
+
|
131
|
+
class UndefinedPathError < StandardError; end
|
132
|
+
# Recursively searchs a nested datastructure for a key and returns
|
133
|
+
# the value. If a block is provided its value will be returned if
|
134
|
+
# the key does not exist
|
135
|
+
#
|
136
|
+
# @example
|
137
|
+
# options = { server: { location: { row: { rack: 34 } } } }
|
138
|
+
# options.recursive_fetch :server, :location, :row, :rack
|
139
|
+
# # => 34
|
140
|
+
# options.recursive_fetch(:non_existent_key) { 'default' }
|
141
|
+
# # => "default"
|
142
|
+
#
|
143
|
+
# @return [Hash, Array, String] value for key
|
144
|
+
#
|
145
|
+
def recursive_fetch(*args, &block)
|
146
|
+
args.reduce(self) do |obj, arg|
|
147
|
+
begin
|
148
|
+
arg = Integer(arg) if obj.is_a? Array
|
149
|
+
obj.fetch(arg)
|
150
|
+
rescue ArgumentError, IndexError, NoMethodError => e
|
151
|
+
break block.call(arg) if block
|
152
|
+
raise UndefinedPathError, "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def recursive_merge(other)
|
158
|
+
hash = self.dup
|
159
|
+
other.each do |key, value|
|
160
|
+
myval = self[key]
|
161
|
+
if value.is_a?(Hash) && myval.is_a?(Hash)
|
162
|
+
hash[key] = myval.recursive_merge(value)
|
163
|
+
else
|
164
|
+
hash[key] = value
|
165
|
+
end
|
166
|
+
end
|
167
|
+
hash
|
168
|
+
end
|
169
|
+
|
170
|
+
private # P R O P R I E T À P R I V A T A divieto di accesso
|
171
|
+
|
172
|
+
# support methods for recursively transforming nested hashes and arrays
|
173
|
+
def _recursively_transform_keys_in_object(object, &block)
|
174
|
+
case object
|
175
|
+
when Hash
|
176
|
+
object.each_with_object({}) do |(key, value), result|
|
177
|
+
result[yield(key)] = _recursively_transform_keys_in_object(value, &block)
|
178
|
+
end
|
179
|
+
when Array
|
180
|
+
object.map { |e| _recursively_transform_keys_in_object(e, &block) }
|
181
|
+
else
|
182
|
+
object
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus
|
21
|
+
class DSL
|
22
|
+
def self.evaluate(&block)
|
23
|
+
raise 'You need a block to build!' unless block_given?
|
24
|
+
DSL.new(&block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(&block)
|
28
|
+
instance_eval(&block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def resource(resource_name, &block)
|
32
|
+
Croesus::ResourceDSL.new(resource_name, &block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus::DSLHelpers
|
21
|
+
def self.included(base)
|
22
|
+
base.extend(ClassMethods)
|
23
|
+
end
|
24
|
+
private_class_method :included
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def setter(*method_names)
|
28
|
+
method_names.each do |name|
|
29
|
+
send :define_method, name do |data|
|
30
|
+
instance_variable_set "@#{name}".to_sym, data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def varags_setter(*method_names)
|
36
|
+
method_names.each do |name|
|
37
|
+
send :define_method, name do |*data|
|
38
|
+
instance_variable_set "@#{name}".to_sym, data
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
# Registry for anonymous methods, not so anonymous now are you!
|
21
|
+
#
|
22
|
+
module Croesus::AnonoMod
|
23
|
+
|
24
|
+
@registry = {}
|
25
|
+
|
26
|
+
def self.registry
|
27
|
+
@registry
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.register(resource_name, mod)
|
31
|
+
registry[resource_name] = mod
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Mysteries abound, dynamic box of factory creation for API resources.
|
36
|
+
#
|
37
|
+
module Croesus::ModFactory
|
38
|
+
include Croesus::Utils
|
39
|
+
|
40
|
+
def initialize(name, mod = Module.new)
|
41
|
+
@mod = mod
|
42
|
+
@name = name
|
43
|
+
Croesus::AnonoMod.register(name, @mod)
|
44
|
+
assemble_module
|
45
|
+
@mod
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sets the description the Delphix Resource.
|
49
|
+
#
|
50
|
+
# @param [String] description
|
51
|
+
# the description of the API resource
|
52
|
+
#
|
53
|
+
# @return [Nil]
|
54
|
+
#
|
55
|
+
# @api public
|
56
|
+
def add_description(description)
|
57
|
+
@mod.instance_variable_set(:@description, description)
|
58
|
+
@mod.define_singleton_method(:description) { @description }
|
59
|
+
end
|
60
|
+
|
61
|
+
# Sets the root endpoint for the resource.
|
62
|
+
#
|
63
|
+
# @param [String] root
|
64
|
+
# the root endpoint for the resource
|
65
|
+
#
|
66
|
+
# @return [Nil]
|
67
|
+
#
|
68
|
+
# @api public
|
69
|
+
def add_root_endpoint(root)
|
70
|
+
@mod.instance_variable_set(:@root, root)
|
71
|
+
@mod.define_singleton_method(:root) { @root }
|
72
|
+
end
|
73
|
+
|
74
|
+
def add_method(dsl_object)
|
75
|
+
raw = dsl_object.to_hash
|
76
|
+
name = raw[:name]
|
77
|
+
verb = raw[:verb]
|
78
|
+
@mod.methods[name] = raw[:description]
|
79
|
+
@mod.singleton_class.send(:alias_method, name, "api_#{verb.downcase}")
|
80
|
+
define_help
|
81
|
+
end
|
82
|
+
|
83
|
+
private # P R O P R I E T À P R I V A T A Vietato L'accesso
|
84
|
+
|
85
|
+
# Internal: Define methods to handle a verb.
|
86
|
+
#
|
87
|
+
# verbs - A list of verbs to define methods for.
|
88
|
+
#
|
89
|
+
# Examples
|
90
|
+
#
|
91
|
+
# define_verbs :get, :post
|
92
|
+
#
|
93
|
+
# Returns nil.
|
94
|
+
def define_verbs(*verbs)
|
95
|
+
verbs.each do |verb|
|
96
|
+
define_verb(verb)
|
97
|
+
define_api_verb(verb)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Internal: Defines a method to handle HTTP requests with the passed in
|
102
|
+
# verb.
|
103
|
+
#
|
104
|
+
# verb - Symbol name of the verb (e.g. :get).
|
105
|
+
#
|
106
|
+
# Examples
|
107
|
+
#
|
108
|
+
# define_verb :get
|
109
|
+
# # => get 'http://server.xyz/path'
|
110
|
+
#
|
111
|
+
# Returns nil.
|
112
|
+
def define_verb(verb)
|
113
|
+
@mod.define_singleton_method(verb.to_sym) do |*args, &block|
|
114
|
+
class_eval "Croesus.#{verb}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Internal: Defines a method to handle HTTP requests with the passed in
|
119
|
+
# verb to a api endpoint.
|
120
|
+
#
|
121
|
+
# verb - Symbol name of the verb (e.g. :get).
|
122
|
+
#
|
123
|
+
# Examples
|
124
|
+
#
|
125
|
+
# define_api_verb :get
|
126
|
+
# # => api_get '/resources/json/delphix/environment'
|
127
|
+
#
|
128
|
+
# Returns nil.
|
129
|
+
def define_api_verb(verb)
|
130
|
+
@mod.define_singleton_method("api_#{verb}") do |*args, &block|
|
131
|
+
class_eval "Croesus.#{verb}(url(*args)).body"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def define_help
|
136
|
+
@mod.define_singleton_method :help do
|
137
|
+
puts
|
138
|
+
dyno_width = terminal_dimensions[0] - 32
|
139
|
+
header title: "Available commands for #{@delphix_object}",
|
140
|
+
align: 'center', width: terminal_dimensions[0]
|
141
|
+
table border: true do
|
142
|
+
row header: true, color: 'red' do
|
143
|
+
column 'Num', width: 3, align: 'right', color: 'blue', padding: 0
|
144
|
+
column 'Method Name', width: 18, align: 'left', padding: 0
|
145
|
+
column "Description (http://#{Croesus.server}/api/#" \
|
146
|
+
"#{@delphix_object.downcase})",
|
147
|
+
width: dyno_width, align: 'left', padding: 0
|
148
|
+
end
|
149
|
+
(@methods.keys).sort.each.with_index(1) do |method, i|
|
150
|
+
row do
|
151
|
+
column '%02d' % i
|
152
|
+
column method
|
153
|
+
column @methods[method.to_sym]
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
puts @description
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def assemble_module
|
162
|
+
add_included_hook
|
163
|
+
add_instance_variables
|
164
|
+
be_polite_and_debuggable
|
165
|
+
define_verbs('get', 'post', 'delete')
|
166
|
+
end
|
167
|
+
|
168
|
+
def add_included_hook
|
169
|
+
@mod.send :include, Croesus::BasicMethods
|
170
|
+
@mod.send :include, CommandLineReporter
|
171
|
+
@mod.send :include, Croesus::Utils
|
172
|
+
end
|
173
|
+
|
174
|
+
def add_instance_variables
|
175
|
+
@mod.instance_variable_set(:@delphix_object, classify(@name))
|
176
|
+
@mod.define_singleton_method(:delphix_object) { @delphix_object }
|
177
|
+
|
178
|
+
@mod.instance_variable_set(:@methods, {})
|
179
|
+
@mod.define_singleton_method(:methods) { @methods }
|
180
|
+
end
|
181
|
+
|
182
|
+
def be_polite_and_debuggable
|
183
|
+
@mod.define_singleton_method :to_s do
|
184
|
+
"<#{self.class.name}:#{self.name}:#{object_id}>"
|
185
|
+
end
|
186
|
+
|
187
|
+
@mod.define_singleton_method :inspect do
|
188
|
+
"<#{self.class.name}:#{self.name}:#{object_id} #{instance_variables}>"
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus
|
21
|
+
class ResourceDSL
|
22
|
+
include Croesus::DSLHelpers
|
23
|
+
include Croesus::ModFactory
|
24
|
+
|
25
|
+
setter :description, :root
|
26
|
+
varags_setter
|
27
|
+
|
28
|
+
def initialize(name, &block)
|
29
|
+
Croesus::ModFactory.instance_method(:initialize).bind(self).call(name)
|
30
|
+
instance_eval(&block)
|
31
|
+
@description ||= description
|
32
|
+
@root ||= root
|
33
|
+
add_description(@description)
|
34
|
+
add_root_endpoint(@root)
|
35
|
+
end
|
36
|
+
|
37
|
+
# The following HTTP methods are supported by the Delphix Appliance:
|
38
|
+
#
|
39
|
+
# GET - Retrieve data from the server where complex input is not needed.
|
40
|
+
# All GET requests are guaranteed to be read-only, but not all
|
41
|
+
# read-only requests are required to use GET. Simple input
|
42
|
+
# (strings, number, boolean values) can be passed as query
|
43
|
+
# parameters.
|
44
|
+
# POST - Issue a read/write operation, or make a read-only call that
|
45
|
+
# requires complex input. The optional body of the call is
|
46
|
+
# expressed as JSON.
|
47
|
+
# DELETE - Delete an object on the system. For languages that don't provide
|
48
|
+
# a native wrapper for DELETE, or for delete operations with
|
49
|
+
# optional input, all delete operations can also be invoked as POST
|
50
|
+
# to the same URL with /delete appended to it.
|
51
|
+
def get(path, &block) route 'GET', path, &block end
|
52
|
+
def post(path, &block) route 'POST', path, &block end
|
53
|
+
def delete(path, &block) route 'DELETE', path, &block end
|
54
|
+
|
55
|
+
def route(verb, path, &block)
|
56
|
+
add_method(Croesus::RouteDSL.new(verb, path, &block))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus
|
21
|
+
class RouteDSL
|
22
|
+
include Croesus::DSLHelpers
|
23
|
+
|
24
|
+
setter :name, :verb, :path, :description, :input, :returns
|
25
|
+
varags_setter
|
26
|
+
|
27
|
+
def initialize(verb, path, &block)
|
28
|
+
instance_eval(&block)
|
29
|
+
@name ||= name
|
30
|
+
@verb = verb
|
31
|
+
@path = path
|
32
|
+
@input ||= nil
|
33
|
+
@returns ||= nil
|
34
|
+
@description ||= description
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_hash
|
38
|
+
{ name: @name, verb: @verb, path: @path, input: @input,
|
39
|
+
returns: @returns, description: @description }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|