class Faraday::SpecificLogging

Public Class Methods

new(app, options = {}) click to toggle source
Calls superclass method
# File lib/faraday/specific_loging.rb, line 6
def initialize(app, options = {})
  super(app)
  @logger = options[:logger]
  @call_logging  = options[:call_logging] || 'info'
  @target_key = options[:target_key]
  @message = options[:message] || ''
end

Public Instance Methods

call(env) click to toggle source
# File lib/faraday/specific_loging.rb, line 14
def call(env)
  return @app.call(env) if lost_options? || not_post_or_put?(env) || env.body.nil?

  request_body = parse_body(env)

  @logger.send(@call_logging, message: @message, @target_key.to_sym => request_body[@target_key.to_s])

  @app.call(env)
end

Private Instance Methods

lost_options?() click to toggle source
# File lib/faraday/specific_loging.rb, line 26
def lost_options?
  @logger.nil? || @target_key.nil?
end
not_post_or_put?(env) click to toggle source
# File lib/faraday/specific_loging.rb, line 30
def not_post_or_put?(env)
  %i(post put).exclude?(env[:method])
end
parse_body(env) click to toggle source
# File lib/faraday/specific_loging.rb, line 34
def parse_body(env)
  # NOTE Try parse to JSON.
  JSON.load(env.body)
rescue JSON::ParserError, TypeError
  url_encoded_params_to_hash(env.body)
end
url_encoded_params_to_hash(string) click to toggle source
# File lib/faraday/specific_loging.rb, line 41
def url_encoded_params_to_hash(string)
  string.split('&').each_with_object({}) {|str, hs|
    key, value = str.split('=')

    hs[key.to_s] = value
  }
end