SOAP with Ruby

SOAP (Simple Object Access Protocol) is a protocol specification for exchanging structured information in the implementation of web services in computer networks. Its purpose is to induce extensibility, neutrality and independence. It uses XML Information Set for its message format, and relies on application layer protocols, most often Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission. – Wikipedia

How to make SOAP calls in ruby

One of the best ways to make SOAP calls will be using the library Savon. Its very easy to get started.

If your service provider has WSDL (Web Services Description Language) document. You can also host the WSDL file in your local machine or deploy along with the source code. Here is sample WSDL file if you wanna have a look. Basically this document consists of all the operations supported, format of the response, the endpoint URL of the services and other details.  Continue reading

Rails : Make test suite with HTTP requests run faster

I was developing a movie sorting ruby application which will fetch data from IMDB database. Unfortunately the gem imdb I have been using to fetch data from IMDB did not have any solution inbuilt to deal with HTTP requests in test mode. My test suite was insanely slow.

Then, I found that the gem imdb uses a gem called fakeweb to fake the HTTP request. fakeweb requires you to know every URL your suite invokes to fake it. Its quite cumbersome, because you may not always figure which URLs the suite is invoking. So, it was a bad idea for me.

Then I thought about the gem vcr which I found to be an awesome tool to record all your HTTP request’s responses as fixtures for the future requests.

require 'rubygems'
require 'test/unit'
require 'vcr'

VCR.configure do |config|
  config.cassette_library_dir = "fixtures/vcr_cassettes"
  config.hook_into :webmock # or :fakeweb

class VCRTest < Test::Unit::TestCase
  def test_example_dot_com
    VCR.use_cassette("synopsis") do
      response = Net::HTTP.get_response(URI(''))
      assert_match /Example domains/, response.body

Every HTTP request you or your code make inside the code_block passed to `VCR.use_cassette(“synopsis”)` will stubbed with the fixture data if present. If not, it will fetch once and use the same fixture data for future requests.



Ruby : Passing data to including module

Sometimes but not often in programming career in Ruby, you need to pass some sort of data to the module that is being included into your class. Firstly, I wondered how would I do that. Luckly Ruby is the only language ( I think) where you can do anything to solve problems.

The only limitation is your imagination and the Language never stops you doing anything. Since you can modify anything,  you end up with your own version of Ruby which you have created using Metaprogramming. Continue reading

Rails : Could not find jwt-1.5.3 in any of the sources

I have been using Capistrano for a long time for this project. This time when I tried to deploy my latest changes I stumbled upon this issue. So I documented this.


(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as bundle exit status: 7
bundle stdout: Could not find jwt-1.5.3 in any of the sources
bundle stderr: Regenerating ruby-2.2.1 wrappers........
Regenerating ruby-2.2.1 wrappers........
/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.11.2/lib/bundler/vendor/thor/lib/thor/shell/basic.rb:356: warning: Insecure world writable dir /usr/local/rvm/gems/ruby-2.2.1/bin in PATH, mode 042777

SSHKit::Command::Failed: bundle exit status: 7
bundle stdout: Could not find jwt-1.5.3 in any of the sources
bundle stderr: Regenerating ruby-2.2.1 wrappers........
Regenerating ruby-2.2.1 wrappers........
/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.11.2/lib/bundler/vendor/thor/lib/thor/shell/basic.rb:356: warning: Insecure world writable dir /usr/local/rvm/gems/ruby-2.2.1/bin in PATH, mode 042777

Tasks: TOP => deploy:updated => bundler:install
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as deployer@ bundle exit status: 7
bundle stdout: Could not find jwt-1.5.3 in any of the sources
bundle stderr: Regenerating ruby-2.2.1 wrappers........


The reason is:

jwt 1.5.3 is yanked because of some reasons. So 1.5.3 version isn’t in repository..!

How to solve

Delete your Gemfile.lock and

$ bundle install

update your remote repo and deploy again


Ruby on Rails : Forman : Passing Environment variables and secrets

With foreman you can use .env files to declare the environment variables for your app (and it’s dependencies) and maintain them isolated from the rest of your environment. So, if you’re using theaws-sdk gem, your .env file would look like:


And foreman automatically loads the .env file that is at the same directory as your Procfile.

This way you can make all environment specific configuration for your app to live at this .env file and let every developer set their own specific configurations here. All variables declared here will be available for all processes started by foreman as environment variables.

Continue reading

Ruby : Evil of Inheritance

As you know that `access modifiers` in ruby  are concerned with how a message is passed rather than who is receiving the message. See my previous blog post for more info about access modifiers in ruby.

The main problem is, there is no proper encapsulation from ancestors to child inheriting them. Every piece of code that parent has is redefined in child as well. So problems like below might occur frequently Continue reading

Getting Started with Google Analytics

First of all I assume you know why you are interested in Google Analytics Service.

Usage Guide:

Adding analytics.js to Your Site

The analytics.js library is a JavaScript library for measuring how users interact with your website. This document explains how to add analytics.js to your site.


Now put the following js code snippet in the <head> section of your pages. Continue reading

Ruby : Public, Private, Protected simplified

Like many parts of Ruby that look like special language features, Ruby’s privacy key-
words are actually methods. Yes access modifiers are simple method calls, they don’t create a new scope. public, private and protected are really methods, so they can take parameters. If you pass a Symbol to one of them, that method’s visibility is altered.

In this case, they’re methods of Module . When you call private , protected , or public , the current module (remember that a class is just a special kind of module) changes the rules it applies to newly defined methods from
that point on.

Private and protected methods work a little differently in Ruby than in most other
programming languages. Suppose you have a class called Foo and a subclass SubFoo .
In languages like Java, SubFoo has no access to any private methods defined by Foo .
As seen in the Solution, Ruby provides no way to hide a class’s methods from its sub-
classes. In this way, Ruby’s private works like Java’s protected .

Suppose further that you have two instances of the Foo class, A and B. In languages
like Java, A and B can call each other’s private methods. In Ruby, you need to use a
protected method for that. This is the main difference between private and pro-
tected methods in Ruby. Continue reading

Ruby : Writing Very long string in multiple lines

Using backward slash (\) at the end of line tells the ruby parser that the line is still incomplete. Also the parser concatenates the two string into one.

> long_sentence = "This is a very long sentence and this line even cannot"\
> " be written in one line"
 => "This is a very long sentence and this line even cannot be written in one line"