Class: CloudhouseGuardian::BaseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudhouse_guardian/BaseObject.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appliance_url, appliance_api_key, sec_key, insecure = false) ⇒ BaseObject

Returns a new instance of BaseObject.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 27

def initialize(appliance_url, appliance_api_key, sec_key, insecure = false)
  if appliance_url.to_s.start_with?("http://") || appliance_url.to_s.start_with?("https://")
    # all good
  else
    appliance_url = "https://" + appliance_url
  end
  self.appliance_url = appliance_url
  self.appliance_api_key = appliance_api_key
  self.sec_key = sec_key
  self.insecure = insecure
end

Instance Attribute Details

#appliance_api_keyObject

Returns the value of attribute appliance_api_key.



6
7
8
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 6

def appliance_api_key
  @appliance_api_key
end

#appliance_urlObject

Returns the value of attribute appliance_url.



5
6
7
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 5

def appliance_url
  @appliance_url
end

#insecureObject

Returns the value of attribute insecure.



8
9
10
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 8

def insecure
  @insecure
end

#sec_keyObject

Returns the value of attribute sec_key.



7
8
9
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 7

def sec_key
  @sec_key
end

Instance Method Details

#from_hash(h) ⇒ Object



9
10
11
12
13
14
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 9

def from_hash(h)
  self.appliance_url = h['appliance_url'] if h.include?('appliance_url')
  self.appliance_api_key = h['appliance_api_key'] if h.include?('appliance_api_key')
  self.sec_key = h['sec_key'] if h.include?('sec_key')
  self.insecure = h['insecure'] if h.include?('insecure')
end

#http_delete(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 85

def http_delete(path)
  response = HTTParty.delete("#{self.appliance_url}#{path}",
               :headers => make_headers,
               :verify => (self.insecure == false)
             )
  if response.code.to_s != "204"
    raise response.body
  end

end

#http_get(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 46

def http_get(path)
  url = "#{self.appliance_url}#{path}"
  response = HTTParty.get(url,
               :headers => make_headers,
               :verify => (self.insecure == false)
               )
  if response.code.to_s != "200"
    raise response.body
  end
  obj = JSON.parse(response.body)
  return obj
end

#http_post(path, body) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 59

def http_post(path, body)
  response = HTTParty.post("#{self.appliance_url}#{path}",
               :headers => make_headers,
               :verify => (self.insecure == false),
               :body => body.to_json
             )
  if ["200", "201", "204"].include?(response.code.to_s) == false
    raise response.body
  end
  return true if response.code.to_s == "204"
  obj = JSON.parse(response.body)
  return obj
end

#http_put(path, body) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 73

def http_put(path, body)
  response = HTTParty.put("#{self.appliance_url}#{path}",
               :headers => make_headers,
               :verify => (self.insecure == false),
               :body => body.to_json
             )
  if response.code.to_s != "204"
    raise response.body
  end

end

#make_headersObject



39
40
41
42
43
44
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 39

def make_headers
  return {
    "Authorization" => "Token token=\"#{self.appliance_api_key}#{self.sec_key}\"",
    "Content-Type" => "application/json"
  }
end

#to_hashObject



15
16
17
18
19
20
21
22
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 15

def to_hash
  h = {}
  h['appliance_url'] = self.appliance_url
  h['appliance_api_key'] = self.appliance_api_key
  h['sec_key'] = self.sec_key
  h['insecure'] = self.insecure
  return h
end

#to_json(options = nil) ⇒ Object



23
24
25
26
# File 'lib/cloudhouse_guardian/BaseObject.rb', line 23

def to_json(options = nil)
  h = to_hash
  return h.to_json(options)
end