Tuesday, 21 October 2014

The core issue we face before using the
explanation of the use of process managment tools/gems:

While I have implemented sidekiq to
monitor the threads in our application for sending emails in certain
time frames. The sidekiq stops if server reboots and the queued
processes goes in a indefinite period of time without execution. So
this needs the sidekiq should be restarted automatically when it
stops.

Let us start with basic examples, how
to write codes to execute the processes using God.

Let us create a new file god_demo.rb in
application folder.

Write the following code in the same
file

loop do

puts 'Hello'

sleep 1

end

Now we'll write a god config file that
tells god about our process. Place it in the same directory and call
it god_demo .god:

God.watch do |w|

w.name = "god_demo"

w.start = "ruby
/full/path/to/god_demo.rb"

w.keepalive

end

God configuration should start with a
God.watch block. A watch in god represents a process that we want to
watch and control. Each watch must have, at minimum, a unique name
and a command that tells god how to start the process.
The keepalive declaration denotes
god to keep this process alive. If the process is not running when
god starts, it will be started. If the process dies, it will be
restarted.

To run god, we give it the
configuration file we wrote with -c. To
see what's going on, we can ask it to run foreground with -D:

There are two ways that god can monitor
your process. The first and better way is with process events. Not
every system supports it, but those that do will automatically use
it. With events, god will know immediately when a process exits. For
those systems without process event support, god will use a polling
mechanism. The output you see throughout this section will show both
ways.

It
uses Fedex API Shipment version 13. This version uses the
Non-SOAP Web Services so there is no need to download the Fedex WSDL
files, note however that you will need to apply for
development/production credentials.

Installation

Include gem
'fedex' in Gemfile.rb .

Run $bundle install .

How to use?

Basically we use
some shipping destination where to ship and the sender's destinations
from where we want to ship. We need to provide package informations .
The package informations consists of

Package Dimensions
with parameters

height

length

width

These
informations need to give in inches
or centimeters .

Package
Weight parameter

weight
with lbs

We
will send a request to the fedex API to get rate from fedex. Later we
will get a request to get label from fedex. The label will give a
report with bar code with valid fedex tracking number.

Using Rails gem
how will acomplish this will described here .....

Defining a Test
Shiper

shipper = { :name => "Test
Fedex Sender",

:company => "Home",

:phone_number =>
"121-333-2332",

:address => "Main
Street",

:city => "Littleton",

:state => "CO",

:postal_code => "80125",

:country_code => "US"
}

Defining a test
Recipient

recipient = { :name => "Test
Fedex Recipient",

:company => "Home",

:phone_number =>
"555-555-5555",

:address => "Main
Street",

:city => "Boulder",

:state => "CO",

:postal_code =>
"80304",

:country_code => "US",

:residential =>
"false" }

Defining the packages; multiple
packages in a single shipment are also allowed (all the dimensions
must be integers).

Tuesday, 9 April 2013

Most of the time we think about a
better version of rails for which our application can run smoothly.
In the mean time rails has been upgraded to 4 new versions. For
rails developer who has been acquainted with all earlier 3 versions
will be easy to understand the later versions of rails . Let us
discuss some new concepts in rails 4. :)

The most important changes that
Rails 4 needs is Ruby 1.9.3 at a minimum.

Vendor/Plugins directory folder
has been removed from Rails 4.

Deprecations

Many things has been deprecated and
moved to separate gems. Like active record deprecator s finder. Rails
4 uses “activerecord-deprecated_finders” gem for finding old
active record finders .

Deprecated finders

Rails 2/3
Model.find([:all/:first/:last]) has been deprecated.

# find_all_by_name

Rails 3 Model.find_all_by_name('name')
is deprecated.

In Rails 4, Model.where(:name, 'name')

#find_last_by

Rails 3
Model.find_last_by_name('name') is deprecated.

In Rails 4, Model.where(:name,
'name').last

#find_or_create_by

Rails 3
Model.find_or_create_by_name('name') is deprecated.

In Rails 4, Model.where(:name,
'name').first_or_create

#find_or_create_by..!

Rails 3
Model.find_or_create_by_name!('name') is deprecated.

In Rails 4, Model.where(:name,
'name').first_or_create!

#find_or_initialize_by

Rails 3
Model.find_or_initialize_by_name('name') is deprecated

In Rails 4, Model.where(:name,
'name').first_or_initialize

Normal Eagerly evaluated Scopes
declarations have been deprecated

Scopes require now a callable object.
In Rails 4 all scopes must use a callable object such as a Proc or
lambda.

For example, In Rails 3 we can declare
scope as

Class Comment <ActiveRecord::Base

scope :recent_comment,
where(:published_at, Time.now-2.weeks)

end

but it is deprecated in Rails 4. And
the new decalration as below

Class Comment <ActiveRecord::Base

scope :active, -> where(:status,
'active')

end

Identity map removed

active_record identitymap
configuration has been removed from Rails4. If you are upgrading
Rails 4 from Rails 3, then please be sure to remove/comment the line
from config.application.rb

// config.active_record.identity_map =
true

ActiveRecord::SessionStore

Storing sessions in databases is an
interesting feature in many of the applications. As many cases it
does not have much influences like a normal cokiee store. So
ActiveRecord::SessionStore has been removed from Rails 4. We can use
it by adding gem 'activerecord-session_store'
in gem file.

ActiveResource

ActiveResource which provides a ORM to
REST web services is no longer exist in Rails 4. To get the
funcationality in your app include gem 'activeresource'

authenticity_token

authenticity_token is no longer
included in forms in Rails 4. While submiting non-ajax forms in
rails, authenticity_token was required in back end to prevent
cross-site forgery attacks.

New Features

Postgress Array Support

We all know ProstgreSQL supports array
datatypes. That means we can store array elements in a column in
PostgreSql. But normally in many of the databases they are stored as
strings.

Active Record with Rails4 will support
array datatypes for PostgreSql. Also it will support MACADDR, INET,
CIDR datatypes.

Strong Parameters

Rails 4 has introduced mass assignment
protection of attributes in controller. This process can be achived
by the “strong parameters” in Rails 4. It is the tool to
fight against the mass assignment vulnerabilities.

Let us consider one example incase of
Rails 3, when we are supposed to submit a form to create a user we do
as,

<form method='post'
action='/users'>

<input type='text' name='name'
/>

<input type='submit' />

</form>

When we submit the form it will go to
create a user record. For this we need to mass assign the name attribute in model as
'attr_accessible', neither it will raise an error. To protect the
name attribute in model we will do something like this ..

Class User<ActiveRecord::Base

attr_accessible :name

end

Then in controller we will do this to
create the user

def create

@user = User.new(params[:user])

@user.save

.....

end

In Rails 4 there are new options to
make the attribute secure and dynamically set them as attr_accessible
in controller side.

Class UsersController<
ApplicationController::Base

def create

@user = User.new(user_params)

@user.save

.....

end

private

def user_params

params.require(:user).permit(:name)

end

end

We can test it in our 3.2.x
applications by installing the strong_parameters gem in app. :)

Turbolinks

Turbolinks makes the following links
in your web application faster. Instead
of letting the browser recompile the JavaScript and CSS between each
page change, it keeps the current page instance alive and replaces
only the body and the title in the head.

Turbolink
works like PJAX. But there are many fuctionalities difference in both
the two.

Turbolinks
uses pushState,
but instead of replacing only parts a page it loads a complete
website from the server and replaces the <title>and<body>in
the currently loaded DOM. By default it applies this to all links on
a page. So unlike PJAX you don't have to mark links and containers to
support in place reloading, Turbolinks will handle that for you.

In
Rails 4 bydefault gem 'turbolinks' is being added in the gem file.
Hope it will be better to slow down the page responses and the sight
will run faster :)

Live
Streaming

Rails
4 will support to live streaming. Live stream is the effect to stream
content to end user's
browser directly from the server with long polling in the browser.

Live
streaming has now got much popularity now a days. It helps to watch
score reports of various games, sports, watching seminars from famous
people hosting through internet, viewing results, and lot more.

The
PATCH verb

PUT
in REST is defined for updating a record in rails. PATCH will work as
same . It will refer to the update action of the controller. But we
need to post the form with the parameter “_method=PATCH”. Also
routing will help to redirect to the update action in the controller.

PUT,
semantically-speaking, should contain a full representation of an
entire object to be updated. But in Rails 4 PATCH will send a new bit
of rescource about an object on the server not a full representation
of it.

The
same PUT method will also work in case of Rails 4.

Concern
in Routing

In
Rails 3 if we have a same nested resource for a resource, we include
the nested resource each time in a parent resouce. For example

resouces
:projects do

resources
:comments

end

resources
:tasks do

resources
:comments

end

Rails
4 introduced concern to cut down the duplicacy of common resource
routing declaration. Here it defined as