<commit_before>require 'rack/request'

class RollbarAPI
  def call(env)
    request = Rack::Request.new(env)
    json = JSON.parse(request.body.read)

    return bad_request(json) unless invalid_data?(json)

    success(json)
  end

  protected

  def response_headers
    {
      'Content-Type' => 'application/json'
    }
  end

  def invalid_data?(json)
    !!json['access_token']
  end

  def bad_request(json)
    [400, response_headers, [bad_request_body]]
  end

  def success(json)
    [200, response_headers, [success_body(json)]]
  end

  def bad_request_body
    result(1, nil, 'bad request')
  end

  def success_body(json)
    result(0, {
      :id => rand(1_000_000),
      :uuid => json['data']['uuid']
    }, nil)
  end

  def result(err, body, message)
    {
      :err => err,
      :result => body,
      :message => message
    }.to_json
  end
end
<commit_msg>841: add support for unauthorized access token simulation<commit_after>require 'rack/request'

class RollbarAPI

  UNAUTHORIZED_ACCESS_TOKEN = '<PASSWORD>'

  def call(env)
    request = Rack::Request.new(env)
    json = JSON.parse(request.body.read)

    return unauthorized(json) unless authorized?(json)

    return bad_request(json) unless invalid_data?(json)

    success(json)
  end

  protected

  def authorized?(json)
    json['access_token'] != UNAUTHORIZED_ACCESS_TOKEN
  end

  def response_headers
    {
      'Content-Type' => 'application/json'
    }
  end

  def invalid_data?(json)
    !!json['access_token']
  end

  def unauthorized(json)
    [401, response_headers, [unauthorized_body]]
  end

  def bad_request(json)
    [400, response_headers, [bad_request_body]]
  end

  def success(json)
    [200, response_headers, [success_body(json)]]
  end

  def unauthorized_body
    result(1, nil, 'invalid access token')
  end

  def bad_request_body
    result(1, nil, 'bad request')
  end

  def success_body(json)
    result(0, {
      :id => rand(1_000_000),
      :uuid => json['data']['uuid']
    }, nil)
  end

  def result(err, body, message)
    {
      :err => err,
      :result => body,
      :message => message
    }.to_json
  end
end

{'fraction_non_alphanumeric': 0.12121212121212122, 'fraction_numerical': 0.01724941724941725, 'mean_word_length': 3.1190019193857967, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 16, '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': '4303872', 'n_tokens_mistral': 776, 'n_tokens_neox': 727, 'n_words': 166}