Module: MarathonDeploy::HttpUtil

Defined in:
lib/marathon_deploy/http_util.rb

Constant Summary collapse

@@o_timeout =
60.0
@@r_timeout =
60.0
@@og_timeout =
60.0
@@rg_timeout =
60.0

Class Method Summary collapse

Class Method Details

.clean_url(url) ⇒ Object



59
60
61
# File 'lib/marathon_deploy/http_util.rb', line 59

def self.clean_url(url)
  url.sub(/(\/)+$/,'')
end

.construct_uri(url) ⇒ Object

Raises:



50
51
52
53
# File 'lib/marathon_deploy/http_util.rb', line 50

def self.construct_uri(url)
  raise Error::BadURLError unless (valid_url(url))
  return URI.parse(url)
end

.delete(url) ⇒ Object



55
56
57
# File 'lib/marathon_deploy/http_util.rb', line 55

def self.delete(url)
  return self.req('Delete', url, nil, false)
end

.get(url) ⇒ Object



63
64
65
# File 'lib/marathon_deploy/http_util.rb', line 63

def self.get(url)
  return self.req('Get', url, nil, false)
end

.post(url, payload) ⇒ Object



46
47
48
# File 'lib/marathon_deploy/http_util.rb', line 46

def self.post(url, payload)
  return self.req('Post', url, payload, false)
end


74
75
76
77
78
79
80
# File 'lib/marathon_deploy/http_util.rb', line 74

def self.print(response)
  begin
    puts JSON.pretty_generate(JSON.parse(response.body))
  rescue
    puts response
  end
end

.put(url, payload) ⇒ Object



42
43
44
# File 'lib/marathon_deploy/http_util.rb', line 42

def self.put(url,payload)
  return self.req('Put', url, payload, true)
end

.req(method, url, payload = nil, errors_are_fatal = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/marathon_deploy/http_util.rb', line 13

def self.req(method, url, payload=nil, errors_are_fatal=false)
  uri = construct_uri url 
  begin
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == 'https'
    http.open_timeout = @@o_timeout
    http.read_timeout = @@r_timeout
    req = Net::HTTP.const_get(method).new(uri.request_uri)
    if MarathonDeploy::MarathonDefaults::marathon_username and MarathonDeploy::MarathonDefaults::marathon_password
      req.basic_auth(MarathonDeploy::MarathonDefaults::marathon_username, MarathonDeploy::MarathonDefaults::marathon_password)
    end
    if payload
      req.body = payload.to_json
    end
    req["Content-Type"] = "application/json"
    response = http.request(req)
  rescue Exception => e
    if errors_are_fatal
      $LOG.error("Error calling marathon api: #{e.message}")
      exit!
    else
      message = "Error calling marathon api: #{e.message}"
      $LOG.error(message)
      raise Error::MarathonError, message, caller
    end
  end
  return response
end

.valid_url(url) ⇒ Object



67
68
69
70
71
72
# File 'lib/marathon_deploy/http_util.rb', line 67

def self.valid_url(url)
  if (url =~ /\A#{URI::regexp}\z/)
    return true
  end
  return false
end