Skip to content

Commit

Permalink
Merge pull request #44 from NYULibraries/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
hab278 committed Sep 22, 2015
2 parents 5b151c8 + 60ab6ce commit fefc430
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
58 changes: 36 additions & 22 deletions app/controllers/ex_cite/export_citations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ class ExportCitationsController < ActionController::Base
# There must be a destination format, or else this whole thing doesnt make sense
before_filter :valid_to_format?
layout "ex_cite/application"

# Sends bad request if there is no destination format
def valid_to_format?
head :bad_request unless to_format
end

# Checks to see if destination format is valid and stores it in a class variable
def to_format
@to_format ||= whitelist_formats :to, params[:to_format]
end
private :to_format

# Constructs an array containing all the citations
def citations
unless defined? @citations
Expand All @@ -30,56 +30,59 @@ def citations
@citations << open_url_citation
end
end
@citations.compact
@citations.compact
end

def record_citation
(params[:id].nil?) ? [] :
params[:id].collect do |id|
record = ExCite.acts_as_citable_class.find_by_id id if ExCite.acts_as_citable_class.respond_to? :find_by_id
record = ExCite.acts_as_citable_class.find_by_id id if ExCite.acts_as_citable_class.respond_to? :find_by_id
(record.nil?) ? (raise(ArgumentError, "This ID cannot be found.")) : record
end
end

# Constructs new citation objects with only the citation key set, returns an array
def resource_citation
(params[:resource_key].nil?) ? [] :
params[:resource_key].collect do |key|
citation = ExCite.acts_as_citable_class.new()
citation.resource_key = key
citation
end
resources = []
return resources if params[:resource_key].nil?
resources << Rails.cache.fetch(params[:resource_key])
resources.flatten!
resources.collect do |key|
citation = ExCite.acts_as_citable_class.new()
citation.resource_key = key
citation
end
end

# Constructs new citation objects with data and source format set (the citation key is constructed automatically), returns an array
def format_citation
(params[:from_format].nil? || params[:data].nil?) ? [] :
params[:from_format].collect.with_index do |format, index|
ExCite.acts_as_citable_class.new ExCite.acts_as_citable_class.data_field.to_sym => params[:data].to_a[index], ExCite.acts_as_citable_class.format_field.to_sym => (whitelist_formats :from, format)
end
end

# Returns a single citation object with data and format set as the url and openurl respectively
def open_url_citation
ExCite.acts_as_citable_class.new ExCite.acts_as_citable_class.data_field.to_sym => CGI::unescape(request.protocol+request.host_with_port+request.fullpath), ExCite.acts_as_citable_class.format_field.to_sym => (whitelist_formats :from, 'openurl')
end

# Maps the output and caches it, alternatively it fetches the already cached result. Seperates each output with two new lines.
# Raises an argument error if any error is caught in mapping (usually the formats are messed up)
def map
@output ||= citations.collect { |citation| Rails.cache.fetch(citation.resource_key+to_format) { citation.send(to_format) } }.join_and_enclose *delimiters
rescue Exception => exc
raise ArgumentError, "#{exc}\n Data or source format not provided and/or mismatched. [citations => #{citations}, to_format => #{@to_format}] "
end

# Maps then decides wether its a push request or a download, catches all bad argument errors
def index
map
serve
rescue ArgumentError => exc
handle_invalid_arguments exc
end

# Pushes to a web service if that is what was requested else it downloads
def serve
@push_to ? push : download
Expand All @@ -105,7 +108,7 @@ def whitelist_formats direction, format
return "#{direction.to_s}_#{@to_format}"
end
end

# For debugging purposes prints out the error. Also sends bad request header
def handle_invalid_arguments exc
logger.debug exc
Expand Down Expand Up @@ -135,9 +138,20 @@ def download
def callback
# Starts with current url minus the querystring..
callback = "#{export_citations_url.gsub(/https?/, @push_to.callback_protocol.to_s)}?"
citations.collect do |citation|
resource_keys = []
citations.each do |citation|
if !citation.respond_to? :new_record || citation.new_record?
resource_keys << citation.resource_key
end
end
unless resource_keys.empty?
resource_key = Digest::SHA1.hexdigest(resource_keys.sort.join)
Rails.cache.write(resource_key,resource_keys)
callback += "resource_key=#{resource_key}&"
end
citations.each do |citation|
# then adds a resource key for each cached resource
callback += (!citation.respond_to? :new_record || citation.new_record?) ? "resource_key[]=#{citation.resource_key}&" : "id[]=#{citation.id}&"
callback += (!citation.respond_to? :new_record || citation.new_record?) ? "" : "id[]=#{citation.id}&"
end
# and finally the to format
callback += "to_format=#{@to_format.formatize}"
Expand All @@ -162,7 +176,7 @@ def filename
def render_push
render :layout => false, :template => @push_to.template
end

def delimiters
case @to_format
when "to_easybib", "to_csl"
Expand Down
1 change: 1 addition & 0 deletions ex_cite.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "simplecov-rcov"
s.add_development_dependency "test-unit"
s.add_development_dependency "coveralls"
s.add_development_dependency "pry"
end
2 changes: 1 addition & 1 deletion lib/ex_cite/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ExCite
VERSION = "2.0.0"
VERSION = "2.1.0"
end
4 changes: 2 additions & 2 deletions test/functional/ex_cite/export_citations _controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def clear
ExCite.acts_as_citable_class = citable_class
ExCite.endnote.protocol = :https
get :index, :to_format => "endnote", :use_route => :export_citations
assert_redirected_to "http://www.myendnoteweb.com/?func=directExport&partnerName=Primo&dataIdentifier=1&dataRequestUrl=https%3A%2F%2Ftest.host%2Fex_cite%2Fexport_citations%3Fresource_key%5B%5D%3Dcc141d92caee81bd0601a5ee365fdf9ec31d23bb%26to_format%3Dris"
assert_redirected_to "http://www.myendnoteweb.com/?func=directExport&partnerName=Primo&dataIdentifier=1&dataRequestUrl=https%3A%2F%2Ftest.host%2Fex_cite%2Fexport_citations%3Fresource_key%3D5b174a9f0bbae8e128c8fd3c5c74b22c5a772cfd%26to_format%3Dris"
ExCite.endnote.callback_protocol = :http
clear
initialize_cite
Expand All @@ -84,7 +84,7 @@ def clear
$acts_as_citable_classes.each do |citable_class|
ExCite.acts_as_citable_class = citable_class
get :index, :to_format => "endnote", :use_route => :export_citations
assert_redirected_to "http://www.myendnoteweb.com/?func=directExport&partnerName=Primo&dataIdentifier=1&dataRequestUrl=http%3A%2F%2Ftest.host%2Fex_cite%2Fexport_citations%3Fresource_key%5B%5D%3Dcc141d92caee81bd0601a5ee365fdf9ec31d23bb%26to_format%3Dris"
assert_redirected_to "http://www.myendnoteweb.com/?func=directExport&partnerName=Primo&dataIdentifier=1&dataRequestUrl=http%3A%2F%2Ftest.host%2Fex_cite%2Fexport_citations%3Fresource_key%3D5b174a9f0bbae8e128c8fd3c5c74b22c5a772cfd%26to_format%3Dris"
clear
initialize_cite
end
Expand Down

0 comments on commit fefc430

Please sign in to comment.