<commit_before>require 'bel'
require 'multi_json'
require 'cgi'
require 'lib/evidence/facet_filter'

module OpenBEL
  module Routes

    class Evidence < Base
      include OpenBEL::Evidence::FacetFilter

      def initialize(app)
        super
        @api = OpenBEL::Settings["evidence-api"].create_instance
      end

      options '/api/evidence' do
        response.headers['Allow'] = 'OPTIONS,POST,GET'
        status 200
      end

      options '/api/evidence/:id' do
        response.headers['Allow'] = 'OPTIONS,GET,PUT,DELETE'
        status 200
      end

      post '/api/evidence' do
        validate_media_type! "application/json", :profile => schema_url('evidence')

        evidence_obj = read_json
        schema_validation = validate_schema(evidence_obj, :evidence)
        unless schema_validation[0]
          halt 400, schema_validation[1].join("\n")
        end

        evidence = evidence_obj['evidence']
        evidence[:facets] = map_evidence_facets(evidence)
        _id = @api.create_evidence(evidence)

        status 201
        headers "Location" => "#{base_url}/api/evidence/#{_id}"
      end

      get '/api/evidence' do
        start    = (params[:start]  || 0).to_i
        size     = (params[:size]   || 0).to_i
        faceted  = as_bool(params[:faceted])

        filter_hash = {}
        filter_params = CGI::parse(env["QUERY_STRING"])['filter']
        filter_params.each do |filter|
          filter = MultiJson.load(filter)
          halt 400 unless ['category', 'name', 'value'].all? { |f| filter.include? f}
          filter_hash["#{filter['category']}.#{filter['name']}"] = filter['value']
        end

        results  = @api.find_evidence_by_query(filter_hash, start, size, faceted)
        evidence = results[:cursor]
        facets   = results[:facets]

        halt 404 unless evidence.has_next?

        stream_resource_collection(:evidence, evidence, facets,
          :start   => start,
          :size    => size,
          :filters => filter_params,
          :facets  => facets
        )
        status 200
      end

      get '/api/evidence/:id' do
        evidence = @api.find_evidence_by_id(params[:id])
        halt 404 unless evidence
        render(
          [evidence],
          :evidence_resource
        )
      end

      put '/api/evidence/:id' do
        validate_media_type! "application/json", :profile => schema_url('evidence')

        id = params[:id]
        ev = @api.find_evidence_by_id(id)
        halt 404 unless ev

        evidence_obj = read_json
        schema_validation = validate_schema(evidence_obj, :evidence)
        unless schema_validation[0]
          halt 400, schema_validation[1].join("\n")
        end

        evidence = evidence_obj['evidence']
        evidence[:facets] = map_evidence_facets(evidence)
        @api.update_evidence_by_id(id, evidence)

        status 202
      end

      delete '/api/evidence/:id' do
        id = params[:id]
        ev = @api.find_evidence_by_id(id)
        halt 404 unless ev

        @api.delete_evidence_by_id(id)
        status 202<commit_msg>invalid evidence returns 400 with JSON body<commit_after>require 'bel'
require 'multi_json'
require 'cgi'
require 'lib/evidence/facet_filter'

module OpenBEL
  module Routes

    class Evidence < Base
      include OpenBEL::Evidence::FacetFilter

      def initialize(app)
        super
        @api = OpenBEL::Settings["evidence-api"].create_instance
      end

      options '/api/evidence' do
        response.headers['Allow'] = 'OPTIONS,POST,GET'
        status 200
      end

      options '/api/evidence/:id' do
        response.headers['Allow'] = 'OPTIONS,GET,PUT,DELETE'
        status 200
      end

      post '/api/evidence' do
        validate_media_type! "application/json", :profile => schema_url('evidence')

        evidence_obj = read_json
        schema_validation = validate_schema(evidence_obj, :evidence)
        unless schema_validation[0]
          halt(
            400,
            { 'Content-Type' => 'application/json' },
            render_json({ :status => 400, :msg => schema_validation[1].join("\n") })
          )
        end

        evidence = evidence_obj['evidence']
        evidence[:facets] = map_evidence_facets(evidence)
        _id = @api.create_evidence(evidence)

        status 201
        headers "Location" => "#{base_url}/api/evidence/#{_id}"
      end

      get '/api/evidence' do
        start    = (params[:start]  || 0).to_i
        size     = (params[:size]   || 0).to_i
        faceted  = as_bool(params[:faceted])

        filter_hash = {}
        filter_params = CGI::parse(env["QUERY_STRING"])['filter']
        filter_params.each do |filter|
          filter = MultiJson.load(filter)
          halt 400 unless ['category', 'name', 'value'].all? { |f| filter.include? f}
          filter_hash["#{filter['category']}.#{filter['name']}"] = filter['value']
        end

        results  = @api.find_evidence_by_query(filter_hash, start, size, faceted)
        evidence = results[:cursor]
        facets   = results[:facets]

        halt 404 unless evidence.has_next?

        stream_resource_collection(:evidence, evidence, facets,
          :start   => start,
          :size    => size,
          :filters => filter_params,
          :facets  => facets
        )
        status 200
      end

      get '/api/evidence/:id' do
        evidence = @api.find_evidence_by_id(params[:id])
        halt 404 unless evidence
        render(
          [evidence],
          :evidence_resource
        )
      end

      put '/api/evidence/:id' do
        validate_media_type! "application/json", :profile => schema_url('evidence')

        id = params[:id]
        ev = @api.find_evidence_by_id(id)
        halt 404 unless ev

        evidence_obj = read_json
        schema_validation = validate_schema(evidence_obj, :evidence)
        unless schema_validation[0]
          halt(
            400,
            { 'Content-Type' => 'application/json' },
            render_json({ :status => 400, :msg => schema_validation[1].join("\n") })
          )
        end

        evidence = evidence_obj['evidence']
        evidence[:facets] = map_evidence_facets(evidence)
        @api.update_evidence_by_id(id, evidence)

        status 202
      end

      delete '/api/evidence/:id' do
        id = params[:id]
        ev = @api.find_evidence_by_id(id)
        halt 404 unless ev

        @api.delete_evidence_by_id(id)
        status 202
{'fraction_non_alphanumeric': 0.11757425742574257, 'fraction_numerical': 0.01531559405940594, 'mean_word_length': 2.2212257100149477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3672595', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1854, 'n_words': 395}