Sha256: 7895f840301311c48166449528d2069ffb15b40b08c5e20429ed43ff0a00218d

Contents?: true

Size: 1.64 KB

Versions: 17

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Zizia
  class BasedNearAttributes
    attr_accessor :based_near

    def initialize(based_near)
      @based_near = based_near
    end

    ##
    # When submitting location data (a.k.a. the "based near" attribute) via the UI,
    # Hyrax expects to receive a `based_near_attributes` hash in a specific format.
    # We need to take geonames urls as provided by the customer and transform them to
    # mimic what the Hyrax UI would ordinarily produce. These will get turned into
    # Hyrax::ControlledVocabularies::Location objects upon ingest.
    # The expected hash looks like this:
    #   "based_near_attributes"=>
    #     {
    #       "0"=> {
    #               "id"=>"http://sws.geonames.org/5667009/", "_destroy"=>""
    #             },
    #       "1"=> {
    #               "id"=>"http://sws.geonames.org/6252001/", "_destroy"=>""
    #             },
    #   }
    # @return [Hash] a "based_near_attributes" hash as
    def to_h
      original_geonames_uris = based_near
      return if original_geonames_uris.empty?
      based_near_attributes = {}
      original_geonames_uris.each_with_index do |uri, i|
        based_near_attributes[i.to_s] = { 'id' => uri_to_sws(uri), "_destroy" => "" }
      end
      based_near_attributes
    end

    #
    # Take a user-facing geonames URI and return an sws URI, of the form Hyrax expects
    # (e.g., "http://sws.geonames.org/6252001/")
    # @param [String] uri
    # @return [String] an sws style geonames uri
    def uri_to_sws(uri)
      uri = URI(uri)
      geonames_number = uri.path.split('/')[1]
      "http://sws.geonames.org/#{geonames_number}/"
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
zizia-6.0.1 lib/zizia/hyrax/based_near_attributes.rb
zizia-6.0.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.5.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.4.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.3.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.2.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.1.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.0.1 lib/zizia/hyrax/based_near_attributes.rb
zizia-5.0.0 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.6.0.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.5.4.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.5.3.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.5.2.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.5.1.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.4.1.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.4.0.alpha.01 lib/zizia/hyrax/based_near_attributes.rb
zizia-4.3.1.alpha.01 lib/zizia/hyrax/based_near_attributes.rb