Class: ForestLiana::ApimapSorter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/apimap_sorter.rb

Constant Summary collapse

KEYS_COLLECTION =

TODO: Remove nameOld attribute once the lianas versions older than 2.0.0 are minority.

[
  'name',
  'name_old',
  'icon',
  'integration',
  'is_read_only',
  'is_searchable',
  'is_virtual',
  'only_for_relationships',
  'pagination_type',
  'fields',
]
KEYS_COLLECTION_FIELD =
[
  'field',
  'type',
  'default_value',
  'enums',
  'integration',
  'is_filterable',
  'is_read_only',
  'is_required',
  'is_sortable',
  'is_virtual',
  'reference',
  'inverse_of',
  'relationship',
  'widget',
  'validations',
]
KEYS_ACTION =
[
  'name',
  'type',
  'base_url',
  'endpoint',
  'http_method',
  'redirect',
  'download',
  'fields',
  'hooks',
]
KEYS_ACTION_FIELD =
[
  'field',
  'type',
  'default_value',
  'enums',
  'is_required',
  'is_read_only',
  'reference',
  'description',
  'position',
  'widget',
  'hook',
]
KEYS_SEGMENT =
['name']

Instance Method Summary collapse

Constructor Details

#initialize(apimap) ⇒ ApimapSorter

Returns a new instance of ApimapSorter.


59
60
61
# File 'app/services/forest_liana/apimap_sorter.rb', line 59

def initialize apimap
  @apimap = apimap.deep_stringify_keys
end

Instance Method Details

#performObject


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/forest_liana/apimap_sorter.rb', line 63

def perform
  begin
    @apimap = reorder_keys_basic(@apimap)
    sort_array_of_objects(@apimap['data'])
    @apimap['data'].map! do |collection|
      collection = reorder_keys_child(collection)
      collection['attributes'] = reorder_collection_attributes(collection['attributes'])
      if collection['attributes']['fields']
        collection['attributes']['fields'] = sort_array_of_fields(collection['attributes']['fields'])
        collection['attributes']['fields'].map! { |field| reorder_collection_fields(field) }
      end
      collection
    end

    if @apimap['included']
      sort_array_of_objects(@apimap['included'])

      @apimap['included'].map! do |object|
        object = reorder_keys_child(object)
        if object['type'] === 'actions'
          object['attributes'] = reorder_action_attributes(object['attributes'])
          if object['attributes']['fields']
            object['attributes']['fields'] = sort_array_of_fields(object['attributes']['fields'])
            object['attributes']['fields'].map! { |field| reorder_action_fields(field)  }

          end
        else
          object['attributes'] = reorder_segment_attributes(object['attributes'])
        end
        object
      end
    end

    @apimap['meta']['stack'] = reorder_keys_basic(@apimap['meta']['stack'])
    @apimap['meta'] = reorder_keys_basic(@apimap['meta'])
    @apimap
  rescue => exception
    FOREST_LOGGER.warn "An Apimap reordering issue occured: #{exception}"
    @apimap
  end
end