Sha256: 7048a3ff21d30e4d57bb225620e5af3df509daf7151d5eeb091339ed78a6f743

Contents?: true

Size: 699 Bytes

Versions: 3

Compression:

Stored size: 699 Bytes

Contents

class RequestLog < ActiveRecord::Base
  self.abstract_class = true

  serialize :request_body, JSON
  serialize :response_body, JSON

  belongs_to :loggable, optional: true, polymorphic: true

  scope :failed, -> { where("response_code not like '2%'") }

  validates :request_body, presence: true
  validates :method, presence: true
  validates :path, presence: true

  def self.from_request(request)
    body = (request.body.respond_to?(:read) ? request.body.read : request.body).dup.force_encoding("UTF-8")
    begin
      body = JSON.parse(body) if body.present?
    rescue JSON::ParserError
      body
    end
    create(path: request.path, request_body: body, method: request.method)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_api_logger-0.2.4 lib/rails_api_logger/request_log.rb
rails_api_logger-0.2.3 lib/rails_api_logger/request_log.rb
rails_api_logger-0.2.2 lib/rails_api_logger/request_log.rb