Class: Morpheus::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/morpheus/rest_client.rb

Overview

A wrapper around rest_client so we can more easily deal with passing options (like turning on/off SSL verification)

Class Method Summary collapse

Class Method Details

.enable_ssl_verification=(verify) ⇒ Object



68
69
70
# File 'lib/morpheus/rest_client.rb', line 68

def enable_ssl_verification=(verify)
  @@ssl_verification_enabled = verify
end

.execute(options) ⇒ Object



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
51
52
53
54
# File 'lib/morpheus/rest_client.rb', line 21

def execute(options)
  default_opts = {}
  # only read requests get default 30 second timeout.
  if options[:method] == :get || options[:method] == :head
    default_opts[:timeout] = 30
  end
  opts = default_opts.merge(options)
  unless ssl_verification_enabled?
    opts[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE
  end

  opts[:headers] ||= {}
  opts[:headers][:user_agent] ||= self.user_agent

  # serialize params ourselves, this way we get arrays without the [] suffix
  params = nil
  if opts[:params] && !opts[:params].empty?
    params = opts.delete(:params)
  elsif opts[:headers] && opts[:headers][:params]
    # params inside headers for restclient reasons..
    params = opts[:headers].delete(:params)
  elsif opts[:query] && !opts[:query].empty?
    params = opts.delete(:query)
  end
  query_string = params
  if query_string.respond_to?(:map)
    query_string = URI.encode_www_form(query_string)
  end
  if query_string && !query_string.empty?
    opts[:url] = "#{opts[:url]}?#{query_string}"
  end

  ::RestClient::Request.execute opts
end

.post(url, payload) ⇒ Object



56
57
58
# File 'lib/morpheus/rest_client.rb', line 56

def post(url, payload)
  execute url: url, payload: payload, method: :post
end

.ssl_verification_enabled?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/morpheus/rest_client.rb', line 60

def ssl_verification_enabled?
  begin
    @@ssl_verification_enabled.nil? ? true : @@ssl_verification_enabled
  rescue
    @@ssl_verification_enabled = true
  end
end

.user_agentObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/morpheus/rest_client.rb', line 10

def user_agent
  if !@user_agent
    begin
      @user_agent = "morpheus-cli #{Morpheus::Cli::VERSION}"
      @user_agent = "#{@user_agent} (#{::RestClient::Platform.architecture}) #{::RestClient::Platform.ruby_agent_version}"
    rescue => e
    end
  end
  return @user_agent
end