<commit_before># encoding utf-8

module Carto
  module Api
    class SnapshotsController < ::Api::ApplicationController
      include ControllerHelper

      ssl_required :index, :show, :create, :update, :destroy

      before_filter :load_visualization,
                    :check_visualization_viewable
      before_filter :load_snapshot,
                    :owners_only, only: [:show, :update, :destroy]

      rescue_from LoadError,
                  UnauthorizedError,
                  UnprocesableEntityError, with: :rescue_from_carto_error

      def index
        snapshots = @visualization.snapshots.where(user_id: current_viewer.id)

        presentation = snapshots.map do |snapshot|
          SnapshotPresenter.new(snapshot).to_hash
        end

        render json: presentation
      end

      def show
        render json: SnapshotPresenter.new(@snapshot).to_hash
      end

      def create
        snapshot = Snapshot.create!(user_id: current_viewer.try(:id),
                                    visualization_id: @visualization.id,
                                    state: params[:state])

        render json: SnapshotPresenter.new(snapshot).to_hash, status: :created
      rescue ActiveRecord::RecordInvalid => exception
        message = exception.record.errors.full_messages.join(', ')
        raise UnprocesableEntityError.new(message)
      end

      def update<commit_msg>no need to defend against nil current_viewer<commit_after># encoding utf-8

module Carto
  module Api
    class SnapshotsController < ::Api::ApplicationController
      include ControllerHelper

      ssl_required :index, :show, :create, :update, :destroy

      before_filter :load_visualization,
                    :check_visualization_viewable
      before_filter :load_snapshot,
                    :owners_only, only: [:show, :update, :destroy]

      rescue_from LoadError,
                  UnauthorizedError,
                  UnprocesableEntityError, with: :rescue_from_carto_error

      def index
        snapshots = @visualization.snapshots.where(user_id: current_viewer.id)

        presentation = snapshots.map do |snapshot|
          SnapshotPresenter.new(snapshot).to_hash
        end

        render json: presentation
      end

      def show
        render json: SnapshotPresenter.new(@snapshot).to_hash
      end

      def create
        snapshot = Snapshot.create!(user_id: current_viewer.id,
                                    visualization_id: @visualization.id,
                                    state: params[:state])

        render json: SnapshotPresenter.new(snapshot).to_hash, status: :created
      rescue ActiveRecord::RecordInvalid => exception
        message = exception.record.errors.full_messages.join(', ')
        raise UnprocesableEntityError.new(message)
      end

      def update
{'fraction_non_alphanumeric': 0.0738255033557047, 'fraction_numerical': 0.0007064641469445425, 'mean_word_length': 2.2739884393063585, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809028', 'n_tokens_mistral': 760, 'n_tokens_neox': 731, 'n_words': 163}