<commit_before>#!/usr/bin/env ruby

require 'rack'
require 'set'
require 'sinatra/base'
require 'sinatra/config_file'
require 'sinatra/reloader'
require 'json'
require_relative 'lib/openbel'

class Namespaces < Sinatra::Base
  include OpenBEL::Namespace

  register Sinatra::ConfigFile
  config_file 'config.yml'

  def initialize
    super
    require './lib/storage/redlander.rb'
    @api = API.new StorageRedlander.new(settings.storage)
  end

  configure :development do
    require 'perftools'
    require 'rack/perftools_profiler'
    use ::Rack::PerftoolsProfiler, :default_printer => 'callgrind'
    use ::Rack::Deflater

    register Sinatra::Reloader

    SPOKEN_CONTENT_TYPES = %w[application/json text/html text/xml]
  end

  get '/namespaces/?' do
    namespaces = @api.find_namespaces
    if not namespaces or namespaces.empty?
      halt 404
    end
    
    render_multiple(request, namespaces.sort { |x,y|
      x.prefLabel.to_s <=> y.prefLabel.to_s
    }, 'All Namespaces')
  end

  get '/namespaces/:namespace/?' do |namespace|
    ns = @api.find_namespace(namespace)
    if not ns
      halt 404
    end

    status 200
    render_single(request, ns, 'Namespace')
  end

  get '/namespaces/:namespace/equivalents/?' do |namespace|
    values = request.params['values'].split(',')
    halt 404 unless values and not values.empty?

    options = {}
    if request.params['namespace']
      options[:target] = request.params['namespace']
    end

    matches = @api.find_equivalents(namespace, values, options)
    
    response.headers['Content-Type'] = 'application/json'
    JSON.dump(matches, response)
  end

  get '/namespaces/:namespace/:id/?' do |namespace, value|
    value = @api.find_namespace_value(namespace, value)
    if not value
      halt 404
    end

    render_single(request, value, 'Namespace Value')
  end

  get '/namespaces/:namespace/:id/equivalents/?' do |namespace, value|
    equivalents = @api.find_equivalent(namespace, value)
    if not equivalents or equivalents.empty?
      halt 404
    end
    <commit_msg>multi-parameter 'value' (side-steps rack)<commit_after>#!/usr/bin/env ruby

require 'rack'
require 'set'
require 'sinatra/base'
require 'sinatra/config_file'
require 'sinatra/reloader'
require 'json'
require 'cgi'
require_relative 'lib/openbel'

class Namespaces < Sinatra::Base
  include OpenBEL::Namespace

  register Sinatra::ConfigFile
  config_file 'config.yml'

  def initialize
    super
    require './lib/storage/redlander.rb'
    @api = API.new StorageRedlander.new(settings.storage)
  end

  configure :development do
    require 'perftools'
    require 'rack/perftools_profiler'
    use ::Rack::PerftoolsProfiler, :default_printer => 'callgrind'
    use ::Rack::Deflater

    register Sinatra::Reloader

    SPOKEN_CONTENT_TYPES = %w[application/json text/html text/xml]
  end

  get '/namespaces/?' do
    namespaces = @api.find_namespaces
    if not namespaces or namespaces.empty?
      halt 404
    end
    
    render_multiple(request, namespaces.sort { |x,y|
      x.prefLabel.to_s <=> y.prefLabel.to_s
    }, 'All Namespaces')
  end

  get '/namespaces/:namespace/?' do |namespace|
    ns = @api.find_namespace(namespace)
    if not ns
      halt 404
    end

    status 200
    render_single(request, ns, 'Namespace')
  end

  get '/namespaces/:namespace/equivalents/?' do |namespace|
    halt 400 unless request.params['value']
    values = CGI::parse(env["QUERY_STRING"])['value']

    options = {}
    if request.params['namespace']
      options[:target] = request.params['namespace']
    end

    matches = @api.find_equivalents(namespace, values, options)
    
    response.headers['Content-Type'] = 'application/json'
    JSON.dump(matches, response)
  end

  get '/namespaces/:namespace/:id/?' do |namespace, value|
    value = @api.find_namespace_value(namespace, value)
    if not value
      halt 404
    end

    render_single(request, value, 'Namespace Value')
  end

  get '/namespaces/:namespace/:id/equivalents/?' do |namespace, value|
    equivalents = @api.find_equivalent(namespace, value)
    if not equivalents or equivalents.empty?
      halt 404
    end
    
{'fraction_non_alphanumeric': 0.11586538461538462, 'fraction_numerical': 0.008653846153846154, 'mean_word_length': 3.4502673796791443, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '880030', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1315, 'n_words': 314}