gloo-lang 0.9.2 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/gloo-lang.gemspec +1 -0
  4. data/lib/VERSION +1 -1
  5. data/lib/dependencies.rb +2 -2
  6. data/lib/gloo-lang.rb +1 -5
  7. data/lib/gloo_lang/app/args.rb +111 -0
  8. data/lib/gloo_lang/app/engine.rb +200 -0
  9. data/lib/gloo_lang/app/help.rb +117 -0
  10. data/lib/gloo_lang/app/info.rb +34 -0
  11. data/lib/gloo_lang/app/log.rb +81 -0
  12. data/lib/gloo_lang/app/mode.rb +27 -0
  13. data/lib/gloo_lang/app/platform.rb +75 -0
  14. data/lib/gloo_lang/app/settings.rb +185 -0
  15. data/lib/gloo_lang/convert/converter.rb +35 -0
  16. data/lib/gloo_lang/convert/string_to_datetime.rb +21 -0
  17. data/lib/gloo_lang/convert/string_to_decimal.rb +20 -0
  18. data/lib/gloo_lang/convert/string_to_integer.rb +20 -0
  19. data/lib/gloo_lang/core/baseo.rb +30 -0
  20. data/lib/gloo_lang/core/dictionary.rb +181 -0
  21. data/lib/gloo_lang/core/error.rb +61 -0
  22. data/lib/gloo_lang/core/event_manager.rb +44 -0
  23. data/lib/gloo_lang/core/factory.rb +210 -0
  24. data/lib/gloo_lang/core/gloo_system.rb +266 -0
  25. data/lib/gloo_lang/core/heap.rb +52 -0
  26. data/lib/gloo_lang/core/here.rb +36 -0
  27. data/lib/gloo_lang/core/it.rb +36 -0
  28. data/lib/gloo_lang/core/literal.rb +30 -0
  29. data/lib/gloo_lang/core/obj.rb +303 -0
  30. data/lib/gloo_lang/core/obj_finder.rb +30 -0
  31. data/lib/gloo_lang/core/op.rb +40 -0
  32. data/lib/gloo_lang/core/parser.rb +59 -0
  33. data/lib/gloo_lang/core/pn.rb +188 -0
  34. data/lib/gloo_lang/core/tokens.rb +165 -0
  35. data/lib/gloo_lang/core/verb.rb +86 -0
  36. data/lib/gloo_lang/exec/action.rb +48 -0
  37. data/lib/gloo_lang/exec/dispatch.rb +40 -0
  38. data/lib/gloo_lang/exec/exec_env.rb +74 -0
  39. data/lib/gloo_lang/exec/runner.rb +45 -0
  40. data/lib/gloo_lang/exec/script.rb +49 -0
  41. data/lib/gloo_lang/exec/stack.rb +78 -0
  42. data/lib/gloo_lang/expr/expression.rb +118 -0
  43. data/lib/gloo_lang/expr/l_boolean.rb +36 -0
  44. data/lib/gloo_lang/expr/l_decimal.rb +39 -0
  45. data/lib/gloo_lang/expr/l_integer.rb +37 -0
  46. data/lib/gloo_lang/expr/l_string.rb +58 -0
  47. data/lib/gloo_lang/expr/op_div.rb +22 -0
  48. data/lib/gloo_lang/expr/op_minus.rb +22 -0
  49. data/lib/gloo_lang/expr/op_mult.rb +22 -0
  50. data/lib/gloo_lang/expr/op_plus.rb +24 -0
  51. data/lib/gloo_lang/help/app/application.txt +22 -0
  52. data/lib/gloo_lang/help/app/configuration.txt +7 -0
  53. data/lib/gloo_lang/help/app/default_help.txt +14 -0
  54. data/lib/gloo_lang/help/app/logging.txt +16 -0
  55. data/lib/gloo_lang/help/core/color.txt +31 -0
  56. data/lib/gloo_lang/help/core/error.txt +33 -0
  57. data/lib/gloo_lang/help/core/events.txt +21 -0
  58. data/lib/gloo_lang/help/core/gloo_system.txt +57 -0
  59. data/lib/gloo_lang/help/core/here.txt +30 -0
  60. data/lib/gloo_lang/help/core/it.txt +23 -0
  61. data/lib/gloo_lang/help/core/ops.txt +16 -0
  62. data/lib/gloo_lang/help/core/pathname.txt +29 -0
  63. data/lib/gloo_lang/help/objs/basic/alias.txt +36 -0
  64. data/lib/gloo_lang/help/objs/basic/boolean.txt +28 -0
  65. data/lib/gloo_lang/help/objs/basic/container.txt +33 -0
  66. data/lib/gloo_lang/help/objs/basic/decimal.txt +28 -0
  67. data/lib/gloo_lang/help/objs/basic/integer.txt +27 -0
  68. data/lib/gloo_lang/help/objs/basic/script.txt +29 -0
  69. data/lib/gloo_lang/help/objs/basic/string.txt +28 -0
  70. data/lib/gloo_lang/help/objs/basic/text.txt +27 -0
  71. data/lib/gloo_lang/help/objs/basic/untyped.txt +22 -0
  72. data/lib/gloo_lang/help/objs/cli/banner.txt +49 -0
  73. data/lib/gloo_lang/help/objs/cli/bar.txt +37 -0
  74. data/lib/gloo_lang/help/objs/cli/colorize.txt +33 -0
  75. data/lib/gloo_lang/help/objs/cli/confirm.txt +26 -0
  76. data/lib/gloo_lang/help/objs/cli/menu.txt +44 -0
  77. data/lib/gloo_lang/help/objs/cli/menu_item.txt +26 -0
  78. data/lib/gloo_lang/help/objs/cli/pastel.txt +43 -0
  79. data/lib/gloo_lang/help/objs/cli/prompt.txt +27 -0
  80. data/lib/gloo_lang/help/objs/cli/select.txt +34 -0
  81. data/lib/gloo_lang/help/objs/ctrl/each.txt +48 -0
  82. data/lib/gloo_lang/help/objs/ctrl/repeat.txt +38 -0
  83. data/lib/gloo_lang/help/objs/data/markdown.txt +25 -0
  84. data/lib/gloo_lang/help/objs/data/mysql.txt +40 -0
  85. data/lib/gloo_lang/help/objs/data/query.txt +37 -0
  86. data/lib/gloo_lang/help/objs/data/sqlite.txt +26 -0
  87. data/lib/gloo_lang/help/objs/data/table.txt +46 -0
  88. data/lib/gloo_lang/help/objs/dev/git_repo.txt +36 -0
  89. data/lib/gloo_lang/help/objs/dev/stats.txt +36 -0
  90. data/lib/gloo_lang/help/objs/dt/date.txt +23 -0
  91. data/lib/gloo_lang/help/objs/dt/datetime.txt +24 -0
  92. data/lib/gloo_lang/help/objs/dt/time.txt +23 -0
  93. data/lib/gloo_lang/help/objs/ror/erb.txt +37 -0
  94. data/lib/gloo_lang/help/objs/ror/eval.txt +24 -0
  95. data/lib/gloo_lang/help/objs/snd/play.txt +23 -0
  96. data/lib/gloo_lang/help/objs/snd/say.txt +28 -0
  97. data/lib/gloo_lang/help/objs/system/file.txt +48 -0
  98. data/lib/gloo_lang/help/objs/system/ssh_exec.txt +30 -0
  99. data/lib/gloo_lang/help/objs/system/system.txt +32 -0
  100. data/lib/gloo_lang/help/objs/web/http_get.txt +35 -0
  101. data/lib/gloo_lang/help/objs/web/http_post.txt +34 -0
  102. data/lib/gloo_lang/help/objs/web/json.txt +34 -0
  103. data/lib/gloo_lang/help/objs/web/slack.txt +33 -0
  104. data/lib/gloo_lang/help/objs/web/teams.txt +30 -0
  105. data/lib/gloo_lang/help/objs/web/uri.txt +38 -0
  106. data/lib/gloo_lang/help/verbs/alert.txt +33 -0
  107. data/lib/gloo_lang/help/verbs/beep.txt +25 -0
  108. data/lib/gloo_lang/help/verbs/cls.txt +24 -0
  109. data/lib/gloo_lang/help/verbs/context.txt +43 -0
  110. data/lib/gloo_lang/help/verbs/create.txt +33 -0
  111. data/lib/gloo_lang/help/verbs/execute.txt +27 -0
  112. data/lib/gloo_lang/help/verbs/help.txt +34 -0
  113. data/lib/gloo_lang/help/verbs/if.txt +37 -0
  114. data/lib/gloo_lang/help/verbs/list.txt +34 -0
  115. data/lib/gloo_lang/help/verbs/load.txt +38 -0
  116. data/lib/gloo_lang/help/verbs/move.txt +42 -0
  117. data/lib/gloo_lang/help/verbs/put.txt +38 -0
  118. data/lib/gloo_lang/help/verbs/quit.txt +25 -0
  119. data/lib/gloo_lang/help/verbs/run.txt +41 -0
  120. data/lib/gloo_lang/help/verbs/save.txt +26 -0
  121. data/lib/gloo_lang/help/verbs/show.txt +30 -0
  122. data/lib/gloo_lang/help/verbs/tell.txt +34 -0
  123. data/lib/gloo_lang/help/verbs/unless.txt +38 -0
  124. data/lib/gloo_lang/help/verbs/version.txt +32 -0
  125. data/lib/gloo_lang/help/verbs/wait.txt +29 -0
  126. data/lib/gloo_lang/objs/basic/alias.rb +78 -0
  127. data/lib/gloo_lang/objs/basic/boolean.rb +120 -0
  128. data/lib/gloo_lang/objs/basic/container.rb +65 -0
  129. data/lib/gloo_lang/objs/basic/decimal.rb +76 -0
  130. data/lib/gloo_lang/objs/basic/integer.rb +73 -0
  131. data/lib/gloo_lang/objs/basic/script.rb +99 -0
  132. data/lib/gloo_lang/objs/basic/string.rb +77 -0
  133. data/lib/gloo_lang/objs/basic/text.rb +79 -0
  134. data/lib/gloo_lang/objs/basic/untyped.rb +41 -0
  135. data/lib/gloo_lang/objs/ctrl/each.rb +279 -0
  136. data/lib/gloo_lang/objs/ctrl/repeat.rb +108 -0
  137. data/lib/gloo_lang/objs/data/markdown.rb +79 -0
  138. data/lib/gloo_lang/objs/data/mysql.rb +192 -0
  139. data/lib/gloo_lang/objs/data/query.rb +176 -0
  140. data/lib/gloo_lang/objs/data/sqlite.rb +159 -0
  141. data/lib/gloo_lang/objs/data/table.rb +112 -0
  142. data/lib/gloo_lang/objs/dt/date.rb +50 -0
  143. data/lib/gloo_lang/objs/dt/datetime.rb +62 -0
  144. data/lib/gloo_lang/objs/dt/time.rb +50 -0
  145. data/lib/gloo_lang/objs/ror/erb.rb +116 -0
  146. data/lib/gloo_lang/objs/ror/eval.rb +107 -0
  147. data/lib/gloo_lang/objs/web/http_get.rb +159 -0
  148. data/lib/gloo_lang/objs/web/http_post.rb +183 -0
  149. data/lib/gloo_lang/objs/web/json.rb +135 -0
  150. data/lib/gloo_lang/objs/web/slack.rb +130 -0
  151. data/lib/gloo_lang/objs/web/teams.rb +117 -0
  152. data/lib/gloo_lang/objs/web/uri.rb +148 -0
  153. data/lib/gloo_lang/persist/file_loader.rb +191 -0
  154. data/lib/gloo_lang/persist/file_saver.rb +49 -0
  155. data/lib/gloo_lang/persist/file_storage.rb +45 -0
  156. data/lib/gloo_lang/persist/line_splitter.rb +81 -0
  157. data/lib/gloo_lang/persist/persist_man.rb +120 -0
  158. data/lib/gloo_lang/utils/format.rb +21 -0
  159. data/lib/gloo_lang/utils/stats.rb +205 -0
  160. data/lib/gloo_lang/utils/words.rb +19 -0
  161. data/lib/gloo_lang/verbs/context.rb +62 -0
  162. data/lib/gloo_lang/verbs/create.rb +68 -0
  163. data/lib/gloo_lang/verbs/execute.rb +56 -0
  164. data/lib/gloo_lang/verbs/help.rb +264 -0
  165. data/lib/gloo_lang/verbs/if.rb +92 -0
  166. data/lib/gloo_lang/verbs/list.rb +98 -0
  167. data/lib/gloo_lang/verbs/load.rb +45 -0
  168. data/lib/gloo_lang/verbs/move.rb +89 -0
  169. data/lib/gloo_lang/verbs/put.rb +94 -0
  170. data/lib/gloo_lang/verbs/quit.rb +40 -0
  171. data/lib/gloo_lang/verbs/run.rb +75 -0
  172. data/lib/gloo_lang/verbs/save.rb +39 -0
  173. data/lib/gloo_lang/verbs/show.rb +64 -0
  174. data/lib/gloo_lang/verbs/tell.rb +79 -0
  175. data/lib/gloo_lang/verbs/unless.rb +92 -0
  176. data/lib/gloo_lang/verbs/version.rb +37 -0
  177. data/lib/gloo_lang/verbs/wait.rb +42 -0
  178. metadata +192 -2
@@ -0,0 +1,116 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # An object that evaluate a ruby statement.
5
+ #
6
+ require 'erb'
7
+
8
+ module GlooLang
9
+ module Objs
10
+ class Erb < GlooLang::Core::Obj
11
+
12
+ KEYWORD = 'erb'.freeze
13
+ KEYWORD_SHORT = 'erb'.freeze
14
+ TEMPLATE = 'template'.freeze
15
+ PARAMS = 'params'.freeze
16
+ RESULT = 'result'.freeze
17
+
18
+ #
19
+ # The name of the object type.
20
+ #
21
+ def self.typename
22
+ return KEYWORD
23
+ end
24
+
25
+ #
26
+ # The short name of the object type.
27
+ #
28
+ def self.short_typename
29
+ return KEYWORD_SHORT
30
+ end
31
+
32
+ #
33
+ # Get the ERB template.
34
+ #
35
+ def template_value
36
+ tmpl = find_child TEMPLATE
37
+ return nil unless tmpl
38
+
39
+ return tmpl.value
40
+ end
41
+
42
+ #
43
+ # Set the result of the ERB template conversion.
44
+ #
45
+ def set_result( data )
46
+ r = find_child RESULT
47
+ return nil unless r
48
+
49
+ r.set_value data
50
+ end
51
+
52
+ #
53
+ # Get a hash with parameters for the ERB render.
54
+ #
55
+ def param_hash
56
+ h = {}
57
+
58
+ body = find_child PARAMS
59
+ body.children.each do |child|
60
+ child = GlooLang::Objs::Alias.resolve_alias( child )
61
+ h[ child.name ] = child.value
62
+ end
63
+
64
+ return h
65
+ end
66
+
67
+ # ---------------------------------------------------------------------
68
+ # Children
69
+ # ---------------------------------------------------------------------
70
+
71
+ #
72
+ # Does this object have children to add when an object
73
+ # is created in interactive mode?
74
+ # This does not apply during obj load, etc.
75
+ #
76
+ def add_children_on_create?
77
+ return true
78
+ end
79
+
80
+ #
81
+ # Add children to this object.
82
+ # This is used by containers to add children needed
83
+ # for default configurations.
84
+ #
85
+ def add_default_children
86
+ fac = $engine.factory
87
+ fac.create_text TEMPLATE, '', self
88
+ fac.create_can PARAMS, self
89
+ fac.create_text RESULT, '', self
90
+ end
91
+
92
+ # ---------------------------------------------------------------------
93
+ # Messages
94
+ # ---------------------------------------------------------------------
95
+
96
+ #
97
+ # Get a list of message names that this object receives.
98
+ #
99
+ def self.messages
100
+ return super + [ 'run' ]
101
+ end
102
+
103
+ #
104
+ # Run the ERB template conversion.
105
+ #
106
+ def msg_run
107
+ tmpl = template_value
108
+ return unless tmpl
109
+
110
+ render = ERB.new( tmpl )
111
+ set_result render.result_with_hash( param_hash )
112
+ end
113
+
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,107 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # An object that evaluate a ruby statement.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class Eval < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'eval'.freeze
12
+ KEYWORD_SHORT = 'ruby'.freeze
13
+ CMD = 'command'.freeze
14
+ DEFAULT_CMD = '1+2'.freeze
15
+ RESULT = 'result'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the URI from the child object.
33
+ # Returns nil if there is none.
34
+ #
35
+ def cmd_value
36
+ cmd = find_child CMD
37
+ return nil unless cmd
38
+
39
+ return cmd.value
40
+ end
41
+
42
+ #
43
+ # Set the result of the system call.
44
+ #
45
+ def set_result( data )
46
+ r = find_child RESULT
47
+ return nil unless r
48
+
49
+ r.set_value data
50
+ end
51
+
52
+ # ---------------------------------------------------------------------
53
+ # Children
54
+ # ---------------------------------------------------------------------
55
+
56
+ #
57
+ # Does this object have children to add when an object
58
+ # is created in interactive mode?
59
+ # This does not apply during obj load, etc.
60
+ #
61
+ def add_children_on_create?
62
+ return true
63
+ end
64
+
65
+ #
66
+ # Add children to this object.
67
+ # This is used by containers to add children needed
68
+ # for default configurations.
69
+ #
70
+ def add_default_children
71
+ fac = $engine.factory
72
+ fac.create_string CMD, DEFAULT_CMD, self
73
+ fac.create_string RESULT, nil, self
74
+ end
75
+
76
+ # ---------------------------------------------------------------------
77
+ # Messages
78
+ # ---------------------------------------------------------------------
79
+
80
+ #
81
+ # Get a list of message names that this object receives.
82
+ #
83
+ def self.messages
84
+ return super + [ 'run' ]
85
+ end
86
+
87
+ #
88
+ # Run the command and evaluate the expression.
89
+ #
90
+ def msg_run
91
+ cmd = cmd_value
92
+ return unless cmd
93
+
94
+ begin
95
+ # rubocop:disable Security/Eval
96
+ result = eval cmd
97
+ # rubocop:enable Security/Eval
98
+ set_result result
99
+ $engine.heap.it.set_to result
100
+ rescue => e
101
+ $engine.err e.message
102
+ end
103
+ end
104
+
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,159 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # An object that can post JSON to a URI.
5
+ #
6
+ require 'net/http'
7
+ require 'uri'
8
+ require 'json'
9
+ require 'openssl'
10
+
11
+ module GlooLang
12
+ module Objs
13
+ class HttpGet < GlooLang::Core::Obj
14
+
15
+ KEYWORD = 'http_get'.freeze
16
+ KEYWORD_SHORT = 'get'.freeze
17
+ URL = 'uri'.freeze
18
+ DEFAULT_URL = 'https://web.site/'.freeze
19
+ PARAMS = 'params'.freeze
20
+ RESULT = 'result'.freeze
21
+ SKIP_SSL_VERIFY = 'skip_ssl_verify'.freeze
22
+
23
+ #
24
+ # The name of the object type.
25
+ #
26
+ def self.typename
27
+ return KEYWORD
28
+ end
29
+
30
+ #
31
+ # The short name of the object type.
32
+ #
33
+ def self.short_typename
34
+ return KEYWORD_SHORT
35
+ end
36
+
37
+ # ---------------------------------------------------------------------
38
+ # Children
39
+ # ---------------------------------------------------------------------
40
+
41
+ #
42
+ # Does this object have children to add when an object
43
+ # is created in interactive mode?
44
+ # This does not apply during obj load, etc.
45
+ #
46
+ def add_children_on_create?
47
+ return true
48
+ end
49
+
50
+ #
51
+ # Add children to this object.
52
+ # This is used by containers to add children needed
53
+ # for default configurations.
54
+ #
55
+ def add_default_children
56
+ fac = $engine.factory
57
+ fac.create_string URL, DEFAULT_URL, self
58
+ fac.create_can PARAMS, self
59
+ fac.create_string RESULT, nil, self
60
+ end
61
+
62
+ # ---------------------------------------------------------------------
63
+ # Messages
64
+ # ---------------------------------------------------------------------
65
+
66
+ #
67
+ # Get a list of message names that this object receives.
68
+ #
69
+ def self.messages
70
+ return super + [ 'run' ]
71
+ end
72
+
73
+ #
74
+ # Post the content to the endpoint.
75
+ #
76
+ def msg_run
77
+ url = full_url_value
78
+ $log.debug url
79
+ r = GlooLang::Objs::HttpGet.invoke_request( url, skip_ssl_verify? )
80
+ update_result r
81
+ end
82
+
83
+ # ---------------------------------------------------------------------
84
+ # Static functions
85
+ # ---------------------------------------------------------------------
86
+
87
+ #
88
+ # Post the content to the endpoint.
89
+ #
90
+ def self.invoke_request( url, skip_ssl_verify = false )
91
+ uri = URI( url )
92
+ params = { use_ssl: uri.scheme == 'https' }
93
+
94
+ params[ :verify_mode ] = ::OpenSSL::SSL::VERIFY_NONE if skip_ssl_verify
95
+
96
+ Net::HTTP.start( uri.host, uri.port, params ) do |http|
97
+ request = Net::HTTP::Get.new uri
98
+ response = http.request request # Net::HTTPResponse object
99
+ return response.body
100
+ end
101
+ end
102
+
103
+ # ---------------------------------------------------------------------
104
+ # Private functions
105
+ # ---------------------------------------------------------------------
106
+
107
+ private
108
+
109
+ #
110
+ # Get the URI from the child object.
111
+ # Returns nil if there is none.
112
+ #
113
+ def uri_value
114
+ uri = find_child URL
115
+ return nil unless uri
116
+
117
+ return uri.value
118
+ end
119
+
120
+ #
121
+ # Should we skip SSL verification during the request?
122
+ #
123
+ def skip_ssl_verify?
124
+ skip = find_child SKIP_SSL_VERIFY
125
+ return false unless skip
126
+
127
+ return skip.value
128
+ end
129
+
130
+ #
131
+ # Set the result of the API call.
132
+ #
133
+ def update_result( data )
134
+ r = find_child RESULT
135
+ return nil unless r
136
+
137
+ r.set_value data
138
+ end
139
+
140
+ #
141
+ # Get the URL for the service including all URL params.
142
+ #
143
+ def full_url_value
144
+ p = ''
145
+ params = find_child PARAMS
146
+ params.children.each do |child|
147
+ p << ( p.empty? ? '?' : '&' )
148
+
149
+ child = GlooLang::Objs::Alias.resolve_alias( child )
150
+
151
+ # TODO: Quote URL params for safety
152
+ p << "#{child.name}=#{child.value}"
153
+ end
154
+ return "#{uri_value}#{p}"
155
+ end
156
+
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,183 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # An object that can post JSON to a URI.
5
+ #
6
+ require 'net/http'
7
+ require 'uri'
8
+ require 'json'
9
+
10
+ module GlooLang
11
+ module Objs
12
+ class HttpPost < GlooLang::Core::Obj
13
+
14
+ KEYWORD = 'http_post'.freeze
15
+ KEYWORD_SHORT = 'post'.freeze
16
+ URL = 'uri'.freeze
17
+ BODY = 'body'.freeze
18
+ RESULT = 'result'.freeze
19
+ SKIP_SSL_VERIFY = 'skip_ssl_verify'.freeze
20
+
21
+ #
22
+ # The name of the object type.
23
+ #
24
+ def self.typename
25
+ return KEYWORD
26
+ end
27
+
28
+ #
29
+ # The short name of the object type.
30
+ #
31
+ def self.short_typename
32
+ return KEYWORD_SHORT
33
+ end
34
+
35
+ #
36
+ # Get the URI from the child object.
37
+ # Returns nil if there is none.
38
+ #
39
+ def uri_value
40
+ uri = find_child URL
41
+ return nil unless uri
42
+
43
+ return uri.value
44
+ end
45
+
46
+ #
47
+ # Get all the children of the body container and
48
+ # convert to JSON that will be sent in the HTTP body.
49
+ #
50
+ def body_as_json
51
+ h = {}
52
+
53
+ body = find_child BODY
54
+ body.children.each do |child|
55
+ child_val = GlooLang::Objs::Alias.resolve_alias( child )
56
+ h[ child.name ] = child_val.value
57
+ end
58
+
59
+ return h.to_json
60
+ end
61
+
62
+ #
63
+ # Set the result of the API call.
64
+ #
65
+ def update_result( data )
66
+ r = find_child RESULT
67
+ return nil unless r
68
+
69
+ r.set_value data
70
+ end
71
+
72
+ # ---------------------------------------------------------------------
73
+ # Children
74
+ # ---------------------------------------------------------------------
75
+
76
+ #
77
+ # Does this object have children to add when an object
78
+ # is created in interactive mode?
79
+ # This does not apply during obj load, etc.
80
+ #
81
+ def add_children_on_create?
82
+ return true
83
+ end
84
+
85
+ #
86
+ # Add children to this object.
87
+ # This is used by containers to add children needed
88
+ # for default configurations.
89
+ #
90
+ def add_default_children
91
+ fac = $engine.factory
92
+ fac.create_string URL, 'https://web.site/', self
93
+ fac.create_can BODY, self
94
+ end
95
+
96
+ # ---------------------------------------------------------------------
97
+ # Messages
98
+ # ---------------------------------------------------------------------
99
+
100
+ #
101
+ # Get a list of message names that this object receives.
102
+ #
103
+ def self.messages
104
+ return super + [ 'run' ]
105
+ end
106
+
107
+ #
108
+ # Post the content to the endpoint.
109
+ #
110
+ def msg_run
111
+ uri = uri_value
112
+ return unless uri
113
+
114
+ $log.debug "posting to: #{uri}"
115
+ body = self.body_as_json
116
+ $log.debug "posting body: #{body}"
117
+ data = GlooLang::Objs::HttpPost.post_json( uri, body, skip_ssl_verify? )
118
+ self.update_result data
119
+ end
120
+
121
+ # ---------------------------------------------------------------------
122
+ # Static functions
123
+ # ---------------------------------------------------------------------
124
+
125
+ #
126
+ # Post the content to the endpoint.
127
+ #
128
+ def self.post_json( url, body, skip_ssl_verify = false )
129
+ uri = URI( url )
130
+ params = { use_ssl: uri.scheme == 'https' }
131
+ params[ :verify_mode ] = ::OpenSSL::SSL::VERIFY_NONE if skip_ssl_verify
132
+
133
+ Net::HTTP.start( uri.host, uri.port, params ) do |http|
134
+ request = Net::HTTP::Post.new uri
135
+ request.content_type = 'application/json'
136
+ request.body = body
137
+
138
+ result = http.request request # Net::HTTPResponse object
139
+ $log.debug result.code
140
+ $log.debug result.message
141
+ return result.body
142
+ end
143
+ end
144
+
145
+ # #
146
+ # # Post the content to the endpoint.
147
+ # #
148
+ # def self.post_json_1( url, body, use_ssl = true )
149
+ # # Structure the request
150
+ # uri = URI.parse( url )
151
+ #
152
+ # request = Net::HTTP::Post.new( uri.path )
153
+ # request.content_type = 'application/json'
154
+ # request.body = body
155
+ # n = Net::HTTP.new( uri.host, uri.port )
156
+ # n.use_ssl = use_ssl
157
+ #
158
+ # # Send the payload to the endpoint.
159
+ # result = n.start { |http| http.request( request ) }
160
+ # $log.debug result.code
161
+ # $log.debug result.message
162
+ # return result.body
163
+ # end
164
+
165
+ # ---------------------------------------------------------------------
166
+ # Private functions
167
+ # ---------------------------------------------------------------------
168
+
169
+ private
170
+
171
+ #
172
+ # Should we skip SSL verification during the request?
173
+ #
174
+ def skip_ssl_verify?
175
+ skip = find_child SKIP_SSL_VERIFY
176
+ return false unless skip
177
+
178
+ return skip.value
179
+ end
180
+
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,135 @@
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 GlooLang
9
+ module Objs
10
+ class Json < GlooLang::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 pretty]
60
+ end
61
+
62
+ #
63
+ # Make the JSON pretty.
64
+ #
65
+ def msg_pretty
66
+ pretty = JSON.pretty_generate( self.value )
67
+ puts pretty
68
+ set_value pretty
69
+ end
70
+
71
+ #
72
+ # Get a value from the JSON data.
73
+ # The additional parameter is the path to the value.
74
+ #
75
+ def msg_get
76
+ if @params&.token_count&.positive?
77
+ expr = GlooLang::Expr::Expression.new( @params.tokens )
78
+ data = expr.evaluate
79
+ end
80
+ return unless data
81
+
82
+ h = JSON.parse( self.value )
83
+ field = h[ data ]
84
+ $engine.heap.it.set_to field
85
+ return field
86
+ end
87
+
88
+ #
89
+ # Parse the JSON data and put it in objects.
90
+ # The additional parameter is the path to the destination
91
+ # for the parsed objects.
92
+ #
93
+ def msg_parse
94
+ if @params&.token_count&.positive?
95
+ pn = GlooLang::Core::Pn.new @params.tokens.first
96
+ unless pn&.exists?
97
+ $engine.err 'Destination path for parsed objects does not exist'
98
+ return
99
+ end
100
+ else
101
+ $engine.err 'Destination path for parsed objects is required'
102
+ return
103
+ end
104
+ parent = pn.resolve
105
+
106
+ json = JSON.parse( self.value )
107
+ self.handle_json( json, parent )
108
+ end
109
+
110
+ #
111
+ # Handle JSON, creating objects and setting values.
112
+ # Note that this is a recursive function.
113
+ #
114
+ def handle_json( json, parent )
115
+ if json.class == Hash
116
+ json.each do |k, v|
117
+ if v.class == Array
118
+ o = parent.find_add_child( k, 'can' )
119
+ handle_json( v, o )
120
+ else
121
+ o = parent.find_add_child( k, 'untyped' )
122
+ o.set_value v
123
+ end
124
+ end
125
+ elsif json.class == Array
126
+ json.each_with_index do |o, index|
127
+ child = parent.find_add_child( index.to_s, 'can' )
128
+ handle_json( o, child )
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+ end
135
+ end