<commit_before>require 'dm-core'
require 'dm-validations'

module RequestLumberjack
  
  class LoggedResponse
    class InvalidArgumentError < StandardError; end
    
    include DataMapper::Resource

    property :id,             Serial
    property :request_uri,    String,  :required => true
    property :status,         Integer, :required => true
    property :request_method, String,  :required => true
    property :user_id,        Integer

    class << self
      def create_from_rails_response(response)
        logged_request = LoggedResponse.create(
          :request_uri    => response.request.request_uri,
          :status         => response.status,
          :request_method => response.request.request_method
        )
      end

      def create_from_response(response)
        create_from_rails_response(response) if defined?(RAILS_ENV)
      end
    end

  end

end<commit_msg>added create_from_response_params class method<commit_after>require 'dm-core'
require 'dm-validations'

module RequestLumberjack
  
  class LoggedResponse
    class InvalidArgumentError < StandardError; end
    
    include DataMapper::Resource

    property :id,             Serial
    property :request_uri,    String,  :required => true
    property :status,         Integer, :required => true
    property :request_method, String,  :required => true
    property :user_id,        Integer

    class << self
      def create_from_response_params(params)
        raise InvalidArgumentError unless params.is_a? Hash
        
        LoggedResponse.new(cleanse_params(params)).save
      end
      
      protected
        def cleanse_params(params)
          params.select {|k,v| ['status', 'request_method', 'request_uri'].include? k }
        end
    end

  end

end
{'fraction_non_alphanumeric': 0.07526881720430108, 'fraction_numerical': 0.0, 'mean_word_length': 2.645360824742268, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '2753785', 'n_tokens_mistral': 485, 'n_tokens_neox': 477, 'n_words': 118}