Showing posts with label hpricot. Show all posts
Showing posts with label hpricot. Show all posts

Wednesday, November 30, 2011

Handle JSON or XML request and response

Hello Guys,
        Some time we required to pass the request in particular format and get response back in same format. 

1. Consider the request and response format as JSON

Request:
curl -H "Accept: application/json" -i -X GET http://DOMAIN:PORT/controller/action/parameters
eg. curl -H "Accept: application/json" -i -X GET http://127.0.0.1:3000/posts/show/1

Response:
In the show method of posts controller we have to check whether request type is JSON than return response back to JSON format.

def show
  if request.format == Mime::JSON 
     post = Post.find_by_id(params[:id])
     render :json => post.to_json, :status => 200
  end
end

Now we get response as the requested post in .json. we will parse the json by JSON::load(response)

2. Consider the request and response format as XML

Request:
curl -H "Accept: application/xml" -i -X GET http://DOMAIN:PORT/controller/action/parameters
eg. curl -H "Accept: application/xml" -i -X GET http://127.0.0.1:3000/posts/show/1

Response:
In the show method of posts controller we have to check whether request type is XML than return response back to XML format

def show
  if request.format == Mime::XML
     post = Post.find_by_id(params[:id])
     render :xml => post, :status => 200
  end
end

Now we get response as the requested post in .xml. we will parse the xml using Hpricot or any other parser.

Hope this post will help you to deal with json and xml format.

Friday, August 19, 2011

Faster XML Parser in ruby on rails

Hello Guys,

Last week i was trying to  parse xml using different xml parser like Hpricot. But when we have large amount of data than segment fault occurs. So i moved to better and faster xml parser called libxml-ruby.

There is simple steps to parse large xml using libxml-ruby. First of all you need to install libxml-ruby by

  gem install libxml-ruby

Lets, we have sample xml

sample.xml
xml = %{
  <users>
    <user>
      <name>Priyanka Pathak</name>
      <mark subject=”biology”> 80 </mark>
    </user>
    <user>
      <name>Rahul Pathak</name>
      <mark subject=”biology”> 85 </mark>
    </user>
  </users>
}

Now create method to parse xml

require 'rubygems'
require 'libxml'
require 'benchmark'

def parse_xml
   Benchmark.bmbm do |r|
      r.report("Process XML"){
        parser = LibXML::XML::Parser.file('sample.xml',:encoding => XML::Encoding::UTF_8)
        doc, collect_data = parser.parse, []
        doc.find('//users/user').each do |e|
           data = {}
           data['name'] = e.find('name').first.content
           mark = e.find('mark').first
           data['mark'] = {:subject => mark.attributes.first.value , :value => mark.content}
           collect_data << data     
        end

        puts "collect data: " + collect_data.inspect
     }
   end
end
Benchmark shows the time required during xml parsing and as per my experience it's faster than other xml parser.

For more information about libxml follow http://libxml.rubyforge.org/rdoc/
Hope this post will help you.