Class: Http

Inherits:
Object
  • Object
show all
Defined in:
lib/vmfloaty/http.rb

Class Method Summary collapse

Class Method Details

.get_conn(verbose, url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vmfloaty/http.rb', line 19

def self.get_conn(verbose, url)
  raise 'Did not provide a url to connect to' if url.nil?

  url = "https://#{url}" unless url?(url)

  Faraday.new(url: url, ssl: { verify: false }) do |faraday|
    faraday.request :url_encoded
    faraday.response :logger if verbose
    faraday.adapter Faraday.default_adapter
  end
end

.get_conn_with_auth(verbose, url, user, password) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vmfloaty/http.rb', line 31

def self.get_conn_with_auth(verbose, url, user, password)
  raise 'Did not provide a url to connect to' if url.nil?

  raise 'You did not provide a user to authenticate with' if user.nil?

  url = "https://#{url}" unless url?(url)

  Faraday.new(url: url, ssl: { verify: false }) do |faraday|
    faraday.request :url_encoded
    faraday.request :basic_auth, user, password
    faraday.response :logger if verbose
    faraday.adapter Faraday.default_adapter
  end
end

.url?(url) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vmfloaty/http.rb', line 7

def self.url?(url)
  # This method exists because it seems like Farady
  # has no handling around if a user gives us a URI
  # with no protocol on the beginning of the URL

  uri = URI.parse(url)

  return true if uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)

  false
end