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

Refactor: extract replace rest_url_prefix to id_url_prefix and and vice versa methods #155

Merged
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
31 changes: 27 additions & 4 deletions lib/ontologies_linked_data/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ def delete(*args)
# Override find method to make sure the id matches what is in the RDF store
# Only do this if the setting is enabled, string comparison sucks
def self.find(id, *options)
if LinkedData.settings.replace_url_prefix && id.to_s.start_with?(LinkedData.settings.rest_url_prefix)
id = RDF::IRI.new(id.to_s.sub(LinkedData.settings.rest_url_prefix, LinkedData.settings.id_url_prefix))
end
id = replace_url_prefix_to_id(id)

# Handle `+` to ` ` conversion here because Sinatra doesn't do it for URI's
id = id.gsub("+", " ") unless id.start_with?("http")
id = id.gsub('+', ' ') unless id.start_with?('http')

super(id, *options)
end
Expand Down Expand Up @@ -137,8 +135,33 @@ def self.goo_aggregates_to_load(attributes = [])
included_aggregates
end

def self.replace_url_prefix_to_id(id)
if replace_url_prefix?(id)
id = RDF::IRI.new(id.to_s.sub(LinkedData.settings.rest_url_prefix, LinkedData.settings.id_url_prefix))
end
id
end

def self.replace_url_id_to_prefix(id)
if replace_url_id?(id)
id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix)
else
id
end
end

def self.replace_url_prefix?(id)
LinkedData.settings.replace_url_prefix && id.to_s.start_with?(LinkedData.settings.rest_url_prefix)
end

def self.replace_url_id?(id)
LinkedData.settings.replace_url_prefix && id.to_s.start_with?(LinkedData.settings.id_url_prefix)
end

private



##
# Looks for an object 'owner' and looks in Thread.current[:remote_user]
# to see if the user for this request matches the owner
Expand Down
12 changes: 3 additions & 9 deletions lib/ontologies_linked_data/monkeypatches/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,10 @@ def convert_nonstandard_types(value, options, &block)
##
# If the config option is set, turn http://data.bioontology.org urls into the configured REST url
def convert_url_prefix(value)
if LinkedData.settings.replace_url_prefix
if value.is_a?(String) && value.start_with?(LinkedData.settings.id_url_prefix)
value = value.sub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix)
end

if (value.is_a?(Array) || value.is_a?(Set)) && value.first.is_a?(String) && value.first.start_with?(LinkedData.settings.id_url_prefix)
value = value.map {|v| v.sub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix)}
end
tmp = Array(value).map do |val|
LinkedData::Models::Base.replace_url_id_to_prefix(val)
end
value
value.is_a?(Array) || value.is_a?(Set) ? tmp : tmp.first
end

##
Expand Down
4 changes: 2 additions & 2 deletions lib/ontologies_linked_data/serializers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def self.serialize(obj, options = {})

# Add the id to json-ld attribute
if current_cls.ancestors.include?(LinkedData::Hypermedia::Resource) && !current_cls.embedded? && hashed_obj.respond_to?(:id)
prefixed_id = LinkedData.settings.replace_url_prefix ? hashed_obj.id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix) : hashed_obj.id.to_s
hash["@id"] = prefixed_id
prefixed_id = LinkedData::Models::Base.replace_url_id_to_prefix(hashed_obj.id)
hash["@id"] = prefixed_id.to_s
end
# Add the type
hash["@type"] = current_cls.type_uri.to_s if hash["@id"] && current_cls.respond_to?(:type_uri)
Expand Down
4 changes: 2 additions & 2 deletions lib/ontologies_linked_data/serializers/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def self.serialize(obj, options)
current_cls = hashed_obj.respond_to?(:klass) ? hashed_obj.klass : hashed_obj.class
# Add the id and type
if current_cls.ancestors.include?(LinkedData::Hypermedia::Resource) && !current_cls.embedded?
prefixed_id = LinkedData.settings.replace_url_prefix ? hashed_obj.id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix) : hashed_obj.id.to_s
hash["id"] = prefixed_id
prefixed_id = LinkedData::Models::Base.replace_url_id_to_prefix(hashed_obj.id)
hash["id"] = prefixed_id.to_s
hash["type"] = current_cls.type_uri.to_s
end

Expand Down