Posts

  • JSON API error handling with ruby

    Hello folks, after being inactive for a while I am finally back!
    This post is about a technique that I found useful for general error handling with REST API and ruby on rails, but the concept works for any programming language/framework. Most of the time programmers handles api errors without a general pattern, that’s bad because it’s obvious that is more error prone, by using a general pattern you can also do some interestic automatic operations such as error logging and messaging to a monitoring service. The base of the approach that i’ve choose is handling errors with Exceptions(that’s what they are made for actually). What we do is create a general exception handler that given an exception name and data will return a particular error response as json depending on which kind of exception was thrown. The basic idea is to configure the error handler with and exception name associated to a configuration hash. In order to make things smoother and be as DRY as possible I’ve made a ruby gem and pushed it to rubygems, if you are interested you can take a look at the docs here

  • Solve problems gracefully with dynamic method generation in ruby

    Days ago i was writing an Rspec macro to gracefully handle authenticated api via a token. At the start i begun creating a couple of methods, each for every Rest verb: module RequestMacros def get_authorized(uri, user) get uri, nil, {‘X-Api-Token’ => user.api_token} end

      def post_authorized(uri, data, user, headers = {})
    	post uri, data, headers.merge({'X-Api-Token' => user.api_token})
      end <!-- more -->
    
  • Ruby: Dynamically create callbacks with metaprogramming

    Hello guys, in this post I’ll explain you how you can handle callbacks with metaprogramming on ruby on rails. But before going deeper into detail you should ask me the reason of that: why shall you use callbacks instead of using general oop techniques? For example in a classic oop design given that you have:

  • Testing for custom Rails validators with Rspec and metaprogramming

    Hello guys, some days ago I’ve made a custom validator for Rails and I wanted to test that it was used correctly in my model (which uses ActiveModel::Model). As a brief preface you have to know that to test for the common Rails validators you can use the shoulda matchers library. But in my case what I’ve done is to unit test the validator (I won’t discuss about that) and then I’ve made a custom matcher to verify that the given classes uses the validator correctly. In order to test for the validator presence in the model I’ve leveraged Ruby metaprogramming. Here is the code where the custom validator is used:

  • Devise remote authentication with rails 4.2

    Hello guys, I’ve been trying to make remote authentication working with devise and i found this useful post: devise remote authentication. The problem is that the post example wasn’t working correctly with new devise versions (3.4.x); In this post I’ll explain you the changes that you need to do to make it work with devise 3.4. Note: Before reading the following part read the other article. The problem are the changes to Devise Authenticatable class regarding the serialization methods. In order to make it work you need to use the following code in your RemoteAuthenticatable Strategy:

subscribe via RSS