Class: SkullIsland::Resources::UpstreamTarget

Inherits:
SkullIsland::Resource show all
Includes:
Helpers::Meta
Defined in:
lib/skull_island/resources/upstream_target.rb

Overview

The Upstream Target resource class

Instance Attribute Summary

Attributes inherited from SkullIsland::Resource

#api_client, #entity, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Meta

#add_meta, #import_time, #import_time=, #metatags, #postprocess_tags, #preprocess_tags, #project, #project=, #raw_tags, #remove_meta, #supports_meta?

Methods inherited from SkullIsland::Resource

cleanup_except, find, from_hash, gen_getter_method, gen_property_methods, gen_setter_method, immutable, #initialize, property, 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, #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, #supports_meta?, #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

.all(options = {}) ⇒ Object

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



45
46
47
48
49
# File 'lib/skull_island/resources/upstream_target.rb', line 45

def self.all(options = {})
  api_client = options[:api_client] || APIClient.instance

  Upstream.all(api_client: api_client).map(&:targets).reduce(&:merge)
end

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

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/skull_island/resources/upstream_target.rb', line 23

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

  known_ids = []

  data.each_with_index do |resource_data, index|
    resource = new
    resource.delayed_set(:target, resource_data, 'target')
    resource.delayed_set(:upstream, resource_data, 'upstream')
    resource.weight = resource_data['weight'] if resource_data['weight']
    resource.tags = resource_data['tags'] if resource_data['tags']
    resource.project = project if project
    resource.import_time = (time || Time.now.utc.to_i) if project
    resource.import_update_or_skip(index: index, verbose: verbose, test: test)
    known_ids << resource.id
  end

  cleanup_except(project, known_ids) if project
end

.get(id, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/skull_island/resources/upstream_target.rb', line 51

def self.get(id, options = {})
  if options[:upstream]&.is_a?(Upstream)
    options[:upstream].target(id)
  elsif options[:upstream]
    upstream_opts = options.merge(lazy: true)
    Upstream.get(options[:upstream], upstream_opts).target(id)
  end
end

Instance Method Details

#created_atObject

The created_at property



18
# File 'lib/skull_island/resources/upstream_target.rb', line 18

property :created_at, read_only: true, postprocess: true

#export(options = {}) ⇒ Object



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

def export(options = {})
  hash = { 'target' => target, 'weight' => weight }
  hash['upstream'] = "<%= lookup :upstream, '#{upstream.name}' %>" if upstream
  hash['tags'] = tags unless tags.empty?
  [*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

#find_by_digestObject

Tests for an existing version of this resource based on its properties rather than its ‘id`



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/skull_island/resources/upstream_target.rb', line 61

def find_by_digest
  result = self.class.where(:digest, digest) # matching digest means the equivalent resource
  if result.size == 1
    @entity  = result.first.entity
    @lazy    = false
    @tainted = false
    true
  else
    false
  end
end

#modified_existing?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/skull_island/resources/upstream_target.rb', line 94

def modified_existing?
  false # Upstream Targets can not be PATCHed, so can not be modified
end

#relative_uriObject



73
74
75
# File 'lib/skull_island/resources/upstream_target.rb', line 73

def relative_uri
  upstream ? "#{upstream.relative_uri}/targets/#{id}" : nil
end

#save_uriObject



77
78
79
# File 'lib/skull_island/resources/upstream_target.rb', line 77

def save_uri
  upstream ? "#{upstream.relative_uri}/targets" : nil
end

#tagsObject

The tags property



19
# File 'lib/skull_island/resources/upstream_target.rb', line 19

property :tags, validate: true, preprocess: true, postprocess: true

#targetObject

The target property



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

property :target, required: true, validate: true, preprocess: true

#upstreamObject

The upstream property



13
14
15
16
# File 'lib/skull_island/resources/upstream_target.rb', line 13

property(
  :upstream,
  required: true, validate: true, preprocess: true, postprocess: true
)

#weightObject

The weight property



17
# File 'lib/skull_island/resources/upstream_target.rb', line 17

property :weight, validate: true