Class: Elasticshell::Shell
- Inherits:
-
Object
- Object
- Elasticshell::Shell
- Defined in:
- lib/elasticshell/shell.rb
Constant Summary collapse
- VERBS =
%w[GET POST PUT DELETE]
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#command ⇒ Object
Returns the value of attribute command.
-
#input ⇒ Object
Returns the value of attribute input.
-
#only ⇒ Object
Returns the value of attribute only.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#state ⇒ Object
Returns the value of attribute state.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
- #clear_line ⇒ Object
- #command_and_query_and_body(command) ⇒ Object
- #die ⇒ Object
- #eval_line(line) ⇒ Object
-
#initialize(options = {}) ⇒ Shell
constructor
A new instance of Shell.
- #int ⇒ Object
- #loop ⇒ Object
- #not_pretty! ⇒ Object
- #pretty! ⇒ Object
- #pretty? ⇒ Boolean
- #print(obj, ignore_only = false) ⇒ Object
- #prompt ⇒ Object
- #prompt_prettiness_indicator ⇒ Object
- #prompt_scope_color ⇒ Object
- #prompt_verb_color ⇒ Object
- #request(verb, params = {}) ⇒ Object
- #run ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Shell
Returns a new instance of Shell.
32 33 34 35 36 37 38 39 |
# File 'lib/elasticshell/shell.rb', line 32 def initialize ={} self.state = :init self.client = Client.new() self.verb = ([:verb] || 'GET') self.scope = Scopes.from_path(([:scope] || '/'), :client => self.client) self.only = [:only] pretty! if [:pretty] end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
14 15 16 |
# File 'lib/elasticshell/shell.rb', line 14 def client @client end |
#command ⇒ Object
Returns the value of attribute command.
14 15 16 |
# File 'lib/elasticshell/shell.rb', line 14 def command @command end |
#input ⇒ Object
Returns the value of attribute input.
14 15 16 |
# File 'lib/elasticshell/shell.rb', line 14 def input @input end |
#only ⇒ Object
Returns the value of attribute only.
14 15 16 |
# File 'lib/elasticshell/shell.rb', line 14 def only @only end |
#scope ⇒ Object
Returns the value of attribute scope.
22 23 24 |
# File 'lib/elasticshell/shell.rb', line 22 def scope @scope end |
#state ⇒ Object
Returns the value of attribute state.
14 15 16 |
# File 'lib/elasticshell/shell.rb', line 14 def state @state end |
#verb ⇒ Object
Returns the value of attribute verb.
16 17 18 |
# File 'lib/elasticshell/shell.rb', line 16 def verb @verb end |
Instance Method Details
#clear_line ⇒ Object
143 144 145 146 147 |
# File 'lib/elasticshell/shell.rb', line 143 def clear_line while Readline.point > 0 $stdin.write("\b \b") end end |
#command_and_query_and_body(command) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/elasticshell/shell.rb', line 155 def command_and_query_and_body command parts = command.split c_and_q = parts[0] c, q = c_and_q.split('?') o = {} URI.decode_www_form(q || '').each do |k, v| o[k] = v end path = parts[1] case when path && File.exist?(path) && File.readable?(path) b = File.read(path) when path && path == '-' b = $stdin.gets(nil) when path b = path else # b = (command.split(' ', 2).last || '') b = '' end [c, o, b] end |
#die ⇒ Object
149 150 151 152 153 |
# File 'lib/elasticshell/shell.rb', line 149 def die puts "C-d" print("C-d...quitting") exit() end |
#eval_line(line) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/elasticshell/shell.rb', line 94 def eval_line line begin self.input = line.strip self.command = Command.new(self, input) self.state = :eval self.command.evaluate! rescue ::Elasticshell::Error => e $stderr.puts e. end self.state = :read end |
#int ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/elasticshell/shell.rb', line 134 def int case self.state when :read $stdout.write("^C\n#{prompt}") else $stdout.write("^C...aborted\n#{prompt}") end end |
#loop ⇒ Object
87 88 89 90 91 92 |
# File 'lib/elasticshell/shell.rb', line 87 def loop self.state = :read while line = Readline.readline(prompt, true) eval_line(line) end end |
#not_pretty! ⇒ Object
65 66 67 |
# File 'lib/elasticshell/shell.rb', line 65 def not_pretty! @pretty = false end |
#pretty! ⇒ Object
61 62 63 |
# File 'lib/elasticshell/shell.rb', line 61 def pretty! @pretty = true end |
#pretty? ⇒ Boolean
57 58 59 |
# File 'lib/elasticshell/shell.rb', line 57 def pretty? @pretty end |
#print(obj, ignore_only = false) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/elasticshell/shell.rb', line 106 def print obj, ignore_only=false if self.only && !ignore_only if self.only == true print(obj, true) else 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 print(obj_to_print, true) end else case obj when nil when String, Fixnum puts obj else if pretty? puts JSON.pretty_generate(obj) else puts obj.to_json end end end end |
#prompt ⇒ Object
41 42 43 |
# File 'lib/elasticshell/shell.rb', line 41 def prompt "\e[1m#{prompt_verb_color}#{verb} #{prompt_scope_color}#{scope.path} #{prompt_prettiness_indicator} \e[0m" end |
#prompt_prettiness_indicator ⇒ Object
53 54 55 |
# File 'lib/elasticshell/shell.rb', line 53 def prompt_prettiness_indicator pretty? ? '$' : '>' end |
#prompt_scope_color ⇒ Object
45 46 47 |
# File 'lib/elasticshell/shell.rb', line 45 def prompt_scope_color scope.exists? ? "\e[32m" : "\e[33m" end |
#prompt_verb_color ⇒ Object
49 50 51 |
# File 'lib/elasticshell/shell.rb', line 49 def prompt_verb_color verb == "GET" ? "\e[34m" : "\e[31m" end |
#request(verb, params = {}) ⇒ Object
181 182 183 184 185 |
# File 'lib/elasticshell/shell.rb', line 181 def request verb, params={} c, o, b = command_and_query_and_body(input) body = (params.delete(:body) || b || '') print(client.request(verb, params.merge(:op => c), o, b)) end |
#run ⇒ Object
82 83 84 85 |
# File 'lib/elasticshell/shell.rb', line 82 def run setup loop end |
#setup ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/elasticshell/shell.rb', line 69 def setup trap("INT") do int end Readline.completer_word_break_characters = " \t\n\"\\'`$><=|&{(" puts "Elasticshell v. \#{Elasticshell.version}\nType \"help\" for contextual help.\n" end |