<commit_before>#!/usr/bin/env ruby
require 'sinatra'
require 'json'

class LiveTestServer < Sinatra::Base

  get '/' do
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Root'
    <<-EOT
      { "name": "whatever API",
        "_links": {
          "curies": [{
            "name": "whatever",
            "templated": true,
            "href": "/rels{?rel}"
          }],
          "self": {"href":"/"},
          "whatever:widgets": {"href":"/widgets"},
          "whatever:slow_widgets": {"href":"/slow_widgets"}
        }
      }
    EOT
  end

  get '/widgets' do
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=WidgetSet'
    <<-EOT
      { "name": "My Widgets",
        "_links": {
          "curies": [{
            "name": "whatever",
            "templated": true,
            "href": "/rels{?rel}"
          }],
          "self": {"href":"/widgets"},
          "whatever:root": {"href":"/"}
        },
        "_embedded": {
          "widgets": [
            { "name": "Widget 1",
              "_links": {
                "curies": [{
                  "name": "whatever",
                  "templated": true,
                  "href": "/rels{?rel}"
                }],
                "self": {"href": "/widgets/1"},
                "whatever:widgets": {"href": "/widgets"}
              }
            }
          ]
        }
      }
    EOT
  end

  put '/widgets/1' do
    params = JSON.parse(request.env["rack.input"].read)
    if params["name"] != 'Awesome Widget dood'
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Error'
      [422, JSON.dump({:error => "Name was wrong; you sent #{params.inspect}"})]
    else
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Widget'
      <<-EOT
        { "name": "#{params["name"]}",
          "_links": {
            "curies": [{
              "name": "whatever",
              "templated": true,
              "href": "/rels{?rel}"
            }],
            "self": {"href": "/widgets/1"},
            "whatever:widgets": {"href": "/widgets"}
          }
        }
      EOT
    end
  end

  post '/widgets' do
    params = JSON.parse(request.env["rack.input"].read)
    if params["name"] != 'Cool Widget brah'
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Error'
      [422, JSON.dump(:error => "Name was wrong; you sent #{params.inspect}")]
    else
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Widget'
      [201, headers, <<-EOT ]
        { "name": "#{params["name"]}",
          "_links": {
            "curies": [{
              "name": "whatever",
              "templated": true,
              "href": "/rels{?rel}"
            }],
            "self": {"href":"/widgets/2"},
            "whatever:widgets": {"href": "/widgets"},
            "whatever:root": {"href":"/"}
          }
        }
      EOT
    end
  end

  delete '/widgets/1' do
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Message'
    <<-EOT
      { "message": "Deleted widget.",
        "_links": {
          "curies": [{
            "name": "whatever",
            "templated": true,
            "href": "/rels{?rel}"
          }],
          "self": {"href":"/widgets/1"},
          "whatever:widgets": {"href": "/widgets"},
          "whatever:root": {"href":"/"}
        }
      }
    EOT
  end

  # To test short timeouts
  get '/slow_widgets' do
    sleep 2
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=WidgetSet'
    <<-EOT
      { "name": "My Slow Widgets",
        "_links": {<commit_msg>Clarify slow_widget type in header<commit_after>#!/usr/bin/env ruby
require 'sinatra'
require 'json'

class LiveTestServer < Sinatra::Base

  get '/' do
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Root'
    <<-EOT
      { "name": "whatever API",
        "_links": {
          "curies": [{
            "name": "whatever",
            "templated": true,
            "href": "/rels{?rel}"
          }],
          "self": {"href":"/"},
          "whatever:widgets": {"href":"/widgets"},
          "whatever:slow_widgets": {"href":"/slow_widgets"}
        }
      }
    EOT
  end

  get '/widgets' do
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=WidgetSet'
    <<-EOT
      { "name": "My Widgets",
        "_links": {
          "curies": [{
            "name": "whatever",
            "templated": true,
            "href": "/rels{?rel}"
          }],
          "self": {"href":"/widgets"},
          "whatever:root": {"href":"/"}
        },
        "_embedded": {
          "widgets": [
            { "name": "Widget 1",
              "_links": {
                "curies": [{
                  "name": "whatever",
                  "templated": true,
                  "href": "/rels{?rel}"
                }],
                "self": {"href": "/widgets/1"},
                "whatever:widgets": {"href": "/widgets"}
              }
            }
          ]
        }
      }
    EOT
  end

  put '/widgets/1' do
    params = JSON.parse(request.env["rack.input"].read)
    if params["name"] != 'Awesome Widget dood'
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Error'
      [422, JSON.dump({:error => "Name was wrong; you sent #{params.inspect}"})]
    else
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Widget'
      <<-EOT
        { "name": "#{params["name"]}",
          "_links": {
            "curies": [{
              "name": "whatever",
              "templated": true,
              "href": "/rels{?rel}"
            }],
            "self": {"href": "/widgets/1"},
            "whatever:widgets": {"href": "/widgets"}
          }
        }
      EOT
    end
  end

  post '/widgets' do
    params = JSON.parse(request.env["rack.input"].read)
    if params["name"] != 'Cool Widget brah'
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Error'
      [422, JSON.dump(:error => "Name was wrong; you sent #{params.inspect}")]
    else
      headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Widget'
      [201, headers, <<-EOT ]
        { "name": "#{params["name"]}",
          "_links": {
            "curies": [{
              "name": "whatever",
              "templated": true,
              "href": "/rels{?rel}"
            }],
            "self": {"href":"/widgets/2"},
            "whatever:widgets": {"href": "/widgets"},
            "whatever:root": {"href":"/"}
          }
        }
      EOT
    end
  end

  delete '/widgets/1' do
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=Message'
    <<-EOT
      { "message": "Deleted widget.",
        "_links": {
          "curies": [{
            "name": "whatever",
            "templated": true,
            "href": "/rels{?rel}"
          }],
          "self": {"href":"/widgets/1"},
          "whatever:widgets": {"href": "/widgets"},
          "whatever:root": {"href":"/"}
        }
      }
    EOT
  end

  # To test short timeouts
  get '/slow_widgets' do
    sleep 2
    headers['Content-type'] = 'application/vnd.example.v1+hal+json;type=SlowWidgetSet'
    <<-EOT
      { "name": "My Slow Widgets",
        "_links": {
{'fraction_non_alphanumeric': 0.19542904064595593, 'fraction_numerical': 0.006842753524018065, 'mean_word_length': 1.7217877094972067, 'pattern_counts': {'":': 140, '<': 29, '<?xml version=': 0, '>': 7, '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': '7124256', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2261, 'n_words': 418}