Sunday, November 14, 2010

EC2 API Tools Installation

EC2 API tools serve as client interface for Amazon EC2 web service. One can use these tools to register and launch instances.


Installation on Ubuntu:


# apt-get install ec2-api-tools



Firefox extension for Amazon EC2

step 1: Download and Install the Firefox Extension for Amazon EC2 from http://s3.amazonaws.com/ec2-downloads/ec2ui.xpi

step 2: Run the extension, Launch Firefox and click on Tools/EC2 UI menu

step 3: Register your credentials with the plugin, Click on the “Credentials” button in the top left of the window. Enter your Account Name.Your Account Name needs to

match your username on the AWS developer site. Then enter your Access Key and Secret Key (these can be found when you log into the Amazon Web Services home page

( http://www.amazon.com/gp/browse.html%3Fnode%3D3435361)

step 4: View the list of available AMIs.

Saturday, January 31, 2009

How to add "Cross Sell" with rails

cross_sell products (Related products):

create table products :

CREATE TABLE products (
id INT NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
);

Create a table 'cross_sell' in the mysql query browser :

CREATE TABLE cross_sells (
product_id INT NOT NULL,
cross_sell_product_id INT NOT NULL,
cs_position INT NOT NULL,
PRIMARY KEY (product_id, cross_sell_product_id),
UNIQUE KEY pos (cs_position)
);

steps to create a table in mysql query browser:

1. Right click on the database and select 'create table'.
2. Fill all the fields, all the columns and say apply. and execute.

In the model say 'product', type the following :
has_and_belongs_to_many :cross_sell_products,
:join_table => 'cross_sells',
:foreign_key => 'product_id',
:association_foreign_key => 'cross_sell_product_id',
:class_name => 'Product',
:order => 'cs_position'

In the show view of product :i.e in our example : /view/home/product.html.erb

Related Products :


<% @product.cross_sell_products.each do |cross_sell_product|%>
<%= link_to cross_sell_product.name, :action => 'product',
:id => cross_sell_product.id %>

<% end %>

Now fill in the table cross_sell, with product_id and its related product id, ie. cross_sell_id.

ActionWebService : Ruby on Rails

Installing the gem
$ sudo gem install datanoise-actionwebservice --source http://gems.github.com

Configuration

  1. Add this line to your config/environment.rb file in the initializer section:

    config.gem 'datanoise-actionwebservice', :lib => 'actionwebservice'
  2. Add this require statement to test/test_helper.rb file:

    require 'action_web_service/test_invoke'

Generating API controller

ActionWebService gem includes web_service generator that you can use like this:

$ ./script/generate web_service post
exists app/services/
exists app/controllers/
exists test/functional/
create app/services/post_api.rb
create app/controllers/post_controller.rb
create test/functional/post_api_test.rb

Note that your Apis are placed into app/services directory which Rails automatically includes in the search path, instead of the old app/apis directory.

Define your API

Open app/services/post_api.rb file and add methods that your service exposes:

class PostApi < ActionWebService::API::Base
api_method :get_posts, :returns => [[:string]]
end

Here, we defined get_posts method that accepts no parameters and returns an array of strings.

API implementation

We are going to use direct dispatching mode, so all methods that implement our API go directly to PostController itself:

class PostController < ApplicationController
wsdl_service_name 'Post'
web_service_api PostApi
web_service_scaffold :invocation if Rails.env == 'development'

def get_posts
["Post 1", "Post 2"]
end
end

Several things need to mention here:

  1. We use scaffolding in the development mode, so we can easily test our API via the browser. Just go to http://localhost:3000/post/invocation and follow the screens to make an API call.

  2. You can get WSDL of our Post API from http://localhost:3000/post/wsdl:

    $ curl http://localhost:3000/post/wsdl

    ...
  3. The URL that you actually use to make API calls is http://localhost:3000/post/api

Unit Testing

As you probably noticed, our web_service generator created a unit test file test/functional/post_api_test.rb. Lets test our get_posts method:

require File.dirname(__FILE__) + '/../test_helper'
require 'post_controller'

class PostController; def rescue_action(e) raise e end; end

class PostControllerApiTest < Test::Unit::TestCase
def setup
@controller = PostController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_get_posts
result = invoke :get_posts
assert_equal(["Post 1", "Post 2"], result)
end
end

Lets see if it works:

$ ruby test/functional/post_api_test.rb
Loaded suite test/functional/post_api_test
Started
.
Finished in 0.182469 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

Compiling Apache from source

Steps to compile apache:
step 1: get the source cd /home/hkorupol/software/
#tar -xvzf http*
#cd httpd *
#./configure --prefix=/usr/local/apache2 \
--enable-so --enable-modules=all \
--enable-mods-shared=all –enable-proxy
#make
#make install

Friday, November 14, 2008

Connecting rails application with oracle 8i

1. Install Oracle Instant ClientDownloaded the Instant Client packages for free from Oracle.
All I needed are these two from Version 10.2:
->Instant Client Package - Basicinstantclient-basic-linux-x86-64-10.2.0.3-20070103.zip (36 MB)
->Instant Client Package - SDKinstantclient-sdk-linux-x86-64-10.2.0.3-20070103.zip (0.6 MB)I had downloaded the files into /opt/oracle.
# mkdir /opt/oracle
# cd /opt/oracle
# unzip /proj/downloads/instantclient-basic-linux-x86-64-10.2.0.3-20070103.zip
# unzip /proj/downloads/instantclient-sdk-linux-x86-64-10.2.0.3-20070103.zip
# cd instantclient_10_2
Then create the following symbolic link
# ln -s libclntsh.so.10.1 libclntsh.so
Edit /etc/bashrc to include the directory in your LD_LIBRARY_PATH environment variable by adding these linesLD_LIBRARY_PATH=/opt/oracle/instantclient_10_2:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

2. Install ruby-oci8Download version 1 of the interface from rubyforge:
# wget http://rubyforge.org/frs/download.php/16630/ruby-oci8-1.0.0-rc1.tar.gz
Unpack, make and install the package# tar -zxvf ruby-oci8-1.0.0-rc1.tar.gz
# cd ruby-oci8-1.0.0-rc1
# make
# make install
The make calls ruby setup.rb to configure and then compile the code.
It should all go smoothly provided the Oracle libraries are in place and it knows where to find them.

3. configuring database.yml file of rails application.

I connected to dev database in the host one.cisco.com
development:
adapter: oci
host: one.cisco.com/dev
username:
password:

Note : Note : In /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active-record/connection-adapters , change oracle_adapter.rb to oci_adapter.rb
#cp oracle_adapter.rb oci_adapter.rb

Tuesday, September 23, 2008

Get started with 'Regular Expressions'

Ruby supports majority of standard regular expression syntax.
Regular expression is pattern that either does or does not match any given string.
A regular expression in ruby is placed between a pair of forward slashes: (/^./)

Basic Special characters and Symbols with in regular expressions:

'^' --> Anchor for the beginning of a line
'$' --> Anchor for the end of a line
'\A'-> Anchor for the start of a string
'\Z'-> Anchor for the end of a string
'.' -->Any character
'..'-> Any two characters
'\w'->Any letter, digit or underscore
'\W'->Any thing that '\w' does not match
'\d' ->Any digit
'\D' ->Any thing that '\d' does not match
'\s' -->White space
'\S'-->Non white space

Some examples:

To substitute first two characters of string :
"Hi Harini".sub(/^../,"Hello")
output : Hello Harini

Here '^' represents the beginning of a line, and '..' represents two characters. That means two characters starting from the line, that would be 'Hi' in our example. Method 'sub' will substitute the first argument with second.
Hi is substituted with Hello.

To substitite last six characters of a string :
"Hi Harini".sub(/......$/,"vamshi")
output : Hi vamshi

Here '$' represents the end of a line, and '......' represents six characters. That means six characters at the end of the line, that would be 'Harini' in our example. Method 'sub' will substitute the first argument with second.
Harini is substituted with vamshi.

Iterations :

To print single character at a time of a string:
"abcde".scan(/./){|x| puts x}
output :
a
b
c
d
e

Scan method looks through the string for anything that matches the regular expression passed to it.
'.' represents a single character, In our example regular expression looks for a single character at a time and prints them separately.

To print two characters at a time:
"Hi Harini, how are you".scan(/../){|x| puts x}
output :
Hi
H
ar
in
i,
h
ow
a
re
y
ou

Scan method looks through the string for anything that matches the regular expression passed to it.
'..' represents a two characters, In our example regular expression looks for a two characters at a time and prints them separately.
Note : '.' means any character that includes white spaces also.

To print two characters at a time with out including white spaces:
"This is a test".scan(/\w\w/) { |x| puts x }
output:
Th
is
is
te
st

Special characters denoted with back slashes have special meaning. '\w' means "any alphanumeric character or an underscore".
'\w' will consider the grouped characters.

To print digits in a given string:
"I have 20$".scan(/\d+/){|x| puts x}
output : 20

Regular expression that uses '\d' is to matchany digit, and the + that follows \d makes \d match as many digits in a row as possible.

with these examples try more with those listed in the table..

Monday, September 8, 2008

RESTFUL rails

RESTful Rails have been a a much debated part of the Rails core, since the original restful_rails plugin was merged in to core just over a year ago. But with the improvements that have been made in Rails 2.0, they are here to stay, so it is important to understand what they are, how they work and when to (and when NOT to) use them. This is actually a pretty big topic, and I think it is worth while giving you some background first, so I’ll split the post in twain - Part I is theoretical, Part II is practical. So get your network protocol hats on, and get ready to learn about the inner workings of the language of the web.

REST vs. CRUD

Where would the web be with out acronyms? REST stands for REpresentational State Transfer and describes resources (in our case URLs) on which we can perform actions. CRUD, which stands for Create, Read, Update, Delete, are the actions that we perform. Although, in Rails, REST and CRUD are bestest buddies, the two can work fine on their own. In fact, every time you have written a backend system that allows you to add, edit and delete items from the database, and a frontend that allows you to view those items, you have been working with CRUD.

About Me

Friendly and humanitarian Honest and loyal Original and inventive Independent and intellectual