external_repository.rb revision c997297b21c83680cdba2953cad369b82a5b8805
class << self
def repository
Repository.where(name: Settings.external_repository_name).first_or_create
end
def add_to_repository(iri, message, user)
tmp_path = download_iri(iri)
repository.save_file_only(tmp_path, determine_path(iri, :fullpath),
message, user)
end
def determine_iri(external_iri)
"http://#{Settings.hostname}/#{repository.path}/#{determine_path(external_iri, :fullpath)}"
end
def determine_path(external_iri, symbol)
case symbol
when :fullpath then determine_filepath(external_iri)
when :dirpath then determine_filepath(external_iri, false)
when :basepath then determine_basepath(external_iri, false)
when :basename then determine_basename(external_iri, false)
when :extension then determine_extension(external_iri)
else nil
end
end
private
def determine_filepath(external_iri, with_file=true)
fullpath = iri_split(external_iri)
with_file ? fullpath : fullpath.sub(determine_basename(external_iri), '')
end
def determine_basepath(external_iri, with_extension=true)
basepath = iri_split(external_iri)
with_extension ? basepath : basepath.sub(/\.[^\.]+\z/, '')
end
def determine_basename(external_iri, with_extension=true)
File.basename(determine_basepath(external_iri, with_extension))
end
def determine_extension(external_iri)
determine_basepath(external_iri) =~ /(\.[^\.]+)\z/
$1
end
# split iri into wget -r style
def iri_split(iri)
match = URI::regexp(['http','https']).match(iri)
host = match[4]
path = match[7]
File.join(host, path)
end
def download_iri(external_iri)
file_content = Net::HTTP.get(URI.parse(external_iri))
dir = Pathname.new('/tmp/reference_ontologies/').
join(determine_path(external_iri, :dirpath))
begin
dir.mkpath
rescue
end
filepath = dir.join(determine_basename(external_iri))
File.open(filepath.to_s, 'w') { |f| f.write(file_content) }
filepath
end
end
end