Module: Cts::Mpx::Aci::Transformations

Defined in:
lib/cts/mpx/aci/transformations.rb

Overview

contains the logic to transform/untransform an entry.

Class Method Summary collapse

Class Method Details

.transform_field_reference(field_reference: nil, user: nil) ⇒ String

Transform a field reference into a transformed_field_reference

Parameters:

  • user (User) (defaults to: nil)

    that will make the service calls.

  • field_reference (String) (defaults to: nil)

    to transform to a transformed field reference

Returns:

  • (String)

    transformed_field_reference



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cts/mpx/aci/transformations.rb', line 35

def transform_field_reference(field_reference: nil, user: nil)
  return field_reference unless Validators.field_reference? field_reference

  service_info = Services.from_url field_reference
  endpoint = service_info[:endpoint]
  service = service_info[:service]
  response = Services::Data.get user: user, service: service, endpoint: endpoint, ids: field_reference.split('/').last, fields: 'ownerId,fieldName,namespace'
  return "urn:cts:aci:no-id-found" unless (entry = response.data["entries"].first)
  return "urn:cts:aci:no-qualified-field-name-found" unless entry["fieldName"]

  namespace = response.data['namespace']
  owner_id = entry['ownerId'].split('/').last
  "urn:cts:aci:#{URI.encode_www_form_component(service)}:#{endpoint}:#{owner_id}:#{namespace}$#{entry['fieldName']}"
end

.transform_reference(reference: nil, user: nil, original_account: nil) ⇒ String

Transform a reference into a transformed_reference

Parameters:

  • user (User) (defaults to: nil)

    that will make the service calls.

  • original_account (String) (defaults to: nil)

    to do the transformation from

  • reference (String) (defaults to: nil)

    to transform to a transformed reference

Returns:

  • (String)

    transformed_reference



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cts/mpx/aci/transformations.rb', line 13

def transform_reference(reference: nil, user: nil, original_account: nil)
  return "urn:cts:aci:target-account" if reference == 
  return reference unless Validators.reference? reference

  service_info = Services.from_url reference
  endpoint = service_info[:endpoint]
  service = service_info[:service]
  return reference if service.start_with? "User Data Service"

  response = Services::Data.get user: user, service: service, endpoint: endpoint, ids: reference.split('/').last

  return "urn:cts:aci:no-id-found" unless (entry = response.data["entries"].first)

  return "urn:cts:aci:no-guid-found" unless (guid = entry["guid"])

  "urn:cts:aci:#{URI.encode_www_form_component(service)}:#{endpoint}:#{entry['ownerId'].split('/').last}:#{guid}"
end

.traverse_array(field, value, direction, &block) ⇒ Object

private module method, not explicitly covered



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cts/mpx/aci/transformations.rb', line 126

def traverse_array(field, value, direction, &block)
  if value.map(&:class).uniq.first == String
    value.map do |v|
      if Validators.reference?(v) || Validators.transformed_reference?(v)
        block.yield field, v
      else
        v
      end
    end
  elsif value.map(&:class).uniq.first == Hash
    value.map { |v| traverse_hash v, direction, &block }
  else
    value
  end
end

.traverse_for(hash, direction, &block) ⇒ Object



101
102
103
104
105
106
# File 'lib/cts/mpx/aci/transformations.rb', line 101

def traverse_for(hash, direction, &block)
  id = hash['id']
  output = hash.reject { |k, _v| k == 'id' }
  output = Transformations.send :traverse_hash, Oj.load(Oj.dump(output)), direction, &block
  { "id" => id }.merge(output)
end

.traverse_hash(entry, direction, &block) ⇒ Object

private module method, not explicitly covered



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cts/mpx/aci/transformations.rb', line 109

def traverse_hash(entry, direction, &block)
  entry.each do |field, value|
    case value
    when String
      entry[field] = block.yield field, value if direction == :transform &&
                                                 Validators.reference?(value)
      entry[field] = block.yield field, value if direction == :untransform &&
                                                 Validators.transformed_reference?(value)
    when Array
      entry[field] = traverse_array field, value, direction, &block
    when Hash
      entry[field] = traverse_hash value, direction, &block
    end
  end
end

.untransform_field_reference(transformed_field_reference: nil, user: nil, target_account: nil) ⇒ String

Untransform a transformed_field_reference into a field reference

Parameters:

  • user (User) (defaults to: nil)

    that will make the service calls.

  • transformed_field_reference (String) (defaults to: nil)

    to transform to a reference

Returns:

  • (String)

    field_reference



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cts/mpx/aci/transformations.rb', line 81

def untransform_field_reference(transformed_field_reference: nil, user: nil, target_account: nil)
  return transformed_field_reference unless Validators.transformed_field_reference? transformed_field_reference

  parts = transformed_field_reference.split ':'
  endpoint = parts[4]
  service = parts[3]
  qualified_field_name = URI.decode_www_form_component(parts[6])
  owner_id = "http://access.auth.theplatform.com/data/Account/#{parts[5]}"

  response = Services::Data.get user:     user,
                                service:  service,
                                endpoint: endpoint,
                                query:    { byQualifiedFieldName: qualified_field_name, ownerId: owner_id }

  raise "could not find an entry by qualified field name" unless (entry = response.data["entries"].first)
  raise "service returned too many entries on qualified field name" if response.data["entries"].count > 1

  entry['id']
end

.untransform_reference(transformed_reference: nil, user: nil, target_account: nil) ⇒ String

Untransform a transformed_reference into a reference

Parameters:

  • user (User) (defaults to: nil)

    that will make the service calls.

  • target_account (String) (defaults to: nil)

    to do the transformation from

  • transformed_reference (String) (defaults to: nil)

    to transform to a reference

Returns:

  • (String)

    reference



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cts/mpx/aci/transformations.rb', line 55

def untransform_reference(transformed_reference: nil, user: nil, target_account: nil)
  return  if transformed_reference == "urn:cts:aci:target-account"
  return transformed_reference unless Validators.transformed_reference? transformed_reference

  parts = transformed_reference.split ':'
  endpoint = parts[4]
  service = parts[3]
  guid = URI.decode_www_form_component(parts[6])
  owner_id = "http://access.auth.theplatform.com/data/Account/#{parts[5]}"

  response = Services::Data.get user:     user,
                                service:  service,
                                endpoint: endpoint,
                                ids:      transformed_reference.split('/').last,
                                query:    { byGuid: guid, ownerId: owner_id }

  raise "could not find an entry by guid" unless (entry = response.data["entries"].first)
  raise "service returned too many entries on guid" if response.data["entries"].count > 1

  entry['id']
end