<commit_before>require 'json'
require 'erb'

class JsonHelper
  def initialize(app)
    @app = app
  end

  # /api以下にアクセスするとレスポンスヘッダを書き換え
  def call(env)
    unless (match = %r(\A/api/(.*)\Z).match(env['REQUEST_PATH']))
      return @app.call(env)
    end

    # webpack-dev-serverからアクセスしてくるためCROS対応
    header = {
      'Content-Type'                     => 'application/json;charset=UTF-8',
      'Access-Control-Allow-Origin'      => 'http://localhost:3001',
      'Access-Control-Allow-Credentials' => 'true',
      'Access-Control-Allow-Headers'     => 'X-PINGOTHER, Content-Type'
    }

    if env['REQUEST_METHOD'] == 'GET'
      path = "./data/#{match[1]}.json.erb"
    elsif env['REQUEST_METHOD'] == 'OPTIONS'
      return [200, header, [{}.to_json]]
    else
      path = "./data/#{match[1]}_post.json.erb"
    end

    unless File.exist?(path)
      puts "#{path} not found"
      return [404, header, [{}.to_json]]
    end
    File.open(path) do |file|
      erb          = ERB.new(File.read(path))
      erb.filename = path
      body         = erb.result(binding)
      obj          = JSON.parse(body)<commit_msg>add comment<commit_after>require 'json'
require 'erb'

class JsonHelper
  def initialize(app)
    @app = app
  end

  # /api以下にアクセスするとレスポンスヘッダを書き換え
  def call(env)
    unless (match = %r(\A/api/(.*)\Z).match(env['REQUEST_PATH']))
      return @app.call(env)
    end

    # webpack-dev-serverからアクセスしてくるためCROS対応
    header = {
      'Content-Type'                     => 'application/json;charset=UTF-8',
      'Access-Control-Allow-Origin'      => 'http://localhost:3001',
      'Access-Control-Allow-Credentials' => 'true',
      'Access-Control-Allow-Headers'     => 'X-PINGOTHER, Content-Type'
    }

    if env['REQUEST_METHOD'] == 'GET'
      path = "./data/#{match[1]}.json.erb"

      # CROSの場合は最初に確認通信が来るので返す
    elsif env['REQUEST_METHOD'] == 'OPTIONS'
      return [200, header, [{}.to_json]]
    else
      path = "./data/#{match[1]}_post.json.erb"
    end

    unless File.exist?(path)
      puts "#{path} not found"
      return [404, header, [{}.to_json]]
    end
    File.open(path) do |file|
      erb          = ERB.new(File.read(path))
      erb.filename = path
      body         = erb.result(binding)
      obj          = JSON.parse(body)
{'fraction_non_alphanumeric': 0.16243432574430824, 'fraction_numerical': 0.01138353765323993, 'mean_word_length': 2.3455344070278183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6582089', 'n_tokens_mistral': 871, 'n_tokens_neox': 844, 'n_words': 139}