Class: ThreeScaleToolbox::Entities::Account

Inherits:
Object
  • Object
show all
Includes:
Entity
Defined in:
lib/3scale_toolbox/entities/account.rb

Constant Summary collapse

PRINTABLE_VARS =
%w[id org_name].freeze
VERBOSE_PRINTABLE_VARS =
%w[
  id org_name created_at updated_at admin_domain domain from_email
  support_email finance_support_email monthly_billing_enabled
  monthly_charging_enabled
].freeze

Instance Attribute Summary

Attributes included from Entity

#id, #remote, #verbose

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Entity

#initialize, #to_s

Class Method Details

.find(remote:, ref:) ⇒ Object



17
18
19
20
21
# File 'lib/3scale_toolbox/entities/account.rb', line 17

def find(remote:, ref:)
  new(id: ref, remote: remote).tap(&:attrs)
rescue ThreeScale::API::HttpClient::NotFoundError
  find_by_text(ref, remote)
end

.find_by_email(remote, email) ⇒ Object



48
49
50
# File 'lib/3scale_toolbox/entities/account.rb', line 48

def find_by_email(remote, email)
  generic_find(remote, email: email)
end

.find_by_provider_or_service_token(remote, text) ⇒ Object



60
61
62
# File 'lib/3scale_toolbox/entities/account.rb', line 60

def find_by_provider_or_service_token(remote, text)
  generic_find(remote, buyer_provider_key: text, buyer_service_token: text)
end

.find_by_text(ref, remote) ⇒ Object

ref can be

  • Email of the account user.

  • Username of the account user.

  • ID of the account user.

  • Master API

    Provider key of the account

  • Master API

    Service token of the account service.

email, username or user_id fields search with AND logic. Therefore separate requests. buyer_provider_key, buyer_service_token fields search with OR logic. Same request.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/3scale_toolbox/entities/account.rb', line 32

def find_by_text(ref, remote)
   = find_by_email(remote, ref)
  return  unless .nil?

   = find_by_username(remote, ref)
  return  unless .nil?

   = find_by_user_id(remote, ref)
  return  unless .nil?

   = find_by_provider_or_service_token(remote, ref)
  return  unless .nil?

  nil
end

.find_by_user_id(remote, user_id) ⇒ Object



56
57
58
# File 'lib/3scale_toolbox/entities/account.rb', line 56

def find_by_user_id(remote, user_id)
  generic_find(remote, user_id: user_id)
end

.find_by_username(remote, username) ⇒ Object



52
53
54
# File 'lib/3scale_toolbox/entities/account.rb', line 52

def find_by_username(remote, username)
  generic_find(remote, username: username)
end

.generic_find(remote, criteria) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/3scale_toolbox/entities/account.rb', line 64

def generic_find(remote, criteria)
   = remote.(criteria)
  if (errors = ['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new(
      'Account find returned errors', errors
    )
  end
  new(id: ['id'], remote: remote, attrs: )
rescue ThreeScale::API::HttpClient::NotFoundError
  nil
end

Instance Method Details

#applicationsObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/3scale_toolbox/entities/account.rb', line 81

def applications
  app_attrs_list = remote.(id)
  if app_attrs_list.respond_to?(:has_key?) && (errors = app_attrs_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Account applications not read', errors)
  end

  app_attrs_list.map do |app_attrs|
    Entities::Application.new(id: app_attrs.fetch('id'), remote: remote, attrs: app_attrs)
  end
end

#attrsObject



77
78
79
# File 'lib/3scale_toolbox/entities/account.rb', line 77

def attrs
  @attrs ||= 
end