<commit_before># encoding utf-8

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

    before_filter :load_visualization,
                  :check_accessible
    before_filter :load_snapshot, only: [:show, :update, :destroy]

    rescue_from Carto::LoadError,
                Carto::UnauthorizedError,
                Carto::UnprocesableEntityError, with: :rescue_from_carto_error

    def index
      snapshots = State.where(visualization_id: @visualization.id,
                              user_id: current_viewer.try(:id))

      snapshots_presentation = snapshots.map do |snapshot|
        StatePresenter.new(snapshot).to_h
      end

      render json: snapshots_presentation
    end

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

    def create
      State.create!(user_id: current_viewer.try(:id),
                    visualization_id: @visualization.id,
                    json: params[:json])
    rescue ActiveRecord::RecordInvalid => exception
      message = exception.record.errors.full_messages.join(', ')
      raise Carto::UnprocesableEntityError.new(message)
    end

    private

    def load_visualization
      @visualization = Carto::visualization.find(params[:visualization_id])
    rescue ActiveRecord::RecordNotFound
      raise Carto::LoadError.new('Visualization not found')
    end

    def check_accessible<commit_msg>fix typo .to_h -> .to_hash<commit_after># encoding utf-8

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

    before_filter :load_visualization,
                  :check_accessible
    before_filter :load_snapshot, only: [:show, :update, :destroy]

    rescue_from Carto::LoadError,
                Carto::UnauthorizedError,
                Carto::UnprocesableEntityError, with: :rescue_from_carto_error

    def index
      snapshots = State.where(visualization_id: @visualization.id,
                              user_id: current_viewer.try(:id))

      snapshots_presentation = snapshots.map do |snapshot|
        StatePresenter.new(snapshot).to_hash
      end

      render json: snapshots_presentation
    end

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

    def create
      State.create!(user_id: current_viewer.try(:id),
                    visualization_id: @visualization.id,
                    json: params[:json])
    rescue ActiveRecord::RecordInvalid => exception
      message = exception.record.errors.full_messages.join(', ')
      raise Carto::UnprocesableEntityError.new(message)
    end

    private

    def load_visualization
      @visualization = Carto::visualization.find(params[:visualization_id])
    rescue ActiveRecord::RecordNotFound
      raise Carto::LoadError.new('Visualization not found')
    end

    def check_accessible
{'fraction_non_alphanumeric': 0.08431237042156185, 'fraction_numerical': 0.000691085003455425, 'mean_word_length': 2.8911290322580645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '7052469', 'n_tokens_mistral': 834, 'n_tokens_neox': 791, 'n_words': 158}