Class: Elasticshell::Shell
- Inherits:
-
Object
- Object
- Elasticshell::Shell
- Includes:
- HasVerb
- Defined in:
- lib/elasticshell/shell.rb
Constant Summary
Constants included from HasVerb
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#client ⇒ Object
Returns the value of attribute client.
-
#input ⇒ Object
Returns the value of attribute input.
-
#input_stream ⇒ Object
Returns the value of attribute input_stream.
-
#line ⇒ Object
Returns the value of attribute line.
-
#only ⇒ Object
Returns the value of attribute only.
-
#output ⇒ Object
Returns the value of attribute output.
-
#ruby_code ⇒ Object
Returns the value of attribute ruby_code.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #command ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #die ⇒ Object
- #eval_line(line) ⇒ Object
- #format_only_part_of(obj) ⇒ Object
- #format_output(obj, ignore_only = false) ⇒ Object
-
#initialize(options = {}) ⇒ Shell
constructor
A new instance of Shell.
- #int ⇒ Object
- #interactive? ⇒ Boolean
- #log_requests? ⇒ Boolean
- #loop ⇒ Object
- #not_pretty! ⇒ Object
- #path ⇒ Object
- #pretty! ⇒ Object
- #pretty? ⇒ Boolean
- #print(obj, ignore_only = false) ⇒ Object
- #prompt ⇒ Object
- #run ⇒ Object
- #scope_from_path(path) ⇒ Object
- #setup ⇒ Object
Methods included from HasVerb
Constructor Details
#initialize(options = {}) ⇒ Shell
Returns a new instance of Shell.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/elasticshell/shell.rb', line 85 def initialize ={} @interactive = false self.state = :init self.client = Client.new() self.cache = {} @initial_servers = ([:servers] || []) self.verb = ([:verb] || 'GET') self.scope = scope_from_path([:scope] || '/') self.only = [:only] self.ruby_code = [:eval] self.input_stream = ([:input] || $stdin) self.output = ([:output] || $stdout) self.line = 0 @log_requests = ([:log_requests] == false ? false : true) pretty! if [:pretty] end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def cache @cache end |
#client ⇒ Object
Returns the value of attribute client.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def client @client end |
#input ⇒ Object
Returns the value of attribute input.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def input @input end |
#input_stream ⇒ Object
Returns the value of attribute input_stream.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def input_stream @input_stream end |
#line ⇒ Object
Returns the value of attribute line.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def line @line end |
#only ⇒ Object
Returns the value of attribute only.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def only @only end |
#output ⇒ Object
Returns the value of attribute output.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def output @output end |
#ruby_code ⇒ Object
Returns the value of attribute ruby_code.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def ruby_code @ruby_code end |
#scope ⇒ Object
Returns the value of attribute scope.
67 68 69 |
# File 'lib/elasticshell/shell.rb', line 67 def scope @scope end |
#state ⇒ Object
Returns the value of attribute state.
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def state @state end |
Instance Method Details
#command ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/elasticshell/shell.rb', line 179 def command matching_command_class_name = Commands::PRIORITY.detect do |command_class_name| Commands.const_get(command_class_name).matches?(input) end # We should never hit the following ArgumentError as there # exists a catch-all command: Unknown raise ArgumentError.new("Could not parse command: '#{input}'") unless matching_command_class_name Commands.const_get(matching_command_class_name).new(self, input) end |
#connect ⇒ Object
154 155 156 |
# File 'lib/elasticshell/shell.rb', line 154 def connect eval_line("connect #{@initial_servers.join(' ')}") end |
#connected? ⇒ Boolean
81 82 83 |
# File 'lib/elasticshell/shell.rb', line 81 def connected? client.connected? end |
#die ⇒ Object
231 232 233 |
# File 'lib/elasticshell/shell.rb', line 231 def die raise ShellError.new("C-d...quitting") end |
#eval_line(line) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/elasticshell/shell.rb', line 166 def eval_line line begin self.state = :eval self.input = line.strip command.evaluate! rescue ::Elasticshell::Error => e Elasticshell.error e. end self.state = :read self.line += 1 self end |
#format_only_part_of(obj) ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'lib/elasticshell/shell.rb', line 212 def format_only_part_of obj only_parts = self.only.to_s.split('.') obj_to_print = obj while obj_to_print && only_parts.size > 0 this_only = only_parts.shift obj_to_print = (obj_to_print || {})[this_only] end format_output(obj_to_print, true) end |
#format_output(obj, ignore_only = false) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/elasticshell/shell.rb', line 195 def format_output obj, ignore_only=false case when self.only == true && !ignore_only format_output(obj, true) when self.only && !ignore_only format_only_part_of(obj) when obj.nil? nil when obj.is_a?(String) || obj.is_a?(Fixnum) obj when pretty? JSON.pretty_generate(obj) else obj.to_json end end |
#int ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/elasticshell/shell.rb', line 222 def int case self.state when :read $stdout.write("^C\n#{prompt}") else $stdout.write("^C...aborted\n#{prompt}") end end |
#interactive? ⇒ Boolean
128 129 130 |
# File 'lib/elasticshell/shell.rb', line 128 def interactive? @interactive end |
#log_requests? ⇒ Boolean
235 236 237 |
# File 'lib/elasticshell/shell.rb', line 235 def log_requests? @log_requests end |
#loop ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/elasticshell/shell.rb', line 158 def loop self.state = :read while line = Readline.readline(prompt, true) eval_line(line) end die end |
#not_pretty! ⇒ Object
124 125 126 |
# File 'lib/elasticshell/shell.rb', line 124 def not_pretty! @pretty = false end |
#path ⇒ Object
77 78 79 |
# File 'lib/elasticshell/shell.rb', line 77 def path scope.path end |
#pretty! ⇒ Object
120 121 122 |
# File 'lib/elasticshell/shell.rb', line 120 def pretty! @pretty = true end |
#pretty? ⇒ Boolean
116 117 118 |
# File 'lib/elasticshell/shell.rb', line 116 def pretty? @pretty end |
#print(obj, ignore_only = false) ⇒ Object
191 192 193 |
# File 'lib/elasticshell/shell.rb', line 191 def print obj, ignore_only=false self.output.puts(format_output(obj, ignore_only)) end |
#prompt ⇒ Object
110 111 112 113 114 |
# File 'lib/elasticshell/shell.rb', line 110 def prompt verb_string = Elasticshell.format((verb.to_s =~ /^(?:G|H)/i ? :passive_http_verb_format : :active_http_verb_format), "%v", verb.to_s.upcase) scope_string = Elasticshell.format((scope.exists? ? :existing_scope_format : :missing_scope_format), "%s", scope.path) Elasticshell.format((pretty? ? :pretty_prompt_format : :prompt_format), ["%s", "%v"], [scope_string, verb_string]) end |
#run ⇒ Object
148 149 150 151 152 |
# File 'lib/elasticshell/shell.rb', line 148 def run setup connect loop end |
#scope_from_path(path) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/elasticshell/shell.rb', line 102 def scope_from_path path if cache[path] cache[path] else cache[path] = Scopes.from_path(path, :client => self.client) end end |
#setup ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/elasticshell/shell.rb', line 132 def setup trap("INT") do int end Readline.completer_word_break_characters = " \t\n\"\\'`$><=|&{(" print <<EOF Elasticshell v. #{Elasticshell.version} Type "help" for contextual help. EOF @interactive = true self.line = 1 end |