gloo 0.3.0 → 0.5.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 +4 -4
- data/.DS_Store +0 -0
- data/.rubocop.yml +73 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +86 -83
- data/Rakefile +7 -6
- data/bin/console +4 -4
- data/gloo.gemspec +22 -18
- data/lib/gloo.rb +6 -6
- data/lib/gloo/app/args.rb +30 -31
- data/lib/gloo/app/engine.rb +80 -30
- data/lib/gloo/app/help.rb +17 -11
- data/lib/gloo/app/info.rb +3 -3
- data/lib/gloo/app/log.rb +17 -17
- data/lib/gloo/app/mode.rb +4 -4
- data/lib/gloo/app/settings.rb +51 -41
- data/lib/gloo/convert/string_to_datetime.rb +21 -0
- data/lib/gloo/convert/string_to_decimal.rb +20 -0
- data/lib/gloo/convert/string_to_integer.rb +20 -0
- data/lib/gloo/core/baseo.rb +7 -7
- data/lib/gloo/core/dictionary.rb +30 -27
- data/lib/gloo/core/error.rb +50 -0
- data/lib/gloo/core/event_manager.rb +17 -23
- data/lib/gloo/core/factory.rb +149 -39
- data/lib/gloo/core/gloo_system.rb +121 -54
- data/lib/gloo/core/heap.rb +15 -13
- data/lib/gloo/core/it.rb +5 -5
- data/lib/gloo/core/literal.rb +7 -7
- data/lib/gloo/core/obj.rb +138 -79
- data/lib/gloo/core/obj_finder.rb +9 -14
- data/lib/gloo/core/op.rb +8 -8
- data/lib/gloo/core/parser.rb +27 -26
- data/lib/gloo/core/pn.rb +68 -52
- data/lib/gloo/core/script.rb +7 -7
- data/lib/gloo/core/tokens.rb +39 -41
- data/lib/gloo/core/verb.rb +30 -19
- data/lib/gloo/exec/dispatch.rb +30 -0
- data/lib/gloo/exec/runner.rb +43 -0
- data/lib/gloo/expr/expression.rb +36 -43
- data/lib/gloo/expr/l_boolean.rb +7 -6
- data/lib/gloo/expr/l_decimal.rb +34 -0
- data/lib/gloo/expr/l_integer.rb +5 -4
- data/lib/gloo/expr/l_string.rb +13 -15
- data/lib/gloo/expr/op_div.rb +5 -5
- data/lib/gloo/expr/op_minus.rb +5 -5
- data/lib/gloo/expr/op_mult.rb +5 -5
- data/lib/gloo/expr/op_plus.rb +7 -7
- data/lib/gloo/objs/basic/alias.rb +111 -0
- data/lib/gloo/objs/basic/boolean.rb +63 -38
- data/lib/gloo/objs/basic/container.rb +71 -12
- data/lib/gloo/objs/basic/decimal.rb +96 -0
- data/lib/gloo/objs/basic/integer.rb +45 -16
- data/lib/gloo/objs/basic/script.rb +62 -38
- data/lib/gloo/objs/basic/string.rb +47 -15
- data/lib/gloo/objs/basic/text.rb +69 -21
- data/lib/gloo/objs/basic/untyped.rb +35 -10
- data/lib/gloo/objs/cli/banner.rb +137 -0
- data/lib/gloo/objs/cli/bar.rb +141 -0
- data/lib/gloo/objs/cli/colorize.rb +54 -24
- data/lib/gloo/objs/cli/confirm.rb +63 -29
- data/lib/gloo/objs/cli/menu.rb +236 -0
- data/lib/gloo/objs/cli/menu_item.rb +128 -0
- data/lib/gloo/objs/cli/pastel.rb +120 -0
- data/lib/gloo/objs/cli/prompt.rb +73 -31
- data/lib/gloo/objs/cli/select.rb +153 -0
- data/lib/gloo/objs/ctrl/each.rb +128 -61
- data/lib/gloo/objs/ctrl/repeat.rb +129 -0
- data/lib/gloo/objs/data/markdown.rb +109 -0
- data/lib/gloo/objs/data/table.rb +168 -0
- data/lib/gloo/objs/dev/git.rb +98 -64
- data/lib/gloo/objs/dt/date.rb +72 -0
- data/lib/gloo/objs/dt/datetime.rb +84 -0
- data/lib/gloo/objs/dt/time.rb +72 -0
- data/lib/gloo/objs/ror/erb.rb +82 -41
- data/lib/gloo/objs/ror/eval.rb +73 -31
- data/lib/gloo/objs/snd/play.rb +71 -0
- data/lib/gloo/objs/snd/say.rb +120 -0
- data/lib/gloo/objs/system/file_handle.rb +129 -48
- data/lib/gloo/objs/system/system.rb +84 -38
- data/lib/gloo/objs/web/http_get.rb +103 -46
- data/lib/gloo/objs/web/http_post.rb +70 -44
- data/lib/gloo/objs/web/json.rb +155 -0
- data/lib/gloo/objs/web/slack.rb +89 -58
- data/lib/gloo/objs/web/teams.rb +88 -53
- data/lib/gloo/objs/web/uri.rb +160 -0
- data/lib/gloo/persist/file_loader.rb +95 -85
- data/lib/gloo/persist/file_saver.rb +12 -12
- data/lib/gloo/persist/file_storage.rb +15 -15
- data/lib/gloo/persist/line_splitter.rb +79 -0
- data/lib/gloo/persist/persist_man.rb +63 -39
- data/lib/gloo/utils/words.rb +2 -2
- data/lib/gloo/verbs/alert.rb +67 -16
- data/lib/gloo/verbs/beep.rb +70 -0
- data/lib/gloo/verbs/cls.rb +67 -0
- data/lib/gloo/verbs/context.rb +61 -21
- data/lib/gloo/verbs/create.rb +52 -21
- data/lib/gloo/verbs/help.rb +186 -27
- data/lib/gloo/verbs/if.rb +55 -21
- data/lib/gloo/verbs/list.rb +55 -24
- data/lib/gloo/verbs/load.rb +47 -12
- data/lib/gloo/verbs/move.rb +128 -0
- data/lib/gloo/verbs/put.rb +90 -34
- data/lib/gloo/verbs/quit.rb +43 -12
- data/lib/gloo/verbs/run.rb +63 -18
- data/lib/gloo/verbs/save.rb +45 -10
- data/lib/gloo/verbs/show.rb +56 -22
- data/lib/gloo/verbs/tell.rb +45 -13
- data/lib/gloo/verbs/unless.rb +56 -21
- data/lib/gloo/verbs/version.rb +42 -12
- data/lib/gloo/verbs/wait.rb +73 -0
- data/lib/run.rb +5 -5
- metadata +90 -12
|
@@ -6,66 +6,81 @@
|
|
|
6
6
|
require 'net/http'
|
|
7
7
|
require 'uri'
|
|
8
8
|
require 'json'
|
|
9
|
+
require 'openssl'
|
|
9
10
|
|
|
10
11
|
module Gloo
|
|
11
12
|
module Objs
|
|
12
13
|
class HttpGet < Gloo::Core::Obj
|
|
13
|
-
|
|
14
|
-
KEYWORD = 'http_get'
|
|
15
|
-
KEYWORD_SHORT = 'get'
|
|
16
|
-
URL = 'uri'
|
|
17
|
-
PARAMS = 'params'
|
|
18
|
-
RESULT = 'result'
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
|
|
15
|
+
KEYWORD = 'http_get'.freeze
|
|
16
|
+
KEYWORD_SHORT = 'get'.freeze
|
|
17
|
+
URL = 'uri'.freeze
|
|
18
|
+
PARAMS = 'params'.freeze
|
|
19
|
+
RESULT = 'result'.freeze
|
|
20
|
+
SKIP_SSL_VERIFY = 'skip_ssl_verify'.freeze
|
|
21
|
+
|
|
22
|
+
#
|
|
21
23
|
# The name of the object type.
|
|
22
|
-
#
|
|
24
|
+
#
|
|
23
25
|
def self.typename
|
|
24
26
|
return KEYWORD
|
|
25
27
|
end
|
|
26
28
|
|
|
27
|
-
#
|
|
29
|
+
#
|
|
28
30
|
# The short name of the object type.
|
|
29
|
-
#
|
|
31
|
+
#
|
|
30
32
|
def self.short_typename
|
|
31
33
|
return KEYWORD_SHORT
|
|
32
34
|
end
|
|
33
|
-
|
|
34
|
-
#
|
|
35
|
+
|
|
36
|
+
#
|
|
35
37
|
# Get the URI from the child object.
|
|
36
38
|
# Returns nil if there is none.
|
|
37
|
-
#
|
|
38
|
-
def
|
|
39
|
+
#
|
|
40
|
+
def uri_value
|
|
39
41
|
uri = find_child URL
|
|
40
42
|
return nil unless uri
|
|
43
|
+
|
|
41
44
|
return uri.value
|
|
42
45
|
end
|
|
43
|
-
|
|
44
|
-
#
|
|
46
|
+
|
|
47
|
+
#
|
|
48
|
+
# Should we skip SSL verification during the request?
|
|
49
|
+
#
|
|
50
|
+
def skip_ssl_verify?
|
|
51
|
+
skip = find_child SKIP_SSL_VERIFY
|
|
52
|
+
return false unless skip
|
|
53
|
+
|
|
54
|
+
return skip.value
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
#
|
|
45
58
|
# Set the result of the API call.
|
|
46
|
-
#
|
|
47
|
-
def
|
|
59
|
+
#
|
|
60
|
+
def update_result( data )
|
|
48
61
|
r = find_child RESULT
|
|
49
62
|
return nil unless r
|
|
63
|
+
|
|
50
64
|
r.set_value data
|
|
51
65
|
end
|
|
52
|
-
|
|
53
|
-
#
|
|
66
|
+
|
|
67
|
+
#
|
|
54
68
|
# Get the URL for the service including all URL params.
|
|
55
|
-
#
|
|
56
|
-
def
|
|
57
|
-
p =
|
|
69
|
+
#
|
|
70
|
+
def full_url_value
|
|
71
|
+
p = ''
|
|
58
72
|
params = find_child PARAMS
|
|
59
73
|
params.children.each do |child|
|
|
60
|
-
p << ( p.empty? ?
|
|
61
|
-
|
|
74
|
+
p << ( p.empty? ? '?' : '&' )
|
|
75
|
+
|
|
76
|
+
child = Gloo::Objs::Alias.resolve_alias( child )
|
|
77
|
+
|
|
62
78
|
# TODO: Quote URL params for safety
|
|
63
79
|
p << "#{child.name}=#{child.value}"
|
|
64
80
|
end
|
|
65
|
-
return "#{
|
|
81
|
+
return "#{uri_value}#{p}"
|
|
66
82
|
end
|
|
67
83
|
|
|
68
|
-
|
|
69
84
|
# ---------------------------------------------------------------------
|
|
70
85
|
# Children
|
|
71
86
|
# ---------------------------------------------------------------------
|
|
@@ -76,53 +91,95 @@ module Gloo
|
|
|
76
91
|
def add_children_on_create?
|
|
77
92
|
return true
|
|
78
93
|
end
|
|
79
|
-
|
|
94
|
+
|
|
80
95
|
# Add children to this object.
|
|
81
|
-
# This is used by containers to add children needed
|
|
96
|
+
# This is used by containers to add children needed
|
|
82
97
|
# for default configurations.
|
|
83
98
|
def add_default_children
|
|
84
99
|
fac = $engine.factory
|
|
85
|
-
fac.create
|
|
86
|
-
|
|
87
|
-
|
|
100
|
+
fac.create( { :name => 'uri',
|
|
101
|
+
:type => 'string',
|
|
102
|
+
:value => 'https://web.site/',
|
|
103
|
+
:parent => self } )
|
|
104
|
+
fac.create( { :name => 'params',
|
|
105
|
+
:type => 'container',
|
|
106
|
+
:value => nil,
|
|
107
|
+
:parent => self } )
|
|
108
|
+
fac.create( { :name => 'result',
|
|
109
|
+
:type => 'string',
|
|
110
|
+
:value => nil,
|
|
111
|
+
:parent => self } )
|
|
88
112
|
end
|
|
89
113
|
|
|
90
|
-
|
|
91
114
|
# ---------------------------------------------------------------------
|
|
92
115
|
# Messages
|
|
93
116
|
# ---------------------------------------------------------------------
|
|
94
117
|
|
|
95
|
-
#
|
|
118
|
+
#
|
|
96
119
|
# Get a list of message names that this object receives.
|
|
97
|
-
#
|
|
120
|
+
#
|
|
98
121
|
def self.messages
|
|
99
|
-
return super + [
|
|
122
|
+
return super + [ 'run' ]
|
|
100
123
|
end
|
|
101
|
-
|
|
124
|
+
|
|
102
125
|
# Post the content to the endpoint.
|
|
103
126
|
def msg_run
|
|
104
|
-
url =
|
|
127
|
+
url = full_url_value
|
|
105
128
|
$log.debug url
|
|
106
|
-
|
|
107
|
-
|
|
129
|
+
r = Gloo::Objs::HttpGet.invoke_request( url, self.skip_ssl_verify? )
|
|
130
|
+
update_result r
|
|
108
131
|
end
|
|
109
|
-
|
|
110
|
-
|
|
132
|
+
|
|
111
133
|
# ---------------------------------------------------------------------
|
|
112
134
|
# Static functions
|
|
113
135
|
# ---------------------------------------------------------------------
|
|
114
136
|
|
|
115
137
|
# Post the content to the endpoint.
|
|
116
|
-
def self.
|
|
138
|
+
def self.invoke_request( url, skip_ssl_verify = false )
|
|
117
139
|
uri = URI( url )
|
|
118
|
-
|
|
119
|
-
|
|
140
|
+
params = { use_ssl: uri.scheme == 'https' }
|
|
141
|
+
|
|
142
|
+
params[ :verify_mode ] = ::OpenSSL::SSL::VERIFY_NONE if skip_ssl_verify
|
|
143
|
+
|
|
144
|
+
Net::HTTP.start( uri.host, uri.port, params ) do |http|
|
|
120
145
|
request = Net::HTTP::Get.new uri
|
|
121
146
|
response = http.request request # Net::HTTPResponse object
|
|
122
147
|
return response.body
|
|
123
148
|
end
|
|
124
149
|
end
|
|
125
150
|
|
|
151
|
+
# ---------------------------------------------------------------------
|
|
152
|
+
# Help
|
|
153
|
+
# ---------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
#
|
|
156
|
+
# Get help for this object type.
|
|
157
|
+
#
|
|
158
|
+
def self.help
|
|
159
|
+
return <<~TEXT
|
|
160
|
+
HTTP_GET OBJECT TYPE
|
|
161
|
+
NAME: http_get
|
|
162
|
+
SHORTCUT: get
|
|
163
|
+
|
|
164
|
+
DESCRIPTION
|
|
165
|
+
Perform an HTTP Get.
|
|
166
|
+
|
|
167
|
+
CHILDREN
|
|
168
|
+
uri - string - 'https://web.site/'
|
|
169
|
+
The URI for the HTTP Get request.
|
|
170
|
+
params - container
|
|
171
|
+
Collection of parameters for the HTTP Get.
|
|
172
|
+
result - string
|
|
173
|
+
The result of the request. Whatever was returned from
|
|
174
|
+
the HTTP Get call.
|
|
175
|
+
skip_ssl_verify - boolean (optional)
|
|
176
|
+
Skip the SSL verification as part of the request.
|
|
177
|
+
|
|
178
|
+
MESSAGES
|
|
179
|
+
run - Run the HTTP Get and update the result.
|
|
180
|
+
TEXT
|
|
181
|
+
end
|
|
182
|
+
|
|
126
183
|
end
|
|
127
184
|
end
|
|
128
185
|
end
|
|
@@ -10,52 +10,53 @@ require 'json'
|
|
|
10
10
|
module Gloo
|
|
11
11
|
module Objs
|
|
12
12
|
class HttpPost < Gloo::Core::Obj
|
|
13
|
-
|
|
14
|
-
KEYWORD = 'http_post'
|
|
15
|
-
KEYWORD_SHORT = 'post'
|
|
16
|
-
URL = 'uri'
|
|
17
|
-
BODY = 'body'
|
|
18
13
|
|
|
19
|
-
|
|
14
|
+
KEYWORD = 'http_post'.freeze
|
|
15
|
+
KEYWORD_SHORT = 'post'.freeze
|
|
16
|
+
URL = 'uri'.freeze
|
|
17
|
+
BODY = 'body'.freeze
|
|
18
|
+
|
|
19
|
+
#
|
|
20
20
|
# The name of the object type.
|
|
21
|
-
#
|
|
21
|
+
#
|
|
22
22
|
def self.typename
|
|
23
23
|
return KEYWORD
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
#
|
|
26
|
+
#
|
|
27
27
|
# The short name of the object type.
|
|
28
|
-
#
|
|
28
|
+
#
|
|
29
29
|
def self.short_typename
|
|
30
30
|
return KEYWORD_SHORT
|
|
31
31
|
end
|
|
32
|
-
|
|
33
|
-
#
|
|
32
|
+
|
|
33
|
+
#
|
|
34
34
|
# Get the URI from the child object.
|
|
35
35
|
# Returns nil if there is none.
|
|
36
|
-
#
|
|
37
|
-
def
|
|
36
|
+
#
|
|
37
|
+
def uri_value
|
|
38
38
|
uri = find_child URL
|
|
39
39
|
return nil unless uri
|
|
40
|
+
|
|
40
41
|
return uri.value
|
|
41
42
|
end
|
|
42
|
-
|
|
43
|
-
#
|
|
44
|
-
# Get all the children of the body container and
|
|
43
|
+
|
|
44
|
+
#
|
|
45
|
+
# Get all the children of the body container and
|
|
45
46
|
# convert to JSON that will be sent in the HTTP body.
|
|
46
|
-
#
|
|
47
|
-
def
|
|
47
|
+
#
|
|
48
|
+
def body_as_json
|
|
48
49
|
h = {}
|
|
49
|
-
|
|
50
|
+
|
|
50
51
|
body = find_child BODY
|
|
51
52
|
body.children.each do |child|
|
|
52
|
-
|
|
53
|
+
child_val = Gloo::Objs::Alias.resolve_alias( child )
|
|
54
|
+
h[ child.name ] = child_val.value
|
|
53
55
|
end
|
|
54
|
-
|
|
56
|
+
|
|
55
57
|
return h.to_json
|
|
56
58
|
end
|
|
57
59
|
|
|
58
|
-
|
|
59
60
|
# ---------------------------------------------------------------------
|
|
60
61
|
# Children
|
|
61
62
|
# ---------------------------------------------------------------------
|
|
@@ -66,50 +67,45 @@ module Gloo
|
|
|
66
67
|
def add_children_on_create?
|
|
67
68
|
return true
|
|
68
69
|
end
|
|
69
|
-
|
|
70
|
+
|
|
70
71
|
# Add children to this object.
|
|
71
|
-
# This is used by containers to add children needed
|
|
72
|
+
# This is used by containers to add children needed
|
|
72
73
|
# for default configurations.
|
|
73
74
|
def add_default_children
|
|
74
75
|
fac = $engine.factory
|
|
75
|
-
fac.
|
|
76
|
-
fac.
|
|
76
|
+
fac.create_string URL, 'https://web.site/', self
|
|
77
|
+
fac.create_can BODY, self
|
|
77
78
|
end
|
|
78
79
|
|
|
79
|
-
|
|
80
80
|
# ---------------------------------------------------------------------
|
|
81
81
|
# Messages
|
|
82
82
|
# ---------------------------------------------------------------------
|
|
83
83
|
|
|
84
|
-
#
|
|
84
|
+
#
|
|
85
85
|
# Get a list of message names that this object receives.
|
|
86
|
-
#
|
|
86
|
+
#
|
|
87
87
|
def self.messages
|
|
88
|
-
return super + [
|
|
88
|
+
return super + [ 'run' ]
|
|
89
89
|
end
|
|
90
|
-
|
|
90
|
+
|
|
91
91
|
# Post the content to the endpoint.
|
|
92
92
|
def msg_run
|
|
93
|
-
uri =
|
|
93
|
+
uri = uri_value
|
|
94
94
|
return unless uri
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
else
|
|
101
|
-
use_ssl = false
|
|
102
|
-
end
|
|
95
|
+
|
|
96
|
+
$log.debug "posting to: #{uri}"
|
|
97
|
+
body = self.body_as_json
|
|
98
|
+
$log.debug "posting body: #{body}"
|
|
99
|
+
use_ssl = uri.downcase.start_with?( 'https' )
|
|
103
100
|
Gloo::Objs::HttpPost.post_json uri, body, use_ssl
|
|
104
101
|
end
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
|
|
107
103
|
# ---------------------------------------------------------------------
|
|
108
104
|
# Static functions
|
|
109
105
|
# ---------------------------------------------------------------------
|
|
110
106
|
|
|
111
107
|
# Post the content to the endpoint.
|
|
112
|
-
def self.post_json url, body, use_ssl=true
|
|
108
|
+
def self.post_json( url, body, use_ssl = true )
|
|
113
109
|
# Structure the request
|
|
114
110
|
uri = URI.parse( url )
|
|
115
111
|
request = Net::HTTP::Post.new( uri.path )
|
|
@@ -119,7 +115,37 @@ module Gloo
|
|
|
119
115
|
n.use_ssl = use_ssl
|
|
120
116
|
|
|
121
117
|
# Send the payload to the endpoint.
|
|
122
|
-
n.start { |http| http.request( request ) }
|
|
118
|
+
result = n.start { |http| http.request( request ) }
|
|
119
|
+
$log.debug result.code
|
|
120
|
+
$log.debug result.message
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# ---------------------------------------------------------------------
|
|
124
|
+
# Help
|
|
125
|
+
# ---------------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
#
|
|
128
|
+
# Get help for this object type.
|
|
129
|
+
#
|
|
130
|
+
def self.help
|
|
131
|
+
return <<~TEXT
|
|
132
|
+
HTTP_POST OBJECT TYPE
|
|
133
|
+
NAME: http_post
|
|
134
|
+
SHORTCUT: post
|
|
135
|
+
|
|
136
|
+
DESCRIPTION
|
|
137
|
+
Perform an HTTP Post.
|
|
138
|
+
|
|
139
|
+
CHILDREN
|
|
140
|
+
uri - string - 'https://web.site/'
|
|
141
|
+
The URI for the HTTP Post.
|
|
142
|
+
body - container
|
|
143
|
+
Collection of parameters for the HTTP Post.
|
|
144
|
+
|
|
145
|
+
MESSAGES
|
|
146
|
+
run - Run the HTTP Post sending the body data to the
|
|
147
|
+
endpoint specified in the URI.
|
|
148
|
+
TEXT
|
|
123
149
|
end
|
|
124
150
|
|
|
125
151
|
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Author:: Eric Crane (mailto:eric.crane@mac.com)
|
|
2
|
+
# Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# JSON data.
|
|
5
|
+
#
|
|
6
|
+
require 'json'
|
|
7
|
+
|
|
8
|
+
module Gloo
|
|
9
|
+
module Objs
|
|
10
|
+
class Json < Gloo::Core::Obj
|
|
11
|
+
|
|
12
|
+
KEYWORD = 'json'.freeze
|
|
13
|
+
KEYWORD_SHORT = 'json'.freeze
|
|
14
|
+
|
|
15
|
+
#
|
|
16
|
+
# The name of the object type.
|
|
17
|
+
#
|
|
18
|
+
def self.typename
|
|
19
|
+
return KEYWORD
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
#
|
|
23
|
+
# The short name of the object type.
|
|
24
|
+
#
|
|
25
|
+
def self.short_typename
|
|
26
|
+
return KEYWORD_SHORT
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
#
|
|
30
|
+
# Set the value with any necessary type conversions.
|
|
31
|
+
#
|
|
32
|
+
def set_value( new_value )
|
|
33
|
+
self.value = new_value.to_s
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# Does this object support multi-line values?
|
|
38
|
+
# Initially only true for scripts.
|
|
39
|
+
#
|
|
40
|
+
def multiline_value?
|
|
41
|
+
return false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
#
|
|
45
|
+
# Get the number of lines of text.
|
|
46
|
+
#
|
|
47
|
+
def line_count
|
|
48
|
+
return value.split( "\n" ).count
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# ---------------------------------------------------------------------
|
|
52
|
+
# Messages
|
|
53
|
+
# ---------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
#
|
|
56
|
+
# Get a list of message names that this object receives.
|
|
57
|
+
#
|
|
58
|
+
def self.messages
|
|
59
|
+
return super + %w[get parse]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
#
|
|
63
|
+
# Get a value from the JSON data.
|
|
64
|
+
# The additional parameter is the path to the value.
|
|
65
|
+
#
|
|
66
|
+
def msg_get
|
|
67
|
+
if @params&.token_count&.positive?
|
|
68
|
+
expr = Gloo::Expr::Expression.new( @params.tokens )
|
|
69
|
+
data = expr.evaluate
|
|
70
|
+
end
|
|
71
|
+
return unless data
|
|
72
|
+
|
|
73
|
+
h = JSON.parse( self.value )
|
|
74
|
+
field = h[ data ]
|
|
75
|
+
$engine.heap.it.set_to field
|
|
76
|
+
return field
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
#
|
|
80
|
+
# Parse the JSON data and put it in objects.
|
|
81
|
+
# The additional parameter is the path to the destination
|
|
82
|
+
# for the parsed objects.
|
|
83
|
+
#
|
|
84
|
+
def msg_parse
|
|
85
|
+
if @params&.token_count&.positive?
|
|
86
|
+
pn = Gloo::Core::Pn.new @params.tokens.first
|
|
87
|
+
unless pn&.exists?
|
|
88
|
+
$engine.err 'Destination path for parsed objects does not exist'
|
|
89
|
+
return
|
|
90
|
+
end
|
|
91
|
+
else
|
|
92
|
+
$engine.err 'Destination path for parsed objects is required'
|
|
93
|
+
return
|
|
94
|
+
end
|
|
95
|
+
parent = pn.resolve
|
|
96
|
+
|
|
97
|
+
json = JSON.parse( self.value )
|
|
98
|
+
self.handle_json( json, parent )
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
#
|
|
102
|
+
# Handle JSON, creating objects and setting values.
|
|
103
|
+
# Note that this is a recursive function.
|
|
104
|
+
#
|
|
105
|
+
def handle_json( json, parent )
|
|
106
|
+
if json.class == Hash
|
|
107
|
+
json.each do |k, v|
|
|
108
|
+
if v.class == Array
|
|
109
|
+
o = parent.find_add_child( k, 'can' )
|
|
110
|
+
handle_json( v, o )
|
|
111
|
+
else
|
|
112
|
+
o = parent.find_add_child( k, 'untyped' )
|
|
113
|
+
o.set_value v
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
elsif json.class == Array
|
|
117
|
+
json.each_with_index do |o, index|
|
|
118
|
+
child = parent.find_add_child( index.to_s, 'can' )
|
|
119
|
+
handle_json( o, child )
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# ---------------------------------------------------------------------
|
|
125
|
+
# Help
|
|
126
|
+
# ---------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
#
|
|
129
|
+
# Get help for this object type.
|
|
130
|
+
#
|
|
131
|
+
def self.help
|
|
132
|
+
return <<~TEXT
|
|
133
|
+
JSON OBJECT TYPE
|
|
134
|
+
NAME: json
|
|
135
|
+
SHORTCUT: json
|
|
136
|
+
|
|
137
|
+
DESCRIPTION
|
|
138
|
+
JSON data in a text string.
|
|
139
|
+
|
|
140
|
+
CHILDREN
|
|
141
|
+
None
|
|
142
|
+
|
|
143
|
+
MESSAGES
|
|
144
|
+
get - get a value from the JSON data
|
|
145
|
+
Example: tell myjson to get ("title")
|
|
146
|
+
The additional parameter is the path to the value.
|
|
147
|
+
parse - parse the JSON data and put values in the
|
|
148
|
+
object specified by the additional parameter
|
|
149
|
+
Example: tell myjson to parse ("path.to.objects")
|
|
150
|
+
TEXT
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|