<commit_before>require 'sinatra/base'
require 'sinatra/namespace'
require 'mongoid'
require 'mongoid_auto_increment_id'
require 'will_paginate_mongoid'
require 'mongoid-simple-tags'
require 'mongoid/geospatial'
require 'redcarpet'
require 'carte/server/validators'
require 'carte/server/models'

module Carte
  class Server < Sinatra::Base
    use Rack::Deflater
    register Sinatra::Namespace
    include Carte::Server::Models

    configure do
      set :protection, :except => :path_traversal
      set :views, File.join(File.dirname(__FILE__), 'server/views')
      set :public_folder, 'public'
      set :default, JSON.parse(File.read(File.join(File.dirname(__FILE__), 'shared/default.json')))
      set :carte, {}
      set :script_path, '/app.js'
    end

    helpers do
      def config
        @config ||= settings.default.update(settings.carte)
      end

      def json_data
        request.body.rewind
        JSON.parse(request.body.read)
      end

      def search(params)
        order = (params[:order] && %w(asc desc random).include?(params[:order])) ? params[:order] : 'desc'
        sort = (params[:sort] && %w(title created_at updated_at).include?(params[:sort])) ? params[:sort] : 'updated_at'
        if order == 'random'
          cards = Card.random
        else
          cards = Card.send(order, sort)
        end
        if title = params[:title]
          cards = cards.any_of({title: /#{title}/i})
        end
        if params[:tags]
          tags = params[:tags].split(',')
          cards = cards.tagged_with_all(tags)
        end
        if content = params[:content]
          cards = cards.any_of({content: /#{content}/i})
        end
        cards = cards.paginate(per_page: 9, page: params[:page])
      end

      def markdown2html(markdown)
        renderer = Redcarpet::Render::HTML.new(filter_html:true)
        Redcarpet::Markdown.new(renderer, autolink: true).render(markdown)
      end
    end

    get '/' do
      haml :index
    end

    get '/app.js' do
      cache_control :public
      send_file settings.carte['script_path']
    end

    namespace '/api' do
      get '/cards.xml' do
        @cards = search(params)
        builder :cards
      end
      
      get '/cards.json' do
        cards = search(params)
        if cards.respond_to?(:current_page) && cards.respond_to?(:total_pages)
          current_page = cards.current_page.to_i
          total_pages = cards.total_pages
        end
        cards = cards.map {|card| {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags}}
        {cards: cards, page: {current: current_page, total: total_pages}}.to_json
      end
      
      get '/cards/:title.json' do
        card = Card.where(title: params[:title]).first
        halt 404 if card.nil?
        {card: {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags, lefts: card.lefts(4), rights: card.rights(4)}}.to_json
      end

      post '/cards.json' do
        card = Card.new(json_data.slice('title', 'content', 'tags'))
        if card.save
          status 201
          {card: {id: card.id}}.to_json
        else
          status 400
          {card: {errors: card.errors}}.to_json
        end
      end
 
      put '/cards/:title.json' do
        card = Card.where(title: params[:title]).first
        halt 404 if card.nil?
        card.histories.create!
        if card.update_attributes(json_data.slice('new_title', 'content', 'tags').compact)
          status 201
          {}.to_json
        else
          status 400
          {card: {errors: card.errors}}.to_json
        end
      end
      
      #delete '/cards/:title.json' do
      #  card = Card.where(title: params[:title]).first
      #  halt 404 if card.nil?
      #  card.destroy
      #end
      
      get '/cards/:title/history.json' do
        card = Card.where(title: params[:title]).first
        halt 404 if card.nil?
        {history: card.histories}.to_json
      end

      get '/tags.json' do
        {tags: Card.all_tags}.to_json
      end

      error(404) do
        {}.to_json
      end
    end
  end
end
<commit_msg>update server side<commit_after>require 'sinatra/base'
require 'sinatra/namespace'
require 'mongoid'
require 'mongoid_auto_increment_id'
require 'will_paginate_mongoid'
require 'mongoid-simple-tags'
require 'mongoid/geospatial'
require 'redcarpet'
require 'carte/server/validators'
require 'carte/server/models'

module Carte
  class Server < Sinatra::Base
    use Rack::Deflater
    register Sinatra::Namespace
    include Carte::Server::Models

    configure do
      set :protection, :except => :path_traversal
      set :views, File.join(File.dirname(__FILE__), 'server/views')
      set :public_folder, 'public'
      set :default, JSON.parse(File.read(File.join(File.dirname(__FILE__), 'shared/default.json')))
      set :carte, {}
    end

    helpers do
      def config
        @config ||= settings.default.update(settings.carte)
      end

      def json_data
        request.body.rewind
        JSON.parse(request.body.read)
      end

      def search(params)
        order = (params[:order] && %w(asc desc random).include?(params[:order])) ? params[:order] : 'desc'
        sort = (params[:sort] && %w(title created_at updated_at).include?(params[:sort])) ? params[:sort] : 'updated_at'
        if order == 'random'
          cards = Card.random
        else
          cards = Card.send(order, sort)
        end
        if title = params[:title]
          cards = cards.any_of({title: /#{title}/i})
        end
        if params[:tags]
          tags = params[:tags].split(',')
          cards = cards.tagged_with_all(tags)
        end
        if content = params[:content]
          cards = cards.any_of({content: /#{content}/i})
        end
        cards = cards.paginate(per_page: 9, page: params[:page])
      end

      def markdown2html(markdown)
        renderer = Redcarpet::Render::HTML.new(filter_html:true)
        Redcarpet::Markdown.new(renderer, autolink: true).render(markdown)
      end
    end

    get '/cards.xml' do
      @cards = search(params)
      builder :cards
    end
    
    get '/cards.json' do
      cards = search(params)
      if cards.respond_to?(:current_page) && cards.respond_to?(:total_pages)
        current_page = cards.current_page.to_i
        total_pages = cards.total_pages
      end
      cards = cards.map {|card| {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags}}
      {cards: cards, page: {current: current_page, total: total_pages}}.to_json
    end
    
    get '/cards/:title.json' do
      card = Card.where(title: params[:title]).first
      halt 404 if card.nil?
      {card: {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags, lefts: card.lefts(4), rights: card.rights(4)}}.to_json
    end

    post '/cards.json' do
      card = Card.new(json_data.slice('title', 'content', 'tags'))
      if card.save
        status 201
        {card: {id: card.id}}.to_json
      else
        status 400
        {card: {errors: card.errors}}.to_json
      end
    end
 
    put '/cards/:title.json' do
      card = Card.where(title: params[:title]).first
      halt 404 if card.nil?
      card.histories.create!
      if card.update_attributes(json_data.slice('new_title', 'content', 'tags').compact)
        status 201
        {}.to_json
      else
        status 400
        {card: {errors: card.errors}}.to_json
      end
    end
    
    #delete '/cards/:title.json' do
    #  card = Card.where(title: params[:title]).first
    #  halt 404 if card.nil?
    #  card.destroy
    #end
    
    get '/cards/:title/history.json' do
      card = Card.where(title: params[:title]).first
      halt 404 if card.nil?
      {history: card.histories}.to_json
    end

    get '/tags.json' do
      {tags: Card.all_tags}.to_json
    end

    error(404) do
      {}.to_json
    end
  end
end

{'fraction_non_alphanumeric': 0.12868061646410225, 'fraction_numerical': 0.00776845006891367, 'mean_word_length': 2.3636746734091867, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2838464', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2493, 'n_words': 578}