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

Advertisements

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
end

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

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.

 

Sources

https://github.com/vcr/vcr

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 user@mysite.com: 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@45.79.200.151: 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:

https://rubygems.org/gems/jwt/versions/1.5.3

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

Sources:

http://stackoverflow.com/a/36219627/3437900

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:

AWS_ACCESS_KEY_ID=your_api_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_REGION=us-west-2

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