2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-09-24 04:27:34 +00:00
|
|
|
class MapExportService
|
2017-01-22 21:42:04 +00:00
|
|
|
attr_reader :user, :map, :base_url
|
|
|
|
|
|
|
|
def initialize(user, map, opts = {})
|
2016-09-24 04:27:34 +00:00
|
|
|
@user = user
|
|
|
|
@map = map
|
2017-01-22 21:42:04 +00:00
|
|
|
@base_url = opts[:base_url] || 'https://metamaps.cc'
|
2016-09-24 04:27:34 +00:00
|
|
|
end
|
|
|
|
|
2016-03-26 03:31:55 +00:00
|
|
|
def json
|
|
|
|
# marshal_dump turns OpenStruct into a Hash
|
|
|
|
{
|
2016-03-26 07:21:55 +00:00
|
|
|
topics: exportable_topics.map(&:marshal_dump),
|
|
|
|
synapses: exportable_synapses.map(&:marshal_dump)
|
2016-03-26 03:31:55 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def csv(options = {})
|
|
|
|
CSV.generate(options) do |csv|
|
|
|
|
to_spreadsheet.each do |line|
|
|
|
|
csv << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-22 21:42:04 +00:00
|
|
|
def rdf
|
|
|
|
output = ''
|
|
|
|
output += "PREFIX d: <#{base_url}/maps/#{map.id}>\n"
|
|
|
|
output += "PREFIX mm: <#{base_url}/owl/map.owl.ttl>\n"
|
|
|
|
output += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
|
|
|
|
output += "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
|
|
|
|
output += "\n"
|
|
|
|
map.contributors.each do |mapper|
|
|
|
|
output += mapper.as_rdf(base_url: base_url)
|
|
|
|
end
|
|
|
|
map.topics.each do |topic|
|
|
|
|
output += topic.as_rdf
|
|
|
|
end
|
|
|
|
map.synapses.each do |synapse|
|
|
|
|
output += synapse.as_rdf
|
|
|
|
end
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2016-03-26 03:31:55 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def topic_headings
|
2018-01-20 22:10:26 +00:00
|
|
|
%i[id name metacode x y description link user permission]
|
2016-03-26 03:31:55 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2016-03-26 03:31:55 +00:00
|
|
|
def synapse_headings
|
2018-01-20 22:10:26 +00:00
|
|
|
%i[topic1 topic2 category description user permission]
|
2016-03-26 03:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def exportable_topics
|
2016-03-26 07:21:55 +00:00
|
|
|
visible_topics ||= Pundit.policy_scope!(user, map.topics)
|
2018-01-20 22:10:26 +00:00
|
|
|
topic_mappings = Mapping.includes(mappable: %i[metacode user])
|
2016-03-26 07:21:55 +00:00
|
|
|
.where(mappable: visible_topics, map: map)
|
2016-03-26 03:31:55 +00:00
|
|
|
topic_mappings.map do |mapping|
|
|
|
|
topic = mapping.mappable
|
2016-08-12 04:39:30 +00:00
|
|
|
next nil if topic.nil?
|
2016-03-26 03:31:55 +00:00
|
|
|
OpenStruct.new(
|
|
|
|
id: topic.id,
|
|
|
|
name: topic.name,
|
|
|
|
metacode: topic.metacode.name,
|
|
|
|
x: mapping.xloc,
|
|
|
|
y: mapping.yloc,
|
|
|
|
description: topic.desc,
|
|
|
|
link: topic.link,
|
|
|
|
user: topic.user.name,
|
|
|
|
permission: topic.permission
|
|
|
|
)
|
2016-08-12 04:39:30 +00:00
|
|
|
end.compact
|
2016-03-26 03:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def exportable_synapses
|
2016-03-26 07:21:55 +00:00
|
|
|
visible_synapses = Pundit.policy_scope!(user, map.synapses)
|
2016-03-26 03:31:55 +00:00
|
|
|
visible_synapses.map do |synapse|
|
2016-08-12 04:39:30 +00:00
|
|
|
next nil if synapse.nil?
|
2016-03-26 03:31:55 +00:00
|
|
|
OpenStruct.new(
|
2016-09-28 02:32:28 +00:00
|
|
|
topic1: synapse.topic1_id,
|
|
|
|
topic2: synapse.topic2_id,
|
2016-03-26 03:31:55 +00:00
|
|
|
category: synapse.category,
|
|
|
|
description: synapse.desc,
|
|
|
|
user: synapse.user.name,
|
|
|
|
permission: synapse.permission
|
|
|
|
)
|
2016-08-12 04:39:30 +00:00
|
|
|
end.compact
|
2016-03-26 03:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_spreadsheet
|
|
|
|
spreadsheet = []
|
2016-07-26 00:14:23 +00:00
|
|
|
spreadsheet << ['Topics']
|
2016-03-26 07:21:55 +00:00
|
|
|
spreadsheet << topic_headings.map(&:capitalize)
|
2016-03-26 03:31:55 +00:00
|
|
|
exportable_topics.each do |topics|
|
|
|
|
# convert exportable_topics into an array of arrays
|
2016-03-26 07:21:55 +00:00
|
|
|
spreadsheet << topic_headings.map { |h| topics.send(h) }
|
2016-03-26 03:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
spreadsheet << []
|
2016-07-26 00:14:23 +00:00
|
|
|
spreadsheet << ['Synapses']
|
2016-03-26 07:21:55 +00:00
|
|
|
spreadsheet << synapse_headings.map(&:capitalize)
|
2016-03-26 03:31:55 +00:00
|
|
|
exportable_synapses.each do |synapse|
|
|
|
|
# convert exportable_synapses into an array of arrays
|
2016-03-26 07:21:55 +00:00
|
|
|
spreadsheet << synapse_headings.map { |h| synapse.send(h) }
|
2016-03-26 03:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
spreadsheet
|
|
|
|
end
|
|
|
|
end
|