Class: Triglav::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/triglav/model.rb

Direct Known Subclasses

Host, Role, Service

Constant Summary collapse

API_ENDPOINT_MAP =
{
  create:  { method: :post,   path: '/api/%s'           },
  show:    { method: :get,    path: '/api/%s/%s'        },
  update:  { method: :put,    path: '/api/%s/%s'        },
  destroy: { method: :delete, path: '/api/%s/%s'        },
  revert:  { method: :put,    path: '/api/%s/%s/revert' },
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'lib/triglav/model.rb', line 9

def initialize(args)
  @client = args[:client]
  @info   = OpenStruct.new(args[:info])
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/triglav/model.rb', line 7

def client
  @client
end

#infoObject

Returns the value of attribute info.



7
8
9
# File 'lib/triglav/model.rb', line 7

def info
  @info
end

Class Method Details

.build_params(params) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/triglav/model.rb', line 33

def self.build_params(params)
  build_params = {}
  params.each do |key, value|
    build_params["#{self.param}[#{key}]"] = value
  end
  build_params
end

.create(client, params = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/triglav/model.rb', line 41

def self.create(client, params = {})
  endpoint = endpoint_for(:create)
  result   = client.dispatch_request(
    endpoint[:method],
    endpoint[:path],
    build_params(params),
  )
  new(client: client, info: result[param])
end

.endpoint_for(type, *args) ⇒ Object



22
23
24
25
26
27
# File 'lib/triglav/model.rb', line 22

def self.endpoint_for (type, *args)
  endpoint = API_ENDPOINT_MAP[type]
  endpoint_path = endpoint[:path] % [self.path, *args.map { |e| URI.encode(e) }]

  { method: endpoint[:method], path: endpoint_path }
end

.paramObject



29
30
31
# File 'lib/triglav/model.rb', line 29

def self.param
  self.to_s.split('::').last.downcase
end

Instance Method Details

#destroyObject



71
72
73
74
75
76
77
# File 'lib/triglav/model.rb', line 71

def destroy
  endpoint = self.class.endpoint_for(:destroy, info.name)
  result   = client.dispatch_request(endpoint[:method], endpoint[:path])

  self.info = OpenStruct.new(result)
  self
end

#revertObject



79
80
81
82
83
84
85
# File 'lib/triglav/model.rb', line 79

def revert
  endpoint  = self.class.endpoint_for(:revert, info.name)
  result    = client.dispatch_request(endpoint[:method], endpoint[:path])

  self.info = OpenStruct.new(result)
  self
end

#showObject



51
52
53
54
55
56
57
# File 'lib/triglav/model.rb', line 51

def show
  endpoint  = self.class.endpoint_for(:show, info.name)
  result    = client.dispatch_request(endpoint[:method], endpoint[:path])

  self.info = OpenStruct.new(result)
  self
end

#update(params = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/triglav/model.rb', line 59

def update(params = {})
  endpoint = self.class.endpoint_for(:update, info.name)
  result   = client.dispatch_request(
    endpoint[:method],
    endpoint[:path],
    self.class.build_params(params),
  )

  self.info = OpenStruct.new(result)
  self
end