Class: ThreeScaleToolbox::Entities::Application

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Application.



61
62
63
64
65
# File 'lib/3scale_toolbox/entities/application.rb', line 61

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.



59
60
61
# File 'lib/3scale_toolbox/entities/application.rb', line 59

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



59
60
61
# File 'lib/3scale_toolbox/entities/application.rb', line 59

def remote
  @remote
end

Class Method Details

.create(remote:, account_id:, plan_id:, app_attrs: nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/3scale_toolbox/entities/application.rb', line 5

def create(remote:, account_id:, plan_id:, app_attrs: nil)
  attrs = remote.create_application(, app_attrs, plan_id: plan_id)
  if (errors = attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Application has not been created', errors)
  end

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

.find(remote:, service_id: nil, ref:) ⇒ Object

ref can be

  • User_key (API key)

  • App_id (from app_id/app_key pair) / Client ID (for OAuth and OpenID Connect authentication modes)

  • Application internal id



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

def find(remote:, service_id: nil, ref:)
  app = find_by_user_key(remote, service_id, ref)
  return app unless app.nil?

  app = find_by_app_id(remote, service_id, ref)
  return app unless app.nil?

  app = find_by_id(remote, service_id, ref)
  return app unless app.nil?

  nil
end

.find_by_app_id(remote, service_id, app_id) ⇒ Object



39
40
41
# File 'lib/3scale_toolbox/entities/application.rb', line 39

def find_by_app_id(remote, service_id, app_id)
  generic_find(remote, service_id, :application_id, app_id)
end

.find_by_id(remote, service_id, id) ⇒ Object



31
32
33
# File 'lib/3scale_toolbox/entities/application.rb', line 31

def find_by_id(remote, service_id, id)
  generic_find(remote, service_id, :id, id)
end

.find_by_user_key(remote, service_id, user_key) ⇒ Object



35
36
37
# File 'lib/3scale_toolbox/entities/application.rb', line 35

def find_by_user_key(remote, service_id, user_key)
  generic_find(remote, service_id, :user_key, user_key)
end

Instance Method Details

#attrsObject



67
68
69
# File 'lib/3scale_toolbox/entities/application.rb', line 67

def attrs
  @attrs ||= application_attrs
end

#deleteObject



108
109
110
# File 'lib/3scale_toolbox/entities/application.rb', line 108

def delete
  remote.delete_application , id
end

#live?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/3scale_toolbox/entities/application.rb', line 112

def live?
  attrs.fetch('state') == 'live'
end

#resumeObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/3scale_toolbox/entities/application.rb', line 82

def resume
  return attrs if live?

  new_attrs = remote.resume_application(, id)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Application has not been resumed', errors)
  end

  @attrs = new_attrs

  new_attrs
end

#suspendObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/3scale_toolbox/entities/application.rb', line 95

def suspend
  return attrs unless live?

  new_attrs = remote.suspend_application(, id)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Application has not been suspended', errors)
  end

  @attrs = new_attrs

  new_attrs
end

#update(app_attrs) ⇒ Object



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

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

  @attrs = new_attrs

  new_attrs
end