Class: CF::UAA::CurlCli

Inherits:
CommonCli show all
Includes:
Http
Defined in:
lib/uaa/cli/curl.rb

Instance Method Summary collapse

Methods inherited from CommonCli

#askd, #auth_header, #clientid, #clientname, #clientsecret, #complain, #debug?, #handle_request, #passcode, #scim_common_list, #scim_get_helper, #scim_get_object, #scim_get_user_object, #scim_request, #trace?, #update_target_info, #username, #userpwd, #verified_pwd

Methods inherited from Topic

#add_command, #ask, #ask_pwd, commands, define_option, desc, #gripe, #help_col_start, #initialize, #opt_help, #opt_strs, option_defs, #opts, #pp, #print_tree, #say, #say_cmd_helper, #say_command_help, #say_commands, #say_definition, #say_help, #terminal_columns, topic

Constructor Details

This class inherits a constructor from CF::UAA::Topic

Instance Method Details

#make_request(uri, options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/uaa/cli/curl.rb', line 63

def make_request(uri, options)
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
    if options[:insecure]
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    elsif options[:cacert]
      http.ca_file = File.expand_path(options[:cacert])
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
  end
  request_class = Net::HTTP.const_get("#{options[:request][0]}#{options[:request][1..-1].downcase}")
  req = request_class.new(uri.request_uri)
  req["Authorization"] = "Bearer #{Config.value(:access_token)}"
  Array(options[:header]).each do |h|
    key, value = h.split(":")
    req[key] = value
  end
  http.request(req, options[:data])
end

#parse_uri(path) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/uaa/cli/curl.rb', line 43

def parse_uri(path)
  uri = URI.parse(path)
  unless uri.host
    uri = URI.parse("#{Config.target}#{path}")
  end
  uri
end


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/uaa/cli/curl.rb', line 51

def print_request(request, uri, data, header, bodyonly)
  say_it("#{request} #{uri.to_s}", bodyonly)
  say_it("REQUEST BODY: \"#{data}\"", bodyonly) if data
  if header
    say_it("REQUEST HEADERS:", bodyonly)
    Array(header).each do |h|
      say_it("  #{h}", bodyonly)
    end
  end
  say_it("", bodyonly)
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/uaa/cli/curl.rb', line 84

def print_response(response, bodyonly)
  say_it("#{response.code} #{response.message}", bodyonly)
  say_it("RESPONSE HEADERS:", bodyonly)
  response.each_capitalized do |key, value|
    say_it("  #{key}: #{value}", bodyonly)
  end

  say_it("RESPONSE BODY:", bodyonly)
  if !response['Content-Type'].nil? && response['Content-Type'].include?('application/json')
    parsed = JSON.parse(response.body)
    formatted = JSON.pretty_generate(parsed)
    say(formatted)
  else
    say(response.body)
  end
end

#say_it(text, bodyonly) ⇒ Object



101
102
103
104
105
# File 'lib/uaa/cli/curl.rb', line 101

def say_it(text, bodyonly)
  if !bodyonly
    say text
  end
end