Class: Elasticshell::Commands::Request

Inherits:
Elasticshell::Command show all
Extended by:
RecognizesVerb
Defined in:
lib/elasticshell/commands/request.rb,
lib/elasticshell/commands/request_parser.rb

Defined Under Namespace

Classes: Parser

Constant Summary

Constants included from RecognizesVerb

RecognizesVerb::VERB_RE

Instance Attribute Summary collapse

Attributes inherited from Elasticshell::Command

#input, #shell

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RecognizesVerb

canonicalize_verb, is_http_verb?, verb_re

Methods inherited from Elasticshell::Command

#be_connected!, #initialize

Constructor Details

This class inherits a constructor from Elasticshell::Command

Instance Attribute Details

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/elasticshell/commands/request.rb', line 7

def request
  @request
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/elasticshell/commands/request.rb', line 7

def response
  @response
end

Class Method Details

.matches?(input) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/elasticshell/commands/request.rb', line 9

def self.matches? input
  input =~ Regexp.new("^(#{verb_re}\s+)?.", true)
end

Instance Method Details

#evaluate!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elasticshell/commands/request.rb', line 61

def evaluate!
  be_connected!
  parse!
  perform_request!
  case
  when pipe_to_irb?
    irb!
  when pipe_to_ruby?
    ruby!
  when pipe_to_file?
    pipe_to_file!
  else
    shell.print(response)
  end
end

#irb!Object



42
43
44
45
# File 'lib/elasticshell/commands/request.rb', line 42

def irb!
  require 'ripl'
  Ripl.start(:binding => binding)
end

#output_filenameObject



29
30
31
32
# File 'lib/elasticshell/commands/request.rb', line 29

def output_filename
  input =~ /\s>\s*(.+)$/
  $1
end

#parse!Object



13
14
15
# File 'lib/elasticshell/commands/request.rb', line 13

def parse!
  self.request = Parser.new(self).request
end

#perform_request!Object



17
18
19
# File 'lib/elasticshell/commands/request.rb', line 17

def perform_request!
  self.response = shell.client.request(request[:verb], {:op => request[:path] }, request[:query_options].merge(:log => shell.log_requests?), request[:body])
end

#pipe?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/elasticshell/commands/request.rb', line 21

def pipe?
  pipe_to_ruby? || pipe_to_irb? || pipe_to_file?
end

#pipe_to_file!Object



34
35
36
# File 'lib/elasticshell/commands/request.rb', line 34

def pipe_to_file!
  File.open(output_filename, 'w') { |f| f.puts response.to_s }
end

#pipe_to_file?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/elasticshell/commands/request.rb', line 25

def pipe_to_file?
  input =~ /\s>\s*.+$/
end

#pipe_to_irb?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/elasticshell/commands/request.rb', line 38

def pipe_to_irb?
  input =~ /\s\|\s*$/
end

#pipe_to_ruby?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/elasticshell/commands/request.rb', line 47

def pipe_to_ruby?
  input =~ /\s\|\s*\S+/
end

#ruby!Object



57
58
59
# File 'lib/elasticshell/commands/request.rb', line 57

def ruby!
  eval(ruby_code, binding)
end

#ruby_codeObject



51
52
53
54
55
# File 'lib/elasticshell/commands/request.rb', line 51

def ruby_code
  return unless pipe?
  input =~ /\s\|(.*)$/
  $1.to_s
end