Class: ChefZero::RestRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_zero/rest_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, rest_base_prefix = []) ⇒ RestRequest


5
6
7
8
# File 'lib/chef_zero/rest_request.rb', line 5

def initialize(env, rest_base_prefix = [])
  @env = env
  @rest_base_prefix = rest_base_prefix
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env


10
11
12
# File 'lib/chef_zero/rest_request.rb', line 10

def env
  @env
end

#rest_base_prefixObject

Returns the value of attribute rest_base_prefix


11
12
13
# File 'lib/chef_zero/rest_request.rb', line 11

def rest_base_prefix
  @rest_base_prefix
end

Instance Method Details

#base_uriObject


13
14
15
# File 'lib/chef_zero/rest_request.rb', line 13

def base_uri
  @base_uri ||= "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}"
end

#base_uri=(value) ⇒ Object


17
18
19
# File 'lib/chef_zero/rest_request.rb', line 17

def base_uri=(value)
  @base_uri = value
end

#bodyObject


37
38
39
# File 'lib/chef_zero/rest_request.rb', line 37

def body
  @body ||= env['rack.input'].read
end

#body=(body) ⇒ Object


33
34
35
# File 'lib/chef_zero/rest_request.rb', line 33

def body=(body)
  @body = body
end

#methodObject


25
26
27
# File 'lib/chef_zero/rest_request.rb', line 25

def method
  @env['REQUEST_METHOD']
end

#query_paramsObject


41
42
43
44
45
46
47
48
49
# File 'lib/chef_zero/rest_request.rb', line 41

def query_params
  @query_params ||= begin
    params = Rack::Request.new(env).GET
    params.keys.each do |key|
      params[key] = URI.unescape(params[key])
    end
    params
  end
end

#requestorObject


21
22
23
# File 'lib/chef_zero/rest_request.rb', line 21

def requestor
  @env['HTTP_X_OPS_USERID']
end

#rest_pathObject


29
30
31
# File 'lib/chef_zero/rest_request.rb', line 29

def rest_path
  @rest_path ||= rest_base_prefix + env['PATH_INFO'].split('/').select { |part| part != "" }
end

#to_sObject


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef_zero/rest_request.rb', line 51

def to_s
  result = "#{method} #{rest_path.join('/')}"
  if query_params.size > 0
    result << "?#{query_params.map { |k,v| "#{k}=#{v}" }.join('&') }"
  end
  if body.chomp != ''
    result << "\n--- #{method} BODY ---\n"
    result << body
    result << "\n" if !body.end_with?("\n")
    result << "--- END #{method} BODY ---"
  end
  result
end