Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
convert /api/evidence to BEL using translators
Browse files Browse the repository at this point in the history
factored out rendering of evidence_resource_collection to evidence
helper

refs #44
  • Loading branch information
Anthony Bargnesi committed Feb 2, 2016
1 parent 3500811 commit 1b4dbb7
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 105 deletions.
17 changes: 17 additions & 0 deletions app/openbel/api/helpers/base.rb
Original file line number Diff line number Diff line change
@@ -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
87 changes: 87 additions & 0 deletions app/openbel/api/helpers/evidence.rb
Original file line number Diff line number Diff line change
@@ -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
80 changes: 6 additions & 74 deletions app/openbel/api/routes/datasets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 7 additions & 31 deletions app/openbel/api/routes/evidence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1b4dbb7

Please sign in to comment.