Class: Elasticshell::Shell

Inherits:
Object
  • Object
show all
Includes:
HasVerb
Defined in:
lib/elasticshell/shell.rb

Constant Summary

Constants included from HasVerb

HasVerb::VERBS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasVerb

#verb, #verb=

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 options={}
  @interactive = false
  self.state  = :init
  self.client = Client.new(options)
  self.cache  = {}
  @initial_servers = (options[:servers] || [])
  self.verb   = (options[:verb] || 'GET')
  self.scope  = scope_from_path(options[:scope] || '/')
  self.only   = options[:only]
  self.ruby_code = options[:eval]
  self.input_stream  = (options[:input]  || $stdin)
  self.output = (options[:output] || $stdout)
  self.line   = 0
  @log_requests = (options[:log_requests] == false ? false : true)
  pretty! if options[:pretty]
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def cache
  @cache
end

#clientObject

Returns the value of attribute client.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def client
  @client
end

#inputObject

Returns the value of attribute input.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def input
  @input
end

#input_streamObject

Returns the value of attribute input_stream.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def input_stream
  @input_stream
end

#lineObject

Returns the value of attribute line.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def line
  @line
end

#onlyObject

Returns the value of attribute only.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def only
  @only
end

#outputObject

Returns the value of attribute output.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def output
  @output
end

#ruby_codeObject

Returns the value of attribute ruby_code.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def ruby_code
  @ruby_code
end

#scopeObject

Returns the value of attribute scope.



67
68
69
# File 'lib/elasticshell/shell.rb', line 67

def scope
  @scope
end

#stateObject

Returns the value of attribute state.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def state
  @state
end

Class Method Details

.formatted_verb(verb) ⇒ Object



116
117
118
# File 'lib/elasticshell/shell.rb', line 116

def self.formatted_verb verb
  Elasticshell.format((verb.to_s =~ /^(?:G|H)/i ? :passive_http_verb_format : :active_http_verb_format), "%v", verb.to_s.upcase)
end

Instance Method Details

#commandObject

Raises:



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/elasticshell/shell.rb', line 183

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

#connectObject



158
159
160
# File 'lib/elasticshell/shell.rb', line 158

def connect
  eval_line("connect #{@initial_servers.join(' ')}")
end

#connected?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/elasticshell/shell.rb', line 81

def connected?
  client.connected?
end

#dieObject

Raises:



235
236
237
# File 'lib/elasticshell/shell.rb', line 235

def die
  raise ShellError.new("C-d...quitting")
end

#eval_line(line) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/elasticshell/shell.rb', line 170

def eval_line line
  begin
    self.state = :eval
    self.input = line.strip
    command.evaluate!
  rescue ::Elasticshell::Error => e
    Elasticshell.error e.message
  end
  self.state = :read
  self.line += 1
  self
end

#format_only_part_of(obj) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/elasticshell/shell.rb', line 216

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/elasticshell/shell.rb', line 199

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

#intObject



226
227
228
229
230
231
232
233
# File 'lib/elasticshell/shell.rb', line 226

def int
  case self.state
  when :read
    $stdout.write("^C\n#{prompt}")
  else
    $stdout.write("^C...aborted\n#{prompt}")
  end
end

#interactive?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/elasticshell/shell.rb', line 132

def interactive?
  @interactive
end

#log_requests?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/elasticshell/shell.rb', line 239

def log_requests?
  @log_requests
end

#loopObject



162
163
164
165
166
167
168
# File 'lib/elasticshell/shell.rb', line 162

def loop
  self.state = :read
  while line = Readline.readline(prompt, true)
    eval_line(line)
  end
  die
end

#not_pretty!Object



128
129
130
# File 'lib/elasticshell/shell.rb', line 128

def not_pretty!
  @pretty = false
end

#pathObject



77
78
79
# File 'lib/elasticshell/shell.rb', line 77

def path
  scope.path
end

#pretty!Object



124
125
126
# File 'lib/elasticshell/shell.rb', line 124

def pretty!
  @pretty = true
end

#pretty?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/elasticshell/shell.rb', line 120

def pretty?
  @pretty
end


195
196
197
# File 'lib/elasticshell/shell.rb', line 195

def print obj, ignore_only=false
  self.output.puts(format_output(obj, ignore_only))
end

#promptObject



110
111
112
113
114
# File 'lib/elasticshell/shell.rb', line 110

def prompt
  verb_string  = self.class.formatted_verb(verb)
  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

#runObject



152
153
154
155
156
# File 'lib/elasticshell/shell.rb', line 152

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

#setupObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/elasticshell/shell.rb', line 136

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