Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎁 Implement Bulkrax location input #991

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading