<commit_before>module Spree
  module Api
    module V1
      class BaseController < ActionController::Metal
        include Spree::Api::ControllerSetup

        attr_accessor :current_api_user

        before_filter :set_content_type
        before_filter :check_for_api_key
        before_filter :authenticate_user
        after_filter  :set_jsonp_format

        rescue_from CanCan::AccessDenied, :with => :unauthorized
        rescue_from ActiveRecord::RecordNotFound, :with => :not_found

        helper Spree::Api::ApiHelpers

        def set_jsonp_format
          if params[:callback] && request.get?
            self.response_body = "#{params[:callback]}(#{self.response_body})"
            headers["Content-Type"] = 'application/javascript'
          end
        end

        def map_nested_attributes_keys(klass, attributes)
          nested_keys = klass.nested_attributes_options.keys
          attributes.inject({}) do |h, (k,v)|
            key = nested_keys.include?(k.to_sym) ? "#{k}_attributes" : k
            h[key] = v
            h
          end.with_indifferent_access
        end

        private

        def set_content_type
          content_type = case params[:format]<commit_msg>Add requires_authentication? check for check_for_api_key before_filter in Api::V1::BaseController<commit_after>module Spree
  module Api
    module V1
      class BaseController < ActionController::Metal
        include Spree::Api::ControllerSetup

        attr_accessor :current_api_user

        before_filter :set_content_type
        before_filter :check_for_api_key, :if => :requires_authentication?
        before_filter :authenticate_user
        after_filter  :set_jsonp_format

        rescue_from CanCan::AccessDenied, :with => :unauthorized
        rescue_from ActiveRecord::RecordNotFound, :with => :not_found

        helper Spree::Api::ApiHelpers

        def set_jsonp_format
          if params[:callback] && request.get?
            self.response_body = "#{params[:callback]}(#{self.response_body})"
            headers["Content-Type"] = 'application/javascript'
          end
        end

        def map_nested_attributes_keys(klass, attributes)
          nested_keys = klass.nested_attributes_options.keys
          attributes.inject({}) do |h, (k,v)|
            key = nested_keys.include?(k.to_sym) ? "#{k}_attributes" : k
            h[key] = v
            h
          end.with_indifferent_access
        end

        private

        def set_content_type
          content_type = case params[:format]
{'fraction_non_alphanumeric': 0.0857707509881423, 'fraction_numerical': 0.0011857707509881424, 'mean_word_length': 2.5698166431593794, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '4886595', 'n_tokens_mistral': 708, 'n_tokens_neox': 705, 'n_words': 140}