Class: Elasticshell::Commands::Request
Defined Under Namespace
Classes: Parser
Constant Summary
RecognizesVerb::VERB_RE
Instance Attribute Summary collapse
#input, #shell
Class Method Summary
collapse
Instance Method Summary
collapse
canonicalize_verb, is_http_verb?, verb_re
#be_connected!, #initialize
Instance Attribute Details
#request ⇒ Object
Returns the value of attribute request.
7
8
9
|
# File 'lib/elasticshell/commands/request.rb', line 7
def request
@request
end
|
#response ⇒ Object
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
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_filename ⇒ Object
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
|
17
18
19
|
# File 'lib/elasticshell/commands/request.rb', line 17
def perform_request!
self.response = shell.client.request(request[:verb], {:op => request[:path].gsub(/^\//,'') }, request[:query_options].merge(:log => shell.log_requests?), request[:body])
end
|
#pipe? ⇒ 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
25
26
27
|
# File 'lib/elasticshell/commands/request.rb', line 25
def pipe_to_file?
input =~ /\s>\s*.+$/
end
|
#pipe_to_irb? ⇒ Boolean
38
39
40
|
# File 'lib/elasticshell/commands/request.rb', line 38
def pipe_to_irb?
input =~ /\s\|\s*$/
end
|
#pipe_to_ruby? ⇒ 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_code ⇒ Object
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
|