Sha256: 8ee5e433fd6525ea551496db583c78239965fb00598c6d627bea0bc20cc1248c

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

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 :method, presence: true
  validates :path, presence: true

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

  def formatted_request_body
    formatted_body(request_body)
  end

  def formatted_response_body
    formatted_body(response_body)
  end

  def formatted_body(body)
    if body.is_a?(Hash)
      JSON.pretty_generate(body)
    else
      xml = Nokogiri::XML(body)
      if xml.errors.any?
        body
      else
        xml.to_xml(indent: 2)
      end
    end
  rescue
    body
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_api_logger-0.3.0 lib/rails_api_logger/request_log.rb