Molly Harper
Molly Harper

Reputation: 2453

Best way to handle callback function in Rails

I have a class with four functions: GET, PUT, POST and DELETE. I want to call a log function after every PUT, POST and DELETE function.

I will still need access to the data in the calling function within my log function.

In an ideal world, this is what I want:

class ApiClient
  after :put, :post, :delete log

  def initialize(url, username = nil, password = nil)
    @connection = Faraday.new(url) do |faraday|
      faraday.basic_auth(username, password) if username
      faraday.request :url_encoded
      faraday.response :logger
      faraday.adapter :net_http
      faraday.use Errors::RaiseError
    end
  end

  def get(path, parameter = nil)
    @connection.get path, parameter
  end

  def post(path, data, headers = {})
    @connection.post path, data, headers
  end

  def put(path, data, headers = {})
    @connection.put path, data, headers
  end

  def delete(path)
    @connection.delete path
  end

  def log
    # handle logging here. will need access to the calling function.
  end
end

Upvotes: 1

Views: 189

Answers (2)

tompave
tompave

Reputation: 12402

Why don't you just pass your data as arguments?

def get(path, parameter = nil)
  result = @connection.get path, parameter
  log result, path, parameter
end

def log(data, *args)
end

Edit after reading comment

A different ugly solution is to make the interface private, and pass all calls through method_missing, where you can implement some common logic.
For example:

class ApiClient

  def initialize(url, username = nil, password = nil)
    @connection = Faraday.new(url) do |faraday|
      faraday.basic_auth(username, password) if username
      faraday.request :url_encoded
      faraday.response :logger
      faraday.adapter :net_http
      faraday.use Errors::RaiseError
    end
  end


  private


  def get(path, parameter = nil)
    @connection.get path, parameter
  end

  def post(path, data, headers = {})
    @connection.post path, data, headers
  end

  def put(path, data, headers = {})
    @connection.put path, data, headers
  end

  def delete(path)
    @connection.delete path
  end


  def log(method, data, *args)
    puts "logging #{method}, #{args}"
  end


  def method_missing(meth, *args)
    if respond_to?(meth, true)
      result = send(meth, *args)
      log(meth, result, *args)
      result
    else
      super
    end
  end
end

Upvotes: 0

Rajarshi Das
Rajarshi Das

Reputation: 12320

You can do another way by do not call logger of each function just include the logger module can give you log

module MethodLogger
  def self.included(base)
    methods = base.instance_methods(false) 
    base.class_eval do
      methods.each do |method_name|
        original_method = instance_method(method_name)
        define_method(method_name) do |*args, &block|
          return_value = original_method.bind(self).call(*args, &block)
          Rails.logger.info "<- #{base}##{method_name} #=> #{return_value.inspect}"
          return_value
        end
      end
    end
  end
end

Now In your class

class ApiClient
   include MethodLogger
  #rest of the code

 end

Now see the rails logs

Upvotes: 2

Related Questions