Skip to content

Commit

Permalink
Merge pull request #991 from scientist-softserv/i964-location-name-fi…
Browse files Browse the repository at this point in the history
…eld-through-bulkrax

🎁 Implement Bulkrax location input
  • Loading branch information
kirkkwang authored Mar 8, 2024
2 parents 687d982 + 992603b commit 19084ad
Show file tree
Hide file tree
Showing 6 changed files with 1,014 additions and 1 deletion.
47 changes: 47 additions & 0 deletions app/models/concerns/bulkrax/has_matchers_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# OVERRIDE Bulkrax v5.3.0 to add a geonames lookup for the `based_near` proprerty

module Bulkrax
module HasMatchersDecorator
def matched_metadata(multiple, name, result, object_multiple)
if name == 'based_near'
result = if result.start_with?('http')
Hyrax::ControlledVocabularies::Location.new(RDF::URI.new(result))
else
geonames_lookup(result)
end
end
super
end

private

def geonames_lookup(result)
geonames_username = ::Site.instance.account.geonames_username
return nil unless geonames_username

base_url = 'http://api.geonames.org/searchJSON'
params = { q: result, maxRows: 10, username: geonames_username }
uri = URI(base_url)
uri.query = URI.encode_www_form(params)

response = Net::HTTP.get_response(uri)
data = JSON.parse(response.body)
geoname = data['geonames'].first

unless geoname
uri = URI::HTTP.build(host: 'fake', fragment: result)
return Hyrax::ControlledVocabularies::Location.new(RDF::URI.new(uri))
end

# Create a Hyrax::ControlledVocabularies::Location object with the RDF subject
rdf_subject = RDF::URI.new("https://sws.geonames.org/#{geoname['geonameId']}/")
Hyrax::ControlledVocabularies::Location.new(rdf_subject)
end
end
end

# Prepending this to `Bulkrax::HasMatchers` yielded an unbound method
# Thus, I am prepending it to `Bulkrax::Entry` since that mixes in `Bulkrax::HasMatchers`
Bulkrax::Entry.prepend(Bulkrax::HasMatchersDecorator)
22 changes: 22 additions & 0 deletions lib/hyrax/controlled_vocabularies/location_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ module LocationDecorator
def split(*)
[]
end

def present?
return true if id

false
end

def rdf_label
return [id.gsub('http://fake#', '').gsub('%20', ' ')] if fake_location?

super
end

def full_label
return rdf_label.first.to_s if fake_location?

super
end

def fake_location?
id.start_with?('http://fake#')
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/factories/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
gtm_id: "GTM-123456", shared_login: "true",
email_format: ["@pacificu.edu", "@ubiquitypress.com", "@test.com"],
allow_signup: "true",
google_analytics_id: 'UA-123456-12'
google_analytics_id: 'UA-123456-12',
geonames_username: 'geonames'
}
end

Expand Down
Loading

0 comments on commit 19084ad

Please sign in to comment.