Go back to the main page

Create a Ruby Gem. Real World, play by play. Part 3

This article is over 2 years old. Proceed with caution.

Regards ♨ – Minimul



Code from screencast and tutorial is available on Github.

Part 3 of the Gem creation tutorial that follows the conception of a real Gem.

I left off in Part 2 creating a way to configure the persistent locations of the Intuit company id and OAuth information. In Part 3, I need to wire up the oauth_client method to retrieve the configuration settings. As in Part 2, these objectives will pose some interesting challenges.

  1. Back in base.rb I create 3 methods: token, retrieve, and send_chain to handle acquiring persistent information.
  2. One of the first difficulties I run into is that I need to be able to handle configuration settings of many association depths. (e.g. @account.settings.qb.token)
    The retrieve method returns a string back from the configuration code (say "settings.qb_token"). It then splits the string into an array ([ 'settings', 'qb_token' ]). The array is fed into the send_chain method that will create a 'send_chain' if the array is size is greater than 1. (e.g. @account.send('settings').send('qb_token')).

    Hey Minimul, that is a lot of code with no tests? Where are the tests?

    Minimul says —

    There coming next but let me say that I mostly write tests or specs last, not first. I design and architect the application code first. Then I write automated tests to confirm my design. I also generally do not write many finely granular tests but write at a "courser" or higher level, testing many methods in one spec.

    I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence. — Kent Beck
    If you find your testers splitting up functions to support the testing process, you’re destroying your system architecture and code comprehension along with it. Test at a coarser level of granularity. — James O Coplien
  3. Next, I want to start testing my design but I need to go back to the configuration.rb code as I haven't verified that it does what it is supposed to do.
  4. I start running the specs at the 7:05 mark in the screencast.
    To spec the retrieve method, I need to test the configuration code. This first spec passes.
  5. Next, I spec the default configuration values, which are 2 association levels deep. (settings.qb_token, settings.qb_secret, etc)
  6. This spec exercises and asserts the send_chain method, which handles the tricky demands of an association level many layers rich.
  7. The previous step's spec passes. Lastly, I test a 1-level-deep association.
  8. With that I am done testing the configuration and retrieve code.
  9. I modify the oauth_client method to use the new retrieve and oauth_consumer methods.
  10. You may notice that I got rid of the token method and am just using the retrieve method directly e.g. (retrieve('token'))
    The oauth_client modifications and testing start at the 13:55 mark in the screencast.
  11. I go back to finish the create_service_for method now that the oauth_client method is completed.
  12. Now I am ready to test create_service_for.
  13. I spec create_service_for, which will also test the oauth_client method.
  14. I create a single test to accomplish this. In my book, the less test code the better.

Great, finished and verified the 'oauth_client' and 'create_service_for' methods. This is a good place to stop for Part 3.

In the next installment I will finish and publish the Gem. Please note that the last screencast is already completed so check it out:

Comment on this article?