Class: ChefZero::RestRequest

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RestRequest.



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

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.



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

def env
  @env
end

#rest_base_prefixObject

Returns the value of attribute rest_base_prefix.



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

def rest_base_prefix
  @rest_base_prefix
end

Class Method Details

.rfc2396_parserObject



84
85
86
# File 'lib/chef_zero/rest_request.rb', line 84

def self.rfc2396_parser
  @parser ||= URI::RFC2396_Parser.new
end

Instance Method Details

#api_v0?Boolean

Returns:

  • (Boolean)


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

def api_v0?
  api_version == 0
end

#api_versionObject



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

def api_version
  Integer(@env["HTTP_X_OPS_SERVER_API_VERSION"] || 0)
end

#base_uriObject



14
15
16
17
18
19
20
21
22
# File 'lib/chef_zero/rest_request.rb', line 14

def base_uri
  # Load balancer awareness
  if env["HTTP_X_FORWARDED_PROTO"]
    scheme = env["HTTP_X_FORWARDED_PROTO"]
  else
    scheme = env["rack.url_scheme"]
  end
  @base_uri ||= "#{scheme}://#{env["HTTP_HOST"]}#{env["SCRIPT_NAME"]}"
end

#base_uri=(value) ⇒ Object



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

def base_uri=(value)
  @base_uri = value
end

#bodyObject



56
57
58
# File 'lib/chef_zero/rest_request.rb', line 56

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

#body=(body) ⇒ Object



52
53
54
# File 'lib/chef_zero/rest_request.rb', line 52

def body=(body)
  @body = body
end

#methodObject



40
41
42
# File 'lib/chef_zero/rest_request.rb', line 40

def method
  @env["REQUEST_METHOD"]
end

#query_paramsObject



60
61
62
63
64
65
66
67
68
# File 'lib/chef_zero/rest_request.rb', line 60

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

#requestorObject



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

def requestor
  @env["HTTP_X_OPS_USERID"]
end

#rest_pathObject



44
45
46
# File 'lib/chef_zero/rest_request.rb', line 44

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

#rest_path=(rest_path) ⇒ Object



48
49
50
# File 'lib/chef_zero/rest_request.rb', line 48

def rest_path=(rest_path)
  @rest_path = rest_path
end

#to_sObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef_zero/rest_request.rb', line 70

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" unless body.end_with?("\n")
    result << "--- END #{method} BODY ---"
  end
  result
end