Class: Elasticshell::Commands::Request::Parser

Inherits:
Object
  • Object
show all
Includes:
RecognizesVerb
Defined in:
lib/elasticshell/commands/request_parser.rb

Constant Summary

Constants included from RecognizesVerb

RecognizesVerb::VERB_RE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RecognizesVerb

#canonicalize_verb, #is_http_verb?, #verb_re

Constructor Details

#initialize(command) ⇒ Parser

Returns a new instance of Parser.



11
12
13
# File 'lib/elasticshell/commands/request_parser.rb', line 11

def initialize(command)
  self.command = command
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#query_optionsObject

Returns the value of attribute query_options.



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

def query_options
  @query_options
end

#rawObject

Returns the value of attribute raw.



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

def raw
  @raw
end

#raw_bodyObject

Returns the value of attribute raw_body.



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

def raw_body
  @raw_body
end

#raw_pathObject

Returns the value of attribute raw_path.



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

def raw_path
  @raw_path
end

#request_stringObject

Returns the value of attribute request_string.



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

def request_string
  @request_string
end

#verbObject

Returns the value of attribute verb.



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

def verb
  @verb
end

Instance Method Details

#construct_body!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/elasticshell/commands/request_parser.rb', line 54

def construct_body!
  self.body = case
  when raw_body.nil?
    ''
  when raw_body == '-'
    Elasticshell.info("Reading request body from STDIN.  Press `C-d' to terminate input...")
    command.shell.input_stream.gets(nil)
  when File.exist?(raw_body)
    File.read(raw_body)
  else
    raw_body
  end
end

#interpret_path!Object



44
45
46
47
48
49
50
51
52
# File 'lib/elasticshell/commands/request_parser.rb', line 44

def interpret_path!
  relative_path, query_string = raw_path.split('?')
  self.path = File.expand_path(relative_path, command.shell.scope.path)
  
  self.query_options = {}
  URI.decode_www_form(query_string || '').each do |k, v|
    self.query_options[k] = v
  end
end

#parse!Object



20
21
22
23
24
25
26
# File 'lib/elasticshell/commands/request_parser.rb', line 20

def parse!
  strip_redirect!
  split_verb_and_request!
  split_path_and_body!
  interpret_path!
  construct_body!
end

#requestObject



15
16
17
18
# File 'lib/elasticshell/commands/request_parser.rb', line 15

def request
  parse!
  { :verb => verb, :path => path, :query_options => query_options, :body => body }
end

#split_path_and_body!Object



40
41
42
# File 'lib/elasticshell/commands/request_parser.rb', line 40

def split_path_and_body!
  self.raw_path, self.raw_body = request_string.split(/ /, 2)
end

#split_verb_and_request!Object



32
33
34
35
36
37
38
# File 'lib/elasticshell/commands/request_parser.rb', line 32

def split_verb_and_request!
  if raw =~ Regexp.new("^(#{verb_re})\s+(.+)$", true)
    self.verb, self.request_string = canonicalize_verb($1), $2
  else
    self.verb, self.request_string = command.shell.verb, raw
  end
end

#strip_redirect!Object



28
29
30
# File 'lib/elasticshell/commands/request_parser.rb', line 28

def strip_redirect!
  self.raw = command.shell.input.gsub(/ (?:\||>).*$/,'')
end