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

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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

    Joined:
    Apr 23, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Great information thanks for sharing this information.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice