This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert /api/evidence to BEL using translators
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
Showing
4 changed files
with
117 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters