Class: DataPackage::CollectionDeserializer

Inherits:
BaseDeserializer show all
Defined in:
app/deserializers/data_package/collection_deserializer.rb

Instance Method Summary collapse

Methods inherited from BaseDeserializer

#initialize

Constructor Details

This class inherits a constructor from DataPackage::BaseDeserializer

Instance Method Details

#deserialize ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/deserializers/data_package/collection_deserializer.rb', line 3

def deserialize
  return unless @object
  attributes = {
    name: @object["title"],
    caption: @object["caption"],
    notes: @object["description"],
    links_attributes: []
  }
  begin
    route_options = Rails.application.routes.recognize_path(@object["path"])
    if route_options[:controller] == "collections"
      attributes[:id] = Collection.find_param(route_options[:id])&.id
    end
  rescue ActiveRecord::RecordNotFound
    # ID wasn't found, match by name instead (at least until we allow collections with the same name)
    attributes[:id] = Collection.find_by(name: attributes[:name])&.id
  rescue ActionController::RoutingError
    # ID wasn't found, match by name instead
    attributes[:id] = Collection.find_by(name: attributes[:name])&.id
    attributes[:links_attributes] << {url: @object["path"]} if @object["path"]&.match?(URI::RFC2396_PARSER.make_regexp)
  end
  attributes[:links_attributes].concat(@object["links"]&.map { LinkDeserializer.new(it).deserialize } || [])
  attributes.compact_blank
end