Monday 29 April 2013

Using NOKUGIRI gem to parse XML

Nokugiri gem can be used to parse html and xml files. Below is a small tutorial on how to do this in UBUNTU. (Installation procedure for windows is different I gue

1) Add gem "nokogiri" inyour gem file.

2) Run bundle install.

3) To open the xml file use this:-
  doc = Nokogiri::XML(open(filename/URL))

4)Suppose you want to read the content from the folowing xml:-
<first_tag>
<second_tag>Hi XYZ</second_tag>
</first_tag>
To read the content do the following:-
doc.css("first_tag second_tag").map {|node|
                 
        p  tag<<node.children.text
        }

This will output HI XYZ.

5) There are also some other methods which can be used to read the xml like:-

data=doc.xpatch("//first_tag").map do |item|
  [
    item.search("second_tag").first.content
  ]
end

Friday 5 April 2013

Nice twitter bootstrap popup box(modal)

Recently I came across a requirement to have some content on the pop up box when a user clicks on the link. The content can be static or dynamic from the database itself. There are several options that are available like jqbox. However I found twitter bootstrap modal quite useful and attractive.

Refer to the link to know how to use it Twitter Bootstrap modal

To make it dynamic I simply made an ajax call and used the returned JSON object to append the content in the modal. Refer the following to get a brief idea about it.

Code for Twitterbootstrap modal:-



<div id="example" class="modal hide fade in" style="display: none; ">
<div class="modal-header" style="background-color:#CCB647"><h3>  <center><font color="white">Identify the Person!!</font><a class="close" data-dismiss="modal">×</a></center> </h3>
</div>
<div class="modal-body" style="background-color:#D6DEE4">
<div id="game">
<img id="photo" src="" style="width:200px;height:200px;"><br/>
 <div id="options">
 </div>
</div>
</div>
<div class="modal-footer" style="background-color:#CCB647">

Link to open the modal:

<a data-toggle='modal' href='#example' data-backdrop="static" data-keyboard="false" onclick="updatemodal();">
</a>

Script for ajax call:

function updatemodal{
  $.ajax({
   url: '../play/play_game',
   error: function (XMLHttpRequest,textStatus,errorThrown) {
     alert("Something went wrong!! Please try again later. Unable to create options");

   },
   success: function(data){

// Here data is the json object returned by the server
      var jsonObj = $.parseJSON(data);

      $.each(jsonObj,function(key,value){
 
  $('#options').append(value.content);
 
    });

   },
   type: "get"
 });



}


Multiple tab excel sheet using spreadsheet gem


To create multiple sheets in a single excel sheet , its good to use spreadsheet gem. In your controller (suppose card is your controller) add the following:

def export
 
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => 'Sheet1'
    sheet2 = book.create_worksheet :name => 'Sheet2'

     sheet1.row(0).push "some content in Column1" //to push content in 1st row 1st column of 1st spreadsheet

     //keep pushing on row(0) if you want to push contents other columns of 1st row

      spreadsheet = StringIO.new
      book.write spreadsheet
      file = "Excelsheet"
      send_data spreadsheet.string, :filename => "#{file}", :type =>  "application/vnd.ms-excel"
  end


In you view
<%=link_to("Export Database to Excel", cards_url + "/export")  %>