Module: MarathonDeploy::HttpUtil

Defined in:
lib/marathon_deploy/http_util.rb

Class Method Summary collapse

Class Method Details

.clean_url(url) ⇒ Object



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

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

.construct_uri(url) ⇒ Object

Raises:



39
40
41
42
# File 'lib/marathon_deploy/http_util.rb', line 39

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

.delete(url) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/marathon_deploy/http_util.rb', line 44

def self.delete(url)
  uri = construct_uri url 
     begin
       http = Net::HTTP.new(uri.host, uri.port)
       req = Net::HTTP::Delete.new(uri.request_uri)         
       response = http.request(req)
     rescue Errno::ECONNREFUSED => e
       message = "Error calling marathon api: #{e.message}"
       $LOG.error(message)
       raise Error::MarathonError, message, caller
     end
     return response    
end

.get(url) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/marathon_deploy/http_util.rb', line 62

def self.get(url)
  uri = construct_uri url
  begin
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.request_uri)
  req["Content-Type"] = "application/json"
  response = http.request(req)
  rescue  Errno::ECONNREFUSED => e
    message = "Error calling marathon api: #{e.message}"
    $LOG.error(message)
    raise Error::MarathonError, message, caller
  end
  return  response
end

.post(url, payload) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/marathon_deploy/http_util.rb', line 23

def self.post(url, payload)
  uri = construct_uri url 
  begin
    http = Net::HTTP.new(uri.host, uri.port)
    req = Net::HTTP::Post.new(uri.request_uri)
    req.body = payload.to_json
    req["Content-Type"] = "application/json"
    response = http.request(req)
  rescue Errno::ECONNREFUSED => e
    message = "Error calling marathon api: #{e.message}"
    $LOG.error(message)
    raise Error::MarathonError, message, caller
  end
  return response
end


84
85
86
87
88
89
90
# File 'lib/marathon_deploy/http_util.rb', line 84

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

.put(url, payload) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/marathon_deploy/http_util.rb', line 8

def self.put(url,payload)
  uri = construct_uri url 
  begin
    http = Net::HTTP.new(uri.host, uri.port)
    req = Net::HTTP::Put.new(uri.request_uri)
    req.body = payload.to_json
    req["Content-Type"] = "application/json"
    response = http.request(req)
  rescue Errno::ECONNREFUSED => e
    $LOG.error("Error calling marathon api: #{e.message}")
    exit!
  end
  return response
end

.valid_url(url) ⇒ Object



77
78
79
80
81
82
# File 'lib/marathon_deploy/http_util.rb', line 77

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