Plaxo Address Book Access Widget / Contact Importer

Plaxo (Address Book Access Widget or contact importer) is an online address book and social networking service.The Plaxo is a simple-to-implement code of JavaScript which you can easily embed on your web page.

The Widget provides facility to import contacts from multiple address books like American Online, Outlook Express , Plaxo ,Gmail, Yahoo, etc…

Here, i provides step which you will follow and get required result

1. Create a page on your site with the following HTML code and save page (like plaxocontacer.html).

<html>
<head>  <script type=”text/javascript” src=”https://www.plaxo.com/ab_chooser/abc_comm.jsdyn“></script>   </head>
<body>     This is for testing </body>
</html>

2. The page in which you want address book(list of contact) widget than you just add following HTML code inside the HEAD tag

<script type=”text/javascript” src=”http://www.plaxo.com/css/m/js/util.js“></script>
<script type=”text/javascript” src=”http://www.plaxo.com/css/m/js/basic.js“></script>
<script type=”text/javascript” src=”http://www.plaxo.com/css/m/js/abc_launcher.js“></script>
<script type=”text/javascript”>
<!—
function onABCommComplete() {
// OPTIONAL: do something here after the new data has been populated in your text area
}
//—></script>

3. You must give the id of textarea in which you want to place names and e-mail addresses
e.g. <textarea id=”recipient_list” name=”recipients”> </textarea>

4. Add a button to your page to launch the address book access widget
– It will place your chosen contact to textarea which has id as recipient_list.
– Once you have filled in the details, add the resulting code to your page to create the button that will launch the widget. You can place your own button image and onclick you call function as showPlaxoABChooser().
<a href=”javascript:;” onclick=”showPlaxoABChooser(‘FILL_ME_IN’, ‘FILL_ME_IN’); return false”>
<img src=”http://www.plaxo.com/images/abc/buttons/add_button.gif&#8221; alt=”Add from my address book” /> </a>

– Now, you get the expected outcome. I think this may help you to integrate address book successfully.
Free to post the comment if you have.

Map different table with model

As per behavior of ruby, it refers table name as pluralized form of model.
If you need to map separate table name with model than simply use “set_table_name“.
Below i provide the example,

class Section < ActiveRecord::Base
set_table_name “contents”
end

Now you set the table as contents. so, instead of referring “sections” table it uses “contents”. So, simple to implement this.

Usage of cURL

cURL – Client for URL (http://en.wikipedia.org/wiki/CURL)

The curl command is use for sending request using client command prompt to server. curl support most of the protocol such as HTTP, HTTPS, FTP, etc.
When you send request using curl it always get some response back from server.To access curl command first of all you have to install that.
If you are linux user than install package by apt-get install curl. Once installing curl you just type curl –help to find out available option with it.

Lets, see the syntax of curl.

curl -u user_name -H ‘Accept: application/xml’ -H ‘Content-Type: application/xml’ -d ‘<root><name>priyanka</name><profession>software developer</profession><email>priyankapathak@mailcatch.com</email></root>’ http://127.0.0.1:3000/sections/contact.xml

-u = user name
-H = format of request / content type
-d = xml data
URL = xml request using any of the protocol (http, https, ftp, etc..).
-x = provide method which you need to use (DELETE, POST, PUT)
There are many other option available with curl.

hope this article will help you out.

Email Broadcasting API – Contactology

The Contactology Email Marketing API for email delivery and tracking service that allows companies to easily and seamlessly send emails and check statistics on reader receipts, bounces, opens, click-through rates and other relevant data.
There are different features available for API.

  • Organize and send the email campaign.
  • Easily manage email lists, contacts etc..
  • Spam protection.
  • Analysed report for activity with contactology API.

Contactology api support many different laguages like  PHP, Ruby, Java, etc..

Lets, we see how we use contactology with rails application.

– First you need to create account with contactology by registring on https://www.contactology.com/trial.php
– Once you register with contactology need to place credential in config/contactology.yml

contactology.yml
wsdl_url: http://soapserver.emailcampaigns.net/1.1/wsdl
login: your login
password: your password

– Install require gem to invoke contactology api’s function.
gem install soap4r –include-dependencies

– After installing gem first you check whether contactology is accesse or not through console.
Code:
require ‘soap/wsdlDriver’

WSDL_URL = “http://soapserver.emailcampaigns.net/1.1/wsdl&#8221;
LOGIN = “your login”;
PASSWORD = “your password”;

soap = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver
soap.options[“protocol.http.basic_auth”] << [WSDL_URL, LOGIN, PASSWORD]

result = soap.GetActiveLists(“U”, “Description”, 0, 0)
puts “result goes here: ”
puts result

– Once you successfully get the result, implement it with you rails application.
Here i place the simple usage of contactology function in my model.
class Section < ActiveRecord::Base
require ‘soap/wsdlDriver’

# need to add custom field – used function GetCustomFields & AddCustomField
def create_custom_field
field_name = “language”  # this custom filed i need to add
soap = authenticate_contactology
existing_custom_fields = soap.GetCustomFields(“U”,”Description”,0,0) # it gets all created custom fields
unless existing_custom_fields.blank?
exisitng_field_tokens = existing_custom_fields.collect{|c| c.field_token}
unless exisitng_field_tokens.include? field_name.to_s
add_custom_field_result = soap.AddCustomField(field_name,field_name,0,1,2,2)
return add_custom_field_result unless add_custom_field_result.blank?
end
end
end

private
# authenticate with contactology by passing credentials.
def authenticate_contactology
config = YAML::load( File.read( File.join(Rails.root, ‘config’, ‘contactology.yml’) ) )
soap = SOAP::WSDLDriverFactory.new(config[‘wsdl_url’]).create_rpc_driver
soap.options[“protocol.http.basic_auth”] << [config[‘wsdl_url’], config[‘login’], config[‘password’]]
end
end

Now.. login to contactology.com and see custom field (“language”) created in your account.

Find interesting!! want to know more… just refer http://www.contactology.com/email-marketing-api/index.php

View live demo of contactology 5.0 – http://www.contactology.com/v5.php

Hope this will help you out. If any suggestion or query than simply post the comment.

Easy steps to cache your rails application

Why we required caching?
– Caching is one of the technique to speed up your application.

What different types of caching available in rails?

There are 3 different types of caching available in ruby on rails.

  • Page Caching
  • Action Caching
  • Fragement Caching

There are 4 different types of storage for cached data.

  • Memory Store – store cached data in form of simple hashes.
  • File Store – store cached data in files.
  • DRb Store – use DRb server to store cached date in form of simple hash.
  • Memcached Store  – store cached data in shared memory cache (memcache).

How to implement caching with rails and importance of different storage.

First you have to enable caching for application by placing this on your config/environments/development.rb

config.action_controller.perform_caching = true

Default cache page is store in public directory of application. If you need to change directory for caching process than use
config.action_controller.page_cache_directory = “/tmp”

Define the storage for cached page/action/fragement.
config.cache_store = :memory_store # for memory storage
config.cache_store = :file_store, ‘/tmp’ # for file storage and specify directory
config.cache_store = :mem_cache_store, ‘localhost’ # for mem_cache storage

Lets, we start with simple caching.

1. Page Caching

From the 3 different caching facility Page caching is one of the most simplest and fastest technique.
Page caching is an approach to caching where the entire action output of is stored as a HTML file that the web server can serve without going through rails.

Two methods:
caches_page : for caching the page.
expire_page : for expiring cached page.

Below is the code to implement page caching.

class SectionsController < ApplicationController
caches_page :index

def index
………..
end

def create/update
expire_page :action => :index
end
end

Now cache page is store in your public and with action name eg. index.html

As it’s simple than always have some limitations:
– Filtering problem like authentication on page
– Query string like ?page=2
– If there are multiple place where you required cached page than it’s turn into complex sweeping

2. Action Caching

Action cache is used when you need to cache response of the action unlike the page caching. When action cache is invoke it access rails controller.

Two methods:
caches_action : for caching the action.
expire_action : for expiring the cached action.

Below is the code to implement action caching.

class SectionsController < ApplicationController
caches_action :show, :expires_in => 1.hour

def show
end
end

Above code is cache the show action and expire every 1 hour duration.

Limitation of action cache:
– Slower than page caching.
– Query string problem.

3. Fragement Caching

Last and more useful caching is fragement caching. It’s use when we need to cache particular fragement/block of page.

There are several methods:
expire_fragement    : for expiring the cached fragement.
fragement_exist?    : check the existance of fragement.
read_fragement(key) : read content from specified fragement.
write_fragement(key, content) : write the fragement by specifing key and the content which you need to write.

Below is the code to implement fragement caching.

sections/index.html.erb

<% cache :key => “set_section” do
unless @sections.blank?
@sections.each do |s| %>
<%= s.name %>
<%= s.description %>
<%     end
end
end
%>

Above code, i just cache the sections listing fragement and give the key as “set_section”.
Now need to expire this fragemented cache by
expire_fragement :key => “set_section”

Once you cache any page or action or fragment, need to expire for that we are using sweeper of rails.
There are pre-built method of the rails available for this. here is the example:

class TestSweeper < ActionController::Caching::Sweeper
#observe model
observe Section

def after_save
expire_page :controller => “sections”, :action => “index” # expire cached page
expire_action :controller => “sections”, :action => “show” # expire cached action
expire_fragement(“set_section”) # expire cached fragement
end
end

Invoke the sweeper from the controller by

class SectionsController < ApplicationController
caches_page :index
caches_action :show
cache_sweeper :test_sweeper

def index
end

def show
end
end

Hope this article will help. If have any query or suggestion than post comment.

Deploy and Checkout rails application from Heroku platform

There are different platforms available to deploy rails application. One of the platform is Heroku.
Heroku uses the github’s repository remotely.

Deploy rails application on Heroku platform.

1. You have to create account with heroku by registring on http://heroku.com/signup.

2. Intially you develop single rails application and than place it under version control (github).
# create rails application
rails sample_app
cd sample_app
# set application under version control
git init # to intialize the repository on github
git add . # add under version control
git commit -m “initial commit”

3. Once your application is under version control. now you deploy application on heroku platform.

First you need to install gem
gem install heroku

Add your public key to access repository to push or clone heroku application.

heroku keys:add
Enter your Heroku credentials.
Email: test@mailcatch.com
Password: *****
Uploading ssh public key /Users/test/.ssh/id_rsa.pub

4. Now you have to create application on heroku.
heroku create sample_app
it returns the message as Created http://sample_app.heroku.com/ | git@heroku.com:sample_app.git

5. Initially your heroku application is blank. now you have to specify the remote branch to push.
git push heroku master

If you already have git repositiry for application than just remotely add that on heroku.
git remote add heroku git@heroku.com:sample_app.git

6. Genearte database by running
heroku rake db:migrate

You successfully deployed your rails application on heroku platform.

Checkout the project from heroku to your local system.

1. On your local system you just need to install heroku gem and add your public key for access.
gem install heroku
heroku keys:add

2. Need to list all the application under heroku by
heroku list

3. cd into one of the application on which you need to work.
eg. cd sample_app

4. List the heroku information for sample_app by
heroku info

5. Capture or Download the application on local using command
heroku bundles:capture sample_app
heroku bundles:download sample_app

6. Now pull database from heroku to your local machine using command
heroku db:pull database_url

7. Now on your local machine rails application set up from heroku platform. Just run your application successfully.

Hope This article will help you out. Need more information about Heroku by visiting official site of Heroku – http://heroku.com/

Difference between active record methods – update, update_all, update_attribute, update_attributes

1. update(id,attributes)

Update single or multiple objects using update method of active record. When update method invokes it invoke model based validation, save the object when validation passes  successfully else object is not save.

It requires two argument id and attributes which need to update.

Model.update(1,:language => “ruby”,:framework => “rails”)

same way you are able to update multiple objects.

Model.update([1,2],[{:language => “ruby”,:framework => “rails”},{:ide => “aptana”}])

2. update_all(attribute, conditions, options)

It updates the attribute of object by invoking specified conditions and options like order, limit. But disadvantage of this method is it’s not invoke validation of model.

Model.update_all(“language = “ruby”, “framework Like ‘%rails'”,:limit => 2)

3. update_attribute

This method update single attribute of object without invoking model based validation.

obj = Model.find_by_id(params[:id])
obj.update_attribute :language, “php”

4. update_attributes

This method update multiple attribute of single object and also pass model based validation.

attributes = {:name => “xyz”, :age => 20}
obj = Model.find_by_id(params[:id])
obj.update_attributes(attributes)

Hope this article will clear out when to use what method of active record. If have any query or suggestion than simply leave the comment.

Read and Write the Open Office Spreadsheets

In ruby on rails we have two different gem available for read and write the Open Office Spreadsheet (ODS) document.

1. Write the .ods file using rodf.

rodf provides facility to generate .ods file and many other features to set the attributes spreadsheets.

First of all install rodf on your system by
gem install rodf

After successful installation of rodf, now create the .ods by refering below sample code.
eg.

ODF::SpreadSheet.file(“#{RAILS_ROOT}/sample_spreadsheet.ods”) do |spreadsheet|
spreadsheet.table ‘Initial Table’ do |table|
spreadsheet.style ‘cell_css’, :family => :cell do |style|
style.property :text, ‘font-size’ => ’10pt’, ‘font-family’ => ‘arial’,
‘font-weight’ => ‘bold’, ‘color’ => ‘#595858’
end
table.row{|row|
row.cell “working with rails have fun”, :style => ‘cell_css’
row.cell “currently using rodf to write the ods”
row.cell 120.09, :type => :float
}
end
end

Now, open the stylesheet which generated in your rails application with name as sample_spreadsheet.ods. It contains data like this
first row and valued cell are like this..
cell A has value – working with rails have fun
cell B has value – currently using rodf to write the ods
cell C has value – 120.09 (with float as cell type)

2. Read the .ods file using roo.

roo provides facility to read the .ods  file with specified attributes.

First of all install roo on your system by
gem install roo

After successful installation of roo, start reading the .ods by refering below sample code.
eg.
require ‘rubygems’
require ‘roo’

ods = Openoffice.new(“#{RAILS_ROOT}/sample_spreadsheet.ods”)
ods.default_sheet = ods.sheets.first
data = {}
1.upto(ods.last_row) do |line|
data[line] = ods.row(line)
end

puts “Here listing the data of ods”
puts data.inspect

It will show that first row has 3 associated values.

This example shows single row with multiple cell values from ods sheet. Same way you also play with multiple rows creation in ods and many other features.

Hope this article is useful to all my readers. If you have any question or suggestion than feel free to post comment.

Usage of bang (!) in ruby on rails

There are different usage of bang – explanation (!) in rails.

1. ActiveRecord Method

We are using save! over save, do you know exact difference between those.
save! – it raise the error if there any exception like some validation which you place in model.
save – it return false when fails during process.

2. Class Method

There are several method available in ruby’s different classes. eg.. array has method compact and compact!
lets we have array a = [2,4]
when you use a.compact return true means array itself. but a.compact! return nil means no element with nil value.