Class: Zizia::BasedNearAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/zizia/hyrax/based_near_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(based_near) ⇒ BasedNearAttributes

Returns a new instance of BasedNearAttributes.



7
8
9
# File 'lib/zizia/hyrax/based_near_attributes.rb', line 7

def initialize(based_near)
  @based_near = based_near
end

Instance Attribute Details

#based_nearObject

Returns the value of attribute based_near.



5
6
7
# File 'lib/zizia/hyrax/based_near_attributes.rb', line 5

def based_near
  @based_near
end

Instance Method Details

#to_hHash

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"=>""
          },
}

Returns:

  • (Hash)

    a “based_near_attributes” hash as



28
29
30
31
32
33
34
35
36
# File 'lib/zizia/hyrax/based_near_attributes.rb', line 28

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

#uri_to_sws(uri) ⇒ String

Take a user-facing geonames URI and return an sws URI, of the form Hyrax expects (e.g., “sws.geonames.org/6252001/”)

Parameters:

  • uri (String)

Returns:

  • (String)

    an sws style geonames uri



43
44
45
46
47
# File 'lib/zizia/hyrax/based_near_attributes.rb', line 43

def uri_to_sws(uri)
  uri = URI(uri)
  geonames_number = uri.path.split('/')[1]
  "http://sws.geonames.org/#{geonames_number}/"
end