Class: SkullIsland::Resources::Consumer

Inherits:
SkullIsland::Resource show all
Defined in:
lib/skull_island/resources/consumer.rb

Overview

The Consumer resource class

Instance Attribute Summary

Attributes inherited from SkullIsland::Resource

#api_client, #entity, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SkullIsland::Resource

all, find, from_hash, gen_getter_method, gen_property_methods, gen_setter_method, get, immutable, #initialize, property, relative_uri, #relative_uri, where

Methods included from Helpers::ResourceClass

#determine_getter_names, #determine_setter_names, #human, #i18n_key, #immutable?, #param_key, #properties, #route_key

Methods included from Helpers::Resource

#<=>, #datetime_from_params, #delayed_set, #destroy, #digest, #digest_properties, #find_by_digest, #fresh?, #host_regex, #id, #id_property, #immutable?, #import_update_or_skip, #lookup, #model_name, #new?, #persisted?, #postprocess_created_at, #postprocess_updated_at, #properties, #prune_for_save, #reload, #required_properties, #save, #save_uri, #tainted?, #to_param, #to_s, #update

Methods included from Validations::Resource

#validate_id, #validate_mutability, #validate_required_properties, #validate_tags

Constructor Details

This class inherits a constructor from SkullIsland::Resource

Class Method Details

.batch_import(data, verbose: false, test: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/skull_island/resources/consumer.rb', line 15

def self.batch_import(data, verbose: false, test: false)
  raise(Exceptions::InvalidArguments) unless data.is_a?(Array)

  data.each_with_index do |resource_data, index|
    resource = new
    resource.username = resource_data['username']
    resource.custom_id = resource_data['custom_id']
    resource.tags = resource_data['tags'] if resource_data['tags']
    resource.import_update_or_skip(index: index, verbose: verbose, test: test)

    BasicauthCredential.batch_import(
      (
        resource_data.dig('credentials', 'basic-auth') || []
      ).map { |t| t.merge('consumer' => { 'id' => resource.id }) },
      verbose: verbose,
      test: test
    )

    JWTCredential.batch_import(
      (
        resource_data.dig('credentials', 'jwt') || []
      ).map { |t| t.merge('consumer' => { 'id' => resource.id }) },
      verbose: verbose,
      test: test
    )

    KeyauthCredential.batch_import(
      (
        resource_data.dig('credentials', 'key-auth') || []
      ).map { |t| t.merge('consumer' => { 'id' => resource.id }) },
      verbose: verbose,
      test: test
    )
  end
end

Instance Method Details

#add_credential!(details) ⇒ Object

Convenience method to add upstream targets rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/skull_island/resources/consumer.rb', line 55

def add_credential!(details)
  r = if [BasicauthCredential, JWTCredential, KeyauthCredential].include? details.class
        details
      elsif details.is_a?(Hash) && details.key?(:algorithm)
        cred = JWTCredential.new(api_client: api_client)
        cred.algorithm = details[:algorithm]
        cred.key = details[:key] if details[:key]
        cred.secret = details[:secret] if details[:secret]
        cred.rsa_public_key = details[:rsa_public_key] if details[:rsa_public_key]
        cred
      elsif details.is_a?(Hash) && details.key?(:key)
        cred = KeyauthCredential.new(api_client: api_client)
        cred.key = details[:key]
        cred
      elsif details.is_a?(Hash) && details.key?(:username)
        cred = BasicauthCredential.new(api_client: api_client)
        cred.username = details[:username]
        cred.password = details[:password]
        cred
      end

  r.consumer = self
  r.save
end

#created_atObject

The created_at property



12
# File 'lib/skull_island/resources/consumer.rb', line 12

property :created_at, read_only: true, postprocess: true

#credentialsObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



83
84
85
86
87
88
89
90
91
92
# File 'lib/skull_island/resources/consumer.rb', line 83

def credentials
  creds = {}
  keyauth_creds = KeyauthCredential.where(:consumer, self, api_client: api_client)
  creds['key-auth'] = keyauth_creds if keyauth_creds
  basicauth_creds = BasicauthCredential.where(:consumer, self, api_client: api_client)
  creds['basic-auth'] = basicauth_creds if basicauth_creds
  jwt_creds = JWTCredential.where(:consumer, self, api_client: api_client)
  creds['jwt'] = jwt_creds if jwt_creds
  creds
end

#custom_idObject

The custom_id property



11
# File 'lib/skull_island/resources/consumer.rb', line 11

property :custom_id

#export(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/skull_island/resources/consumer.rb', line 99

def export(options = {})
  hash = { 'username' => username, 'custom_id' => custom_id }
  creds = credentials_for_export
  hash['credentials'] = creds unless creds.empty?
  hash['tags'] = tags if tags
  [*options[:exclude]].each do |exclude|
    hash.delete(exclude.to_s)
  end
  [*options[:include]].each do |inc|
    hash[inc.to_s] = send(inc.to_sym)
  end
  hash.reject { |_, value| value.nil? }
end

#modified_existing?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/skull_island/resources/consumer.rb', line 113

def modified_existing?
  return false unless new?

  # Find consumers of the same username
  same_username = self.class.where(:username, username)

  existing = same_username.size == 1 ? same_username.first : nil

  if existing
    @entity['id'] = existing.id
    save
  else
    false
  end
end

#pluginsObject

Provides a collection of related Plugin instances



95
96
97
# File 'lib/skull_island/resources/consumer.rb', line 95

def plugins
  Plugin.where(:consumer, self, api_client: api_client)
end

#tagsObject

The tags property



13
# File 'lib/skull_island/resources/consumer.rb', line 13

property :tags, validate: true

#usernameObject

The username property



10
# File 'lib/skull_island/resources/consumer.rb', line 10

property :username