Class: SkullIsland::Resources::UpstreamTarget

Inherits:
SkullIsland::Resource show all
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 inherited from SkullIsland::Resource

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, #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



32
33
34
35
36
# File 'lib/skull_island/resources/upstream_target.rb', line 32

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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/skull_island/resources/upstream_target.rb', line 19

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.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.import_update_or_skip(index: index, verbose: verbose, test: test)
  end
end

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



38
39
40
41
42
43
44
45
# File 'lib/skull_island/resources/upstream_target.rb', line 38

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



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

property :created_at, read_only: true, postprocess: true

#export(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/skull_island/resources/upstream_target.rb', line 68

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

#find_by_digestObject

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



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

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



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

def modified_existing?
  return false unless new?

  # Find routes of the same name
  same_target_and_upstream = self.class.where(:target, target).and(:upstream, upstream)

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

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

#relative_uriObject



60
61
62
# File 'lib/skull_island/resources/upstream_target.rb', line 60

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

#save_uriObject



64
65
66
# File 'lib/skull_island/resources/upstream_target.rb', line 64

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

#tagsObject

The tags property



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

property :tags, validate: true

#targetObject

The target property



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

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

#upstreamObject

The upstream property



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

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

#weightObject

The weight property



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

property :weight, validate: true