Class: VMCAdmin::Curl

Inherits:
VMC::CLI
  • Object
show all
Defined in:
lib/admin-vmc-plugin/curl.rb

Instance Method Summary collapse

Instance Method Details

#curlObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/admin-vmc-plugin/curl.rb', line 19

def curl
  mode = input[:mode].upcase
  path = input[:path]
  body = input[:body]

  headers = {}
  input[:headers].each do |h|
    k, v = h.split(/\s*:\s*/, 2)
    headers[k.downcase] = v
  end

  content = headers["content-type"]
  accept = headers["accept"]

  content ||= :json if body
  accept ||= :json unless %w(DELETE HEAD).include?(mode)

  req, res =
    client.base.request_raw(
      mode,
      remove_leading_slash(path),
      :headers => headers,
      :accept => accept,
      :payload => body,
      :content => body && content)

  if [:json, "application/json"].include? accept
    puts MultiJson.dump(res, :pretty => true)
  else
    puts res
  end
end

#preconditionObject



5
6
7
# File 'lib/admin-vmc-plugin/curl.rb', line 5

def precondition
  check_target
end

#remove_leading_slash(path) ⇒ Object



52
53
54
# File 'lib/admin-vmc-plugin/curl.rb', line 52

def remove_leading_slash(path)
  path.sub(%r{^/}, '')
end