Class: Cafmal::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/cafmal/resource.rb

Direct Known Subclasses

Alert, Alerter, Check, Datasource, Event, Team, User, Worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cafmal_api_url, token) ⇒ Resource

Returns a new instance of Resource.



18
19
20
21
22
23
24
25
26
27
# File 'lib/cafmal/resource.rb', line 18

def initialize(cafmal_api_url, token)
  @cafmal_api_url = cafmal_api_url
  @token = token
  @decoded_token = {}
  @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0]))
  @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1]))
  @query = ""
  @headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{@token}"}
  @resourcename = (demodulize(self.class.name) + "s").downcase
end

Instance Attribute Details

#cafmal_api_urlObject (readonly)

Returns the value of attribute cafmal_api_url.



14
15
16
# File 'lib/cafmal/resource.rb', line 14

def cafmal_api_url
  @cafmal_api_url
end

#decoded_tokenObject (readonly)

Returns the value of attribute decoded_token.



13
14
15
# File 'lib/cafmal/resource.rb', line 13

def decoded_token
  @decoded_token
end

#queryObject

Returns the value of attribute query.



15
16
17
# File 'lib/cafmal/resource.rb', line 15

def query
  @query
end

#tokenObject (readonly)

Returns the value of attribute token.



12
13
14
# File 'lib/cafmal/resource.rb', line 12

def token
  @token
end

Instance Method Details

#create(params) ⇒ Object



29
30
31
32
33
# File 'lib/cafmal/resource.rb', line 29

def create(params)
  request_create_resource = Cafmal::Request::Post.new(@cafmal_api_url + "/#{@resourcename}", params.to_json, @headers)

  return request_create_resource.response
end

#demodulize(path) ⇒ Object

helpers below



67
68
69
70
71
72
73
74
# File 'lib/cafmal/resource.rb', line 67

def demodulize(path)
  path = path.to_s
  if i = path.rindex('::')
    path[(i+2)..-1]
  else
    path
  end
end

#destroy(params) ⇒ Object



59
60
61
62
63
# File 'lib/cafmal/resource.rb', line 59

def destroy(params)
  request_destroy_resource = Cafmal::Request::Delete.new(@cafmal_api_url + "/#{@resourcename}/#{params['id']}", params.to_json, @headers)

  return request_destroy_resource.response
end

#list(*options) ⇒ Object



41
42
43
44
45
# File 'lib/cafmal/resource.rb', line 41

def list(*options)
  request_list_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}" + @query, @headers)

  return request_list_resource.response
end

#newObject



35
36
37
38
39
# File 'lib/cafmal/resource.rb', line 35

def new
  request_new_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}/new", @headers)

  return request_new_resource.response
end

#show(id) ⇒ Object



47
48
49
50
51
# File 'lib/cafmal/resource.rb', line 47

def show(id)
  request_show_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}/" + id.to_s, @headers)

  return request_show_resource.response
end

#update(params) ⇒ Object



53
54
55
56
57
# File 'lib/cafmal/resource.rb', line 53

def update(params)
  request_update_resource = Cafmal::Request::Put.new(@cafmal_api_url + "/#{@resourcename}/#{params['id']}", params.to_json, @headers)

  return request_update_resource.response
end