<commit_before>require 'redis'
require 'sinatra'
require 'sinatra/cross_origin'
require 'json'
require 'yaml'

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

WIDGET_PATH = __dir__ + '/widgets/'
Dir.glob(File.expand_path(WIDGET_PATH + "/*.rb", __FILE__)).each {|f| require f }

store = Redis.new

set :bind, '0.0.0.0'

set :allow_origin, :any
set :allow_methods, [:get, :options]
set :allow_credentials, false
set :max_age, "1728000"
set :expose_headers, ['Content-Type']

configure do
  enable :cross_origin
end

def enabled_widgets(store)
  enabled = JSON.parse(store.get("enabled_widgets")) rescue enabled = []
  enabled
end

get '/dashboard' do
  cross_origin
  content_type :json

  enabled = {}
  widget_data = {}

  # get data for all enabled widgets
  enabled_widgets(store).each do |name|
    begin
      widget_data[name] = JSON.parse(store.get("widget_%s" % name))
    rescue
      widget_data[name] = nil
    end
  end

  { dashboard: { items: widget_data } }.to_json
end

post '/widgets/*' do
  begin
    name = request.path.split('/')[-1]
  rescue
    status 400
    return ''
  end

  unless enabled_widgets(store).include? name
    status 404
    return ''
  end

  Object.const_get(name.camelize).new({}, store).update(request.body.read)

  status 204
  ''
end

options "*" do
  response.headers["Allow"] = "GET, OPTIONS"
  response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type, Cache-Control, Accept"
  200
end<commit_msg>Fixed UTF-8 issue in push handler<commit_after>require 'redis'
require 'sinatra'
require 'sinatra/cross_origin'
require 'json'
require 'yaml'

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

WIDGET_PATH = __dir__ + '/widgets/'
Dir.glob(File.expand_path(WIDGET_PATH + "/*.rb", __FILE__)).each {|f| require f }

store = Redis.new

set :bind, '0.0.0.0'

set :allow_origin, :any
set :allow_methods, [:get, :options]
set :allow_credentials, false
set :max_age, "1728000"
set :expose_headers, ['Content-Type']

configure do
  enable :cross_origin
end

def enabled_widgets(store)
  enabled = JSON.parse(store.get("enabled_widgets")) rescue enabled = []
  enabled
end

get '/dashboard' do
  cross_origin
  content_type :json

  enabled = {}
  widget_data = {}

  # get data for all enabled widgets
  enabled_widgets(store).each do |name|
    begin
      widget_data[name] = JSON.parse(store.get("widget_%s" % name))
    rescue
      widget_data[name] = nil
    end
  end

  { dashboard: { items: widget_data } }.to_json
end

post '/widgets/*' do
  begin
    name = request.path.split('/')[-1]
  rescue
    status 400
    return ''
  end

  unless enabled_widgets(store).include? name
    status 404
    return ''
  end

  info = request.body.read.force_encoding("UTF-8")
  Object.const_get(name.camelize).new({}, store).update(info)

  status 204
  ''
end

options "*" do
  response.headers["Allow"] = "GET, OPTIONS"
  response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type, Cache-Control, Accept"
  200
end
{'fraction_non_alphanumeric': 0.13450292397660818, 'fraction_numerical': 0.017543859649122806, 'mean_word_length': 4.047540983606558, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '426560', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1067, 'n_words': 252}