Thursday, October 13, 2011

Escape or Encode the character using javascript or ruby

Guys,
          When we are storing the data in the database using any encoding method like UTF-8 and retrieve data in javascript it gives us in output in decode and unescape format.

To solve above issue using javascript or ruby
Javascript functions:
escape(String) - unescape(String)
encodeURI(String) - decodeURI(String)
encodeURIComponent(String) - decodeURIComponent(String)

Ruby methods:
CGI.escape(String) - CGI.unescape(String) or CGI.unescapeHTML(String)
URI.escape(String) - URI.unescape(String)

Online tool for escape/unescape, encode/decode the character.
http://www.the-art-of-web.com/javascript/escape/

Wednesday, October 12, 2011

How to create low stack ruby application using sinatra

Hello Guys,
        Yesterday i learn something new. How we create the low stack ruby application? So, here is the one of the possible solution. We can use the sinatra gem for it.

Sinatra is a DSL for quickly creating low stack web application in Ruby. Lets now install the sinatra gem by
gem install sinatra

To create simple web application by following below steps:

# test_sinatra.rb
require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra'

get '/test' do   
    file_name = "google"
    file_store = "#{RAILS_ROOT}/tmp/#{file_name}.pdf"   
  send_file file_store, :filename => file_name ,:disposition => 'inline', :encoding => "utf-8"
end

if you want to pass the argument than use below 2 cases.

case 1:
get '/test/:arg' do 
  puts "Here is your supplied arg : " + params[:arg]
end

case 2:
get '/test?' do
  puts "Here is your supplied arg : " + params[:input_args]
end

so after ? = we can pass N numbers of query parameters. here i conduct the input_args as query parameter

There are all HTTP methods supported by sinatra.

Run the sinatra app by ruby test_sinatra.rb -p 9999

Find it interesting? Read more information about sinatra by referring http://www.sinatrarb.com/intro.html

http://titusd.co.uk/2010/04/07/a-beginners-sinatra-tutorial/

Wednesday, October 5, 2011

Avoid the memory blockage during large xml request

Guys,
        When i am playing with handling the xml request through net::http discover the problem of memory blockage and just worried about how to handle that. This problem happen b'coze i have bulk of xml data in the request. So, i form the one of the solution by using the block of net http.

When we using the block of net::http. It yields each fragment of the entity body in turn as a string as it are read from the socket.

Lets we consider 2 scenario:

require 'rubygems'
require 'net/https'
require 'uri'
uri = URI.parse("http://google.com")
http = Net::HTTP.new(uri.host, uri.port)

case 1:
response = http.post('http://google.com', 'query=language')

case 2:
# using block
   File.open('test.txt',  'w') { |f|
      http.post('http://google.com', 'query=language') do |str|
          f.write str
       end
   }

In case 1 it loads the entire response in memory. While in case 2 it read each fragment and write into file. so when we use block it's avoid memory blockage.
For more information just follow

Hope above post will help you to avoid memory blockage and increase performance.

Tuesday, October 4, 2011

undefined method 'add_to_rails_paths' for Cms:Module (NoMethodError)

Hello guys,
          During the browser cms configuration i have got the some errors which i have listed below with appropriate fix.

Error:
/gems/bcms_event-1.0.0/rails/init.rb:2:in `evaluate_init_rb': undefined method `add_to_rails_paths' for Cms:Module (NoMethodError)

Solution:
In bcms_event-1.0.0/rails/init.rb add below line
require 'browsercms'

Error:
/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:466:in `load_missing_constant': uninitialized constant Cms::S3 (NameError)

Solution:
install dependency via gem install right_aws

Keep in mind browser cms application runs with ruby 1.8.7 or latest.

Content Management System - Browser CMS

Hello rubies,
        I have integrated browser cms in one of the rails application. Awesome to deal with it. Install the browser cms as per your rails version.

If you are using rails below 3 than use browsercms 3.1.2
else use latest version browsercms 3.3.2

After installing cms create the rails application by using command
bcms new project_name -d mysql
cd project_name
rake db:install
rails server

This will create the cms using browsercms. Now you will deal with it by 
adding additional pages, portlets, sections..etc

Want to create in built demo application using browser cms?
bcms demo project_name -d mysql
cd project_name 
rake db:install
rails server

This will create a BrowserCMS project which used MySql as the data
storage. Run the application using http://localhost:3000/cms. and use it's
default user credential cmsadmin/cmsadmin

Get more information regarding browser cms by referring
https://github.com/browsermedia/browsercms/wiki

Monday, October 3, 2011

How to integrate newrelic rpm with rails

Hello,
          If you want to measure the performance (loading time) of your application than use newrelic rpm plugin/gem.

Either install newrelic plugin or gem.

script/plugin install http://newrelic.rubyforge.org/svn/newrelic_rpm
or
gem install newrelic_rpm
Add config.gem "newrelic_rpm" in environment.rb

Now in config edit the newrelic.yml
If you want to monitor application in development mode than set monitor mode

development:
  <<: *default_settings
   monitor_mode: true
   developer_mode: true

Now access the http://localhost:#{port}/newrelic. it shows the time consumption as per request. so from this you will get idea from where most time consumed. Based on that you can optimize the code to save time.


Want to get more information?
follow - https://github.com/mislav/newrelic_rpm