Method: Gzr::Session#build_connection_hash

Defined in:
lib/gzr/modules/session.rb

#build_connection_hash(api_version = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gzr/modules/session.rb', line 59

def build_connection_hash(api_version=nil)
  conn_hash = Hash.new
  conn_hash[:api_endpoint] = "http#{@options[:ssl] ? "s" : ""}://#{@options[:host]}:#{@options[:port]}/api/#{api_version||@current_version||""}"
  if @options[:http_proxy]
    conn_hash[:connection_options] ||= {}
    conn_hash[:connection_options][:proxy] = {
      :uri => @options[:http_proxy]
    }
  end
  if @options[:ssl]
    conn_hash[:connection_options] ||= {}
    if @options[:verify_ssl] then
      conn_hash[:connection_options][:ssl] = {
        :verify => true,
        :verify_mode => (OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT)
      }
    else
      conn_hash[:connection_options][:ssl] = {
        :verify => false,
        :verify_mode => (OpenSSL::SSL::VERIFY_NONE)
      }
    end
  end
  if @options[:timeout]
    conn_hash[:connection_options] ||= {}
    conn_hash[:connection_options][:request] = {
      :timeout => @options[:timeout]
    }
  end
  conn_hash[:user_agent] = "Gazer #{Gzr::VERSION}"
  if @options[:client_id] then
    conn_hash[:client_id] = @options[:client_id]
    if @options[:client_secret] then
      conn_hash[:client_secret] = @options[:client_secret]
    else
      reader = TTY::Reader.new
      @secret ||= reader.read_line("Enter your client_secret:", echo: false)
      conn_hash[:client_secret] = @secret
    end
  else
    conn_hash[:netrc] = true
    conn_hash[:netrc_file] = "~/.netrc"
  end
  conn_hash
end