Class: ThreeScaleToolbox::Entities::BackendUsage

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

Overview

BackendUsage represents Product - Backend mapping entry

Constant Summary collapse

CREATE_PARAMS =
%w[path backend_api_id].freeze
UPDATE_PARAMS =
%w[path].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, product:, attrs: nil) ⇒ BackendUsage

Returns a new instance of BackendUsage.



40
41
42
43
44
45
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 40

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



38
39
40
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 38

def id
  @id
end

#productObject (readonly)

Returns the value of attribute product.



38
39
40
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 38

def product
  @product
end

#remoteObject (readonly)

Returns the value of attribute remote.



38
39
40
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 38

def remote
  @remote
end

Class Method Details

.create(product:, attrs:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 12

def create(product:, attrs:)
  resp = product.remote.create_backend_usage(
    product.id,
    Helper.filter_params(CREATE_PARAMS, attrs)
  )
  if (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage has not been created',
                                                    errors)
  end

  new(id: resp.fetch('id'), product: product, attrs: resp)
end

.find_by_path(product:, path:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 25

def find_by_path(product:, path:)
  resp = product.remote.list_backend_usages product.id
  if resp.respond_to?(:has_key?) && (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage list error', errors)
  end

  backend_usage_attrs = resp.find { |bus| bus['path'] == path }
  return if backend_usage_attrs.nil?

  new(id: backend_usage_attrs.fetch('id'), product: product, attrs: backend_usage_attrs)
end

Instance Method Details

#attrsObject



47
48
49
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 47

def attrs
  @attrs ||= fetch_attrs
end

#backend_idObject



55
56
57
58
59
60
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 55

def backend_id
  # 3scale API returns 'backend_id'
  # 3scale API only accepts 'backend_api_id' as params
  # good job
  attrs['backend_id']
end

#deleteObject



81
82
83
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 81

def delete
  remote.delete_backend_usage product.id, id
end

#pathObject



51
52
53
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 51

def path
  attrs['path']
end

#update(usage_attrs) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 62

def update(usage_attrs)
  new_attrs = remote.update_backend_usage(
    product.id, id,
    Helper.filter_params(UPDATE_PARAMS, usage_attrs)
  )
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage not been updated', errors)
  end

  if new_attrs['service_id'] != product.id
    raise ThreeScaleToolbox::Error, 'Backend usage product updated'
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end