Class: ThreeScaleToolbox::Entities::ActiveDocs

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale_toolbox/entities/activedocs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, remote:, attrs: nil) ⇒ ActiveDocs

Returns a new instance of ActiveDocs.



44
45
46
47
48
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 44

def initialize(id:, remote:, attrs: nil)
  @id = id.to_i
  @remote = remote
  @attrs = attrs
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



42
43
44
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 42

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



42
43
44
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 42

def remote
  @remote
end

Class Method Details

.create(remote:, attrs:) ⇒ Object



5
6
7
8
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 5

def create(remote:, attrs:)
  activedocs_res = create_activedocs(remote: remote, attrs: attrs)
  new(id: activedocs_res.fetch('id'), remote: remote, attrs: activedocs_res)
end

.find(remote:, ref:) ⇒ Object

ref can be system_name or activedocs_id



11
12
13
14
15
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 11

def find(remote:, ref:)
  new(id: ref, remote: remote).tap(&:attrs)
rescue ThreeScaleToolbox::ActiveDocsNotFoundError
  find_by_system_name(remote: remote, system_name: ref)
end

.find_by_system_name(remote:, system_name:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 17

def find_by_system_name(remote:, system_name:)
  activedocs_list = remote.list_activedocs

  if activedocs_list.respond_to?(:has_key?) && (errors = activedocs_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('ActiveDocs list not read', errors)
  end

  res_attrs = activedocs_list.find { |svc| svc['system_name'] == system_name }
  return if res_attrs.nil?

  new(id: res_attrs.fetch('id'), remote: remote, attrs: res_attrs)
end

Instance Method Details

#attrsObject



50
51
52
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 50

def attrs
  @attrs ||= activedoc_attrs
end

#bodyObject



58
59
60
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 58

def body
  attrs['body']
end

#deleteObject



66
67
68
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 66

def delete
  remote.delete_activedocs id
end

#nameObject



62
63
64
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 62

def name
  attrs['name']
end

#system_nameObject



54
55
56
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 54

def system_name
  attrs['system_name']
end

#update(a_attrs) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/3scale_toolbox/entities/activedocs.rb', line 70

def update(a_attrs)
  new_attrs = remote.update_activedocs(id, a_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('ActiveDocs has not been updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end