1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Access Web Services & Remote URLs with Ruby's HTTParty

Discussion in 'Ruby on Rails' started by pradeep, Apr 22, 2013.

  1. pradeep

    pradeep Team Leader

    As programmers most of us today are writing programs/scripts that consume data from web services or APIs, like the Facebook Graph API, etc. Writing your own subroutines from open socket and making request and the likes are time consuming and are not worth putting effort on for all projects, so Ruby has a gem called HTTParty which will help you concentrate on the business logic instead of mundane HTTP request tasks.

    HTTParty is a very neat gem, it combined with the power of Ruby creates a lucid and free flowing code. HTTParty can automatically parse JSON/XML type responses on the basis of the Content-Type header. In this article we'll look at the installation of the HTTParty gem and basic usage to get you started.

    Installing The HTTParty Gem



    Installation is pretty simple, issue the following command as a root user:

    Code:
    $ gem install httparty
    
    That's it, HTTParty is installed.

    Using HTTParty To Fetch Remote URLs



    Let's look at a very basic example of fetching Google's homepage and printing the content and the HTTP response code, follow the code below:

    Code:
    require 'rubygems'
    require 'httparty'
    
    response = HTTParty.get('http://www.google.com')
    
    print response.code, response.body
    
    That looked pretty simple, HTTParty can also handle HTTP Basic Auth, let's see how:

    Code:
    require 'rubygems'
    require 'httparty'
    
    response = HTTParty.get('http://www.go4expert.com/admin',{:basic_auth => {:username => 'pradeep', :password => 'test'}})
    
    print response.code, response.body
    
    Next is a demo Ruby class I have written which gets Facebook info using the Graph API, you'll need to pass a valid access token for it to work, you can get one from at https://developers.facebook.com/tools/explorer
    I have commented the code as required, so that you find it easier to follow:

    Code:
    require 'rubygems'
    require 'httparty'
    
    ## define class
    class Facebook
      ## load and inherit HTTParty class
      include HTTParty
      ## set the default base URI
      base_uri 'https://graph.facebook.com'
      ## set the format to JSON, so that HTTParty will automatically decode it
      format :json
    
      ## constructor
      def initialize(access_token)
        @access_token = access_token
      end
    
      def get_info
        self.class.get('/me',:query => { :access_token => @access_token })    
      end
    end
    
    ## create an instance of the Facebook class
    fb = Facebook.new(<your-access-token>);
    
    ## call the method
    r = fb.get_info()
    
    print r.body
    
     
    shabbir likes this.
  2. Vinus26

    Vinus26 New Member

    Great information thanks for sharing this information.
     

Share This Page