diff --git a/app/openbel/api/helpers/base.rb b/app/openbel/api/helpers/base.rb new file mode 100644 index 0000000..b588b91 --- /dev/null +++ b/app/openbel/api/helpers/base.rb @@ -0,0 +1,17 @@ +module OpenBEL + module Helpers + + DEFAULT_CONTENT_TYPE = 'application/hal+json' + DEFAULT_CONTENT_TYPE_ID = :hal + + def wants_default? + if params[:format] + return params[:format] == DEFAULT_CONTENT_TYPE + end + + request.accept.any? { |accept_entry| + accept_entry.to_s == DEFAULT_CONTENT_TYPE + } + end + end +end diff --git a/app/openbel/api/helpers/evidence.rb b/app/openbel/api/helpers/evidence.rb new file mode 100644 index 0000000..fbabb52 --- /dev/null +++ b/app/openbel/api/helpers/evidence.rb @@ -0,0 +1,87 @@ +require_relative 'base' +require_relative 'translators' + +module OpenBEL + module Helpers + + def render_evidence_collection( + name, page_results, start, size, filters, + filtered_total, collection_total + ) + # see if the user requested a BEL translator (Accept header or ?format) + translator = Translators.requested_translator(request, params) + translator_plugin = Translators.requested_translator_plugin(request, params) + + # Serialize to HAL if they [Accept]ed it, specified it as ?format, or + # no translator was found to match request. + if wants_default? || !translator + evidence = page_results[:cursor].map { |item| + item.delete('facets') + item + }.to_a + + facets = page_results[:facets] + + halt 404 if evidence.empty? + + pager = Pager.new(start, size, filtered_total) + + options = { + :facets => facets, + :start => start, + :size => size, + :filters => filters, + :metadata => { + :collection_paging => { + :total => collection_total, + :total_filtered => pager.total_size, + :total_pages => pager.total_pages, + :current_page => pager.current_page, + :current_page_size => evidence.size, + } + } + } + + # pager links + options[:previous_page] = pager.previous_page + options[:next_page] = pager.next_page + + render_collection(evidence, :evidence, options) + else + extension = translator_plugin.file_extensions.first + + response.headers['Content-Type'] = translator_plugin.media_types.first + status 200 + attachment "#{name}.#{extension}" + stream :keep_open do |response| + cursor = page_results[:cursor] + dataset_evidence = cursor.lazy.map { |evidence| + evidence.delete('facets') + evidence.delete('_id') + evidence = keys_to_symbols(evidence) + BEL::Model::Evidence.create(evidence) + } + + translator.write(dataset_evidence, response) + end + end + end + + def keys_to_symbols(obj) + case obj + when Array + obj.inject([]) {|new_array, v| + new_array << keys_to_symbols(v) + new_array + } + when Hash + obj.inject({}) {|new_hash, (k, v)| + new_hash[k.to_sym] = keys_to_symbols(v) + new_hash + } + else + obj + end + end + end +end diff --git a/app/openbel/api/routes/datasets.rb b/app/openbel/api/routes/datasets.rb index 231cbfc..067c275 100644 --- a/app/openbel/api/routes/datasets.rb +++ b/app/openbel/api/routes/datasets.rb @@ -5,9 +5,9 @@ require 'openbel/api/evidence/mongo' require 'openbel/api/evidence/facet_filter' require_relative '../resources/evidence_transform' +require_relative '../helpers/evidence' require_relative '../helpers/filters' require_relative '../helpers/pager' -require_relative '../helpers/translators' module OpenBEL module Routes @@ -176,23 +176,6 @@ def retrieve_dataset(uri) dataset end - - def keys_to_symbols(obj) - case obj - when Array - obj.inject([]) {|new_array, v| - new_array << keys_to_symbols(v) - new_array - } - when Hash - obj.inject({}) {|new_hash, (k, v)| - new_hash[k.to_sym] = keys_to_symbols(v) - new_hash - } - else - obj - end - end end options '/api/datasets' do @@ -339,63 +322,12 @@ def keys_to_symbols(obj) collection_total = @api.count_evidence filtered_total = @api.count_evidence(filters) page_results = @api.find_dataset_evidence(dataset, filters, start, size, faceted, max_values_per_facet) + name = dataset[:identifier].gsub(/[^\w]/, '_') - translator = Translators.requested_translator(request, params) - translator_plugin = Translators.requested_translator_plugin(request, params) - - # Serialize to HAL if they [Accept]ed it, specified it as ?format, or - # no translator was found to match request. - if wants_default? || !translator - evidence = page_results[:cursor].map { |item| - item.delete('facets') - item - }.to_a - - facets = page_results[:facets] - - halt 404 if evidence.empty? - - pager = Pager.new(start, size, filtered_total) - - options = { - :facets => facets, - :start => start, - :size => size, - :filters => filters, - :metadata => { - :collection_paging => { - :total => collection_total, - :total_filtered => pager.total_size, - :total_pages => pager.total_pages, - :current_page => pager.current_page, - :current_page_size => evidence.size, - } - } - } - - # pager links - options[:previous_page] = pager.previous_page - options[:next_page] = pager.next_page - - render_collection(evidence, :evidence, options) - else - extension = translator_plugin.file_extensions.first - - response.headers['Content-Type'] = translator_plugin.media_types.first - status 200 - attachment "#{dataset[:identifier].gsub(/[^\w]/, '_')}.#{extension}" - stream :keep_open do |response| - cursor = page_results[:cursor] - dataset_evidence = cursor.lazy.map { |evidence| - evidence.delete('facets') - evidence.delete('_id') - evidence = keys_to_symbols(evidence) - BEL::Model::Evidence.create(evidence) - } - - translator.write(dataset_evidence, response) - end - end + render_evidence_collection( + name, page_results, start, size, filters, + filtered_total, collection_total + ) end get '/api/datasets' do diff --git a/app/openbel/api/routes/evidence.rb b/app/openbel/api/routes/evidence.rb index 576f334..71146fa 100644 --- a/app/openbel/api/routes/evidence.rb +++ b/app/openbel/api/routes/evidence.rb @@ -3,6 +3,8 @@ require 'openbel/api/evidence/mongo' require 'openbel/api/evidence/facet_filter' require_relative '../resources/evidence_transform' +require_relative '../helpers/evidence' +require_relative '../helpers/filters' require_relative '../helpers/pager' module OpenBEL @@ -163,37 +165,11 @@ def keys_to_s_deep(hash) collection_total = @api.count_evidence() filtered_total = @api.count_evidence(filters) page_results = @api.find_evidence(filters, start, size, faceted, max_values_per_facet) - evidence = page_results[:cursor].map { |item| - item.delete('facets') - item - }.to_a - facets = page_results[:facets] - - halt 404 if evidence.empty? - - pager = Pager.new(start, size, filtered_total) - - options = { - :facets => facets, - :start => start, - :size => size, - :filters => filters, - :metadata => { - :collection_paging => { - :total => collection_total, - :total_filtered => pager.total_size, - :total_pages => pager.total_pages, - :current_page => pager.current_page, - :current_page_size => evidence.size, - } - } - } - - # pager links - options[:previous_page] = pager.previous_page - options[:next_page] = pager.next_page - - render_collection(evidence, :evidence, options) + + render_evidence_collection( + 'evidence-export', page_results, start, size, filters, + filtered_total, collection_total + ) end get '/api/evidence/:id' do