8e47d6ce64ae5a3392b4d92867b2a83bd0dc9bbeEugen Kuksaclass RouterConstraint
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase def params(request)
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase request.send(:env)["action_dispatch.request.path_parameters"]
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase end
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase
a847d9812b328c048773e705606b10875a929034Eugen Kuksa def set_path_parameters(request, new_params)
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase params = params(request)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa controller = params[:controller]
a847d9812b328c048773e705606b10875a929034Eugen Kuksa action = params[:action]
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa params.except!(*params.keys).merge!(
a847d9812b328c048773e705606b10875a929034Eugen Kuksa controller: controller,
a847d9812b328c048773e705606b10875a929034Eugen Kuksa action: action).merge!(new_params)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa end
c4f318dca9446ec8beb46dc5695d4435f1c12d5bTim Reddehase
c4f318dca9446ec8beb46dc5695d4435f1c12d5bTim Reddehase def add_path_parameters(request, add_params)
c4f318dca9446ec8beb46dc5695d4435f1c12d5bTim Reddehase set_path_parameters(request, params(request).merge(add_params))
c4f318dca9446ec8beb46dc5695d4435f1c12d5bTim Reddehase end
a847d9812b328c048773e705606b10875a929034Eugen Kuksaend
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
8e47d6ce64ae5a3392b4d92867b2a83bd0dc9bbeEugen Kuksaclass FilesRouterConstraint < RouterConstraint
a847d9812b328c048773e705606b10875a929034Eugen Kuksa def matches?(request)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa return false if Repository.find_by_path(request.params[:repository_id]).nil?
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa result = !RepositoryFile.find_with_path(
a847d9812b328c048773e705606b10875a929034Eugen Kuksa params_path_without_format(request)).nil?
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa set_params_path_without_format(request) if result
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa return result
a847d9812b328c048773e705606b10875a929034Eugen Kuksa end
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa protected
a847d9812b328c048773e705606b10875a929034Eugen Kuksa def params_path_without_format(request)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa @params = request.send(:env)["action_dispatch.request.path_parameters"]
a847d9812b328c048773e705606b10875a929034Eugen Kuksa @path = @params[:path]
a847d9812b328c048773e705606b10875a929034Eugen Kuksa @path += ".#{@params[:format]}" if @params[:format]
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa @params.merge({ path: @path }).except(:format)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa end
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa def set_params_path_without_format(request)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa params_path_without_format(request)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa @params.merge!({ path: @path }).except!(:format)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa end
a847d9812b328c048773e705606b10875a929034Eugen Kuksaend
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
0abeb43875687e94f2d551053ad09eebeff1f7c9Tim Reddehaseclass LocIdRouterConstraint < RouterConstraint
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef ELEMENTS_CACHE_CLEARING_INTERVAL = 1.minute
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef @elements_cache_clear_at = Time.now + ELEMENTS_CACHE_CLEARING_INTERVAL
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef @elements_cache = {}
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
d51cee8257d50d1f2d7c0818d968b32096f6b795Tim Reddehase def initialize(find_in_klass, **map)
d51cee8257d50d1f2d7c0818d968b32096f6b795Tim Reddehase @find_in_klass = find_in_klass
d51cee8257d50d1f2d7c0818d968b32096f6b795Tim Reddehase @map = map
d51cee8257d50d1f2d7c0818d968b32096f6b795Tim Reddehase super()
d51cee8257d50d1f2d7c0818d968b32096f6b795Tim Reddehase end
d51cee8257d50d1f2d7c0818d968b32096f6b795Tim Reddehase
0abeb43875687e94f2d551053ad09eebeff1f7c9Tim Reddehase def matches?(request, path = nil)
03ec1d0391beb40e0ae66a73cf99554e1ca6fa15Tim Reddehase path ||= Journey::Router::Utils.
03ec1d0391beb40e0ae66a73cf99554e1ca6fa15Tim Reddehase unescape_uri(request.original_fullpath)
c4cb6bccad9d72c8b284075f716fc9095b3f3be2Tim Reddehase # retrieves the hierarchy and member portions of loc/id's
c4cb6bccad9d72c8b284075f716fc9095b3f3be2Tim Reddehase hierarchy_member = path.split('?', 2).first.split('///', 2).first
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef element =
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef elements_cache[hierarchy_member] ||
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef LocIdBaseModel.find_with_locid(hierarchy_member)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef if element.is_a?(@find_in_klass)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef elements_cache.delete(hierarchy_member)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef clear_elements_cache if elements_cache_clearing_scheduled?
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef assign_path_parameters(request, element)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef true
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef else
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef if !elements_cache.key?(hierarchy_member) && element
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef elements_cache[hierarchy_member] = element
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef end
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef false
0abeb43875687e94f2d551053ad09eebeff1f7c9Tim Reddehase end
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef end
0abeb43875687e94f2d551053ad09eebeff1f7c9Tim Reddehase
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef def elements_cache
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef self.class.instance_variable_get(:@elements_cache)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef end
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef # To keep the memory footprint low in case of unproper removal of cached
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef # elements, we clear the cache periodically. This prevents memory leaks.
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef def clear_elements_cache
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef next_clearing = Time.now + ELEMENTS_CACHE_CLEARING_INTERVAL
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef self.class.instance_variable_set(:@elements_cache_clear_at, next_clearing)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef self.class.instance_variable_set(:@elements_cache, {})
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef end
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef def elements_cache_clearing_scheduled?
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef Time.now > self.class.instance_variable_get(:@elements_cache_clear_at)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef end
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef def assign_path_parameters(request, element)
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef ontology = element.is_a?(Ontology) ? element : element.ontology
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef proof_attempt = element.proof_attempt if @map[:proof_attempt]
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef theorem = element.theorem if @map[:theorem]
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef path_params = {repository_id: ontology.repository.to_param}
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef %i(proof_attempt theorem ontology element).each do |p|
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef path_params[@map[p]] = binding.local_variable_get(p).id if @map[p]
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef end
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef
5a102b3c56d4ac75632c7e9244ee0ce5bdbbf13aSascha Graef add_path_parameters(request, path_params)
0abeb43875687e94f2d551053ad09eebeff1f7c9Tim Reddehase end
0abeb43875687e94f2d551053ad09eebeff1f7c9Tim Reddehaseend
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehaseclass RefLocIdRouterConstraint < LocIdRouterConstraint
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase def matches?(request)
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase params = params(request)
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase result = OntologyVersionFinder.
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase applicable_reference?(params[:reference])
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase path = Journey::Router::Utils.unescape_uri(request.original_fullpath)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase result && update_version_id!(request, path.dup)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase end
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase def update_version_id!(request, path)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase version = OntologyVersionFinder.find(path)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase version_id = version.try(:to_param)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase ontology_id = version.try(:ontology).try(:to_param)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase add_path_parameters(request, id: version_id, ontology_id: ontology_id)
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase !! version_id
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase end
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehaseend
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase
4949048bda09e116ee3627383e831455954cbe41Tim Reddehaseclass MMTRouterConstraint < LocIdRouterConstraint
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase def matches?(request)
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase path = request.original_fullpath.
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase # Convert MMT to standard Loc/Id
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase gsub(/\?+/, '//').
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase # Prune ref-portion
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase sub('/ref/mmt', '')
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase super(request, path)
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase end
4949048bda09e116ee3627383e831455954cbe41Tim Reddehaseend
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase
8e47d6ce64ae5a3392b4d92867b2a83bd0dc9bbeEugen Kuksaclass IRIRouterConstraint < RouterConstraint
511ba3e3d7deb4a13a03ce522a99a0395acc61b7Tim Reddehase def matches?(request, path = nil)
03ec1d0391beb40e0ae66a73cf99554e1ca6fa15Tim Reddehase path ||= Journey::Router::Utils.
03ec1d0391beb40e0ae66a73cf99554e1ca6fa15Tim Reddehase unescape_uri(request.original_fullpath)
03ec1d0391beb40e0ae66a73cf99554e1ca6fa15Tim Reddehase ontology = Ontology.find_with_iri(path)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa result = !ontology.nil?
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa if result
a847d9812b328c048773e705606b10875a929034Eugen Kuksa set_path_parameters(request,
a847d9812b328c048773e705606b10875a929034Eugen Kuksa repository_id: ontology.repository.to_param, id: ontology.id)
a847d9812b328c048773e705606b10875a929034Eugen Kuksa end
a847d9812b328c048773e705606b10875a929034Eugen Kuksa
a847d9812b328c048773e705606b10875a929034Eugen Kuksa return result
a847d9812b328c048773e705606b10875a929034Eugen Kuksa end
a847d9812b328c048773e705606b10875a929034Eugen Kuksaend
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehaseclass RefIRIRouterConstraint < IRIRouterConstraint
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehase def matches?(request)
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehase # remove the ref/:version_number portion from path
03ec1d0391beb40e0ae66a73cf99554e1ca6fa15Tim Reddehase path = Journey::Router::Utils.unescape_uri(request.original_fullpath).
5af5b2ebdc340fa37e797a9d9b4f16ff75d1674bEugen Kuksa sub(%r{\A/ref/\d+/}, '')
1365c7c52c67284d3c305ed7cbc2f22a20de7acfEugen Kuksa
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehase super(request, path)
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehase end
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehaseend
419f986abb76f6fce54b71e17f52a1deaa06dbd6Tim Reddehase
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehaseclass MIMERouterConstraint < RouterConstraint
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase attr_accessor :mime_types
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase def initialize(*mime_types)
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase self.mime_types = mime_types.flatten.map { |m| Mime::Type.lookup(m) }
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase super()
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase end
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase
80021fb6bde14c39046950cdbaaadf767d173f94Tim Reddehase # In some cases request.accepts == [nil] (e.g. cucumber tests),
80021fb6bde14c39046950cdbaaadf767d173f94Tim Reddehase # in these cases we will default to true.
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase def matches?(request)
80021fb6bde14c39046950cdbaaadf767d173f94Tim Reddehase highest_mime = request.accepts.first
80021fb6bde14c39046950cdbaaadf767d173f94Tim Reddehase highest_mime ? mime_types.any? { |m| highest_mime == m } : true
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase end
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehaseend
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehaseclass GroupedConstraint
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase attr_accessor :constraints
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase def initialize(*args)
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase self.constraints = args.flatten
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase end
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase def matches?(request)
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase constraints.all? { |c| c.matches?(request) }
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehase end
d9a3935fa80ce492e782d17ec22825d1708dea97Tim Reddehaseend