Installing Ruby on Rails with RVM on Ubuntu

It is always a pain to set up Ruby on Rails on Ubuntu for someone who is new to command lines. With worked on .net for years I was really struggling to get ROR set up on ubuntu. After many failed attempts I came across below article from Ryan Bigg which is very simple and precise.

http://ryanbigg.com/2010/12/ubuntu-ruby-rvm-rails-and-you/


Tuesday, June 18, 2013
Posted by Pramod Chavan

.nil? .empty? .blank? .present? in Ruby on Rails


We get confused when there are many options to choose from. Same is the case when it comes to use any one from the above list. But one needs to be careful in using them and it is better that we understand it well before using it.

Let's see which method does what.

.nil?
- It is Ruby method
- It can be used on any object and is true if the object is nil.
- "Only the object nil responds true to nil?" - RailsAPI

nil.nil? = true
anthing_else.nil? = false
a = nil
a.nil? = true
“”.nil = false

.empty?
- It is Ruby method
- can be used on strings, arrays and hashes and returns true if:
String length == 0
Array length == 0
Hash length == 0
- Running .empty? on something that is nil will throw a NoMethodError

"".empty = true
" ".empty? = false

.blank?
- It is Rails method
- operate on any object as well as work like .empty? on strings, arrays and hashes.

nil.blank? = true
[].blank? = true
{}.blank? = true
"".blank? = true
5.blank? == false

- It also evaluates true on strings which are non-empty but contain only whitespace:
"  ".blank? == true"  ".empty? == false

Quick tip: 
!obj.blank? == obj.present?

activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9)
def present?
 !blank?
end
Friday, May 31, 2013
Posted by Pramod Chavan

COALESCE function in SQL

Definition:

Definition of COALESCE according to MSDN is “Returns the first non-null expression among its arguments.”
In general it is said : COALESCE() accepts a series of values and a value to use in the event that all items in the list are null; then, it returns the first not-null value. 
Example:
SELECT COALESCE(NULL, NULL, NULL, GETDATE()) 
SELECT FirstName + ' ' + COALESCE(MiddleName,'')+ ' ' + COALESCE(LastName,'')


Similar to CASE:
It is the same as the following CASE statement:

SELECT CASE ("column_name")
  WHEN "expression 1 is not NULL" THEN "expression 1"
  WHEN "expression 2 is not NULL" THEN "expression 2"
  ...
  [ELSE "NULL"]
  END
FROM "table_name"
For examples, say we have the following table,

Table Contact_Info
Name
Business_Phone
Cell_Phone
Home_Phone
Jeff
531-2531
622-7813
565-9901
Laura
NULL
772-5588
312-4088
Peter
NULL
NULL
594-7477

and we want to find out the best way to contact each person according to the following rules:

1. If a person has a business phone, use the business phone number.

2. If a person does not have a business phone and has a cell phone, use the cell phone number.

3. If a person does not have a business phone, does not have a cell phone, and has a home phone, use the home phone number.

We can use the COALESCE function to achieve our goal:

SELECT Name, COALESCE(Business_Phone, Cell_Phone, Home_Phone) Contact_Phone
FROM Contact_Info;

Result:
Name
Contact_Phone
Jeff
531-2531
Laura
772-5588
Peter
594-7477


A complex example:
In the following example, the wages table includes three columns that contain information about the yearly wages of the employees: the hourly wage, salary, and commission. However, an employee receives only one type of pay. To determine the total amount paid to all employees, use COALESCE to receive only the nonnull value found in hourly_wage, salary, and commission.
SET NOCOUNT ON;
GO
USE tempdb;
IF OBJECT_ID('dbo.wages') IS NOT NULL
    DROP TABLE wages;
GO
CREATE TABLE dbo.wages
(
    emp_id        tinyint   identity,
    hourly_wage   decimal   NULL,
    salary        decimal   NULL,
    commission    decimal   NULL,
    num_sales     tinyint   NULL
);
GO
INSERT dbo.wages (hourly_wage, salary, commission, num_sales)
VALUES
    (10.00, NULL, NULL, NULL),
    (20.00, NULL, NULL, NULL),
    (30.00, NULL, NULL, NULL),
    (40.00, NULL, NULL, NULL),
    (NULL, 10000.00, NULL, NULL),
    (NULL, 20000.00, NULL, NULL),
    (NULL, 30000.00, NULL, NULL),
    (NULL, 40000.00, NULL, NULL),
    (NULL, NULL, 15000, 3),
    (NULL, NULL, 25000, 2),
    (NULL, NULL, 20000, 6),
    (NULL, NULL, 14000, 4);
GO
SET NOCOUNT OFF;
GO
SELECT CAST(COALESCE(hourly_wage * 40 * 52,
   salary,
   commission * num_sales) AS money) AS 'Total Salary'
FROM dbo.wages
GO
Here is the result set.
Total Salary
------------
20800.0000
41600.0000
62400.0000
83200.0000
10000.0000
20000.0000
30000.0000
40000.0000
45000.0000
50000.0000
120000.0000
56000.0000
(12 row(s) affected)

If you want to pivot the data you could run the following command.
DECLARE @FirstName VARCHAR(1000)

SELECT @FirstName COALESCE(@FirstName ,'') + Name ';'
FROM User

SELECT @FirstName  AS ListOfNames
Output:
Pramod; Alex; Joe

COALESCE() or ISNULL()?

  • COALESCE() is ANSI standard, so that is an advantage for the purists out there.
     
  • Many consider ISNULL()'s readability and common sense naming to be an advantage. While I will agree that it easier to spell and pronounce, I disagree that its naming is intuitive. In other languages such as VB/VBA/VBScript, ISNULL() accepts a single input and returns a single boolean output.
     
  • ISNULL() accepts exactly two parameters. If you want to take the first non-NULL among more than two values, you will need to nest your ISNULL() statements. COALESCE(), on the other hand, can take multiple inputs:
     
SELECT ISNULL(NULL, NULL, 'foo') 
 
-- yields: 
Server: Msg 174, Level 15, State 1, Line 1 
The isnull function requires 2 arguments. 
 
SELECT COALESCE(NULL, NULL, 'foo') 
 
-- yields: 
---- 
foo

  • In order to make this work with ISNULL(), you would have to say:
     
SELECT ISNULL(NULL, ISNULL(NULL, 'foo'))
  •  
  • The result of ISNULL() always takes on the datatype of the first parameter (regardless of whether it is NULL or NOT NULL). COALESCE works more like a CASE expression, which returns a single datatype depending on precendence and accommodating all possible outcomes. For example:
     
DECLARE @foo VARCHAR(5) 
SET @foo = NULL 
SELECT ISNULL(@foo, '123456789') 
 
-- yields: 
-----  
12345 
 
SELECT COALESCE(@foo, '123456789') 
 
-- yields: 
---------  
123456789

  • This gets more complicated if you start mixing incompatible datatypes, e.g.:
     
DECLARE @foo VARCHAR(5), @bar INT 
SET @foo = 'foo' 
SET @bar = NULL 
 
SELECT ISNULL(@foo, @bar) 
SELECT COALESCE(@foo, @bar) 
 
-- yields: 
 
-----  
foo 
 
Server: Msg 245, Level 16, State 1, Line 6 
Syntax error converting the varchar value 'foo' to a column of data type int.

  • Finally, COALESCE() can generate a less efficient plan in some cases, for example when it is used against a subquery.
References:



Friday, April 29, 2011
Posted by Pramod Chavan
Tag :

Basics of Web Service for beginners

What are Web Services?

  • Web services are small units of code
  • Web services are designed to handle a limited set of tasks
  • Web services use XML based communicating protocols
  • Web services are independent of operating systems
  • Web services are independent of programming languages
  • Web services connect people, systems and devices

Small Units of Code

Web services are small units of code designed to handle a limited set of tasks.

An example of a web service can be a small program designed to supply other applications with the latest stock exchange prices. Another example can be a small program designed to handle credit card payment.


XML Based Web Protocols

Web services use the standard web protocols HTTP, XML, SOAP, WSDL, and UDDI.

HTTP

HTTP (Hypertext Transfer Protocol) is the World Wide Web standard for communication over the Internet. HTTP is standardized by the World Wide Web Consortium (W3C).

XML

XML (eXtensible Markup Language) is a well known standard for storing, carrying, and exchanging data. XML is standardized by the W3C.

SOAP

SOAP (Simple Object Access Protocol) is a lightweight platform and language neutral communication protocol that allows programs to communicate via standard Internet HTTP. SOAP is standardized by the W3C.

WSDL

WSDL (Web Services Description Language) is an XML-based language used to define web services and to describe how to access them. WSDL is a suggestion by Ariba, IBM and Microsoft for describing services for the W3C XML Activity on XML Protocols.

UDDI

UDDI (Universal Description, Discovery and Integration) is a directory service where businesses can register and search for web services.

UDDI is a public registry, where one can publish and inquire about web services.


Independent of Operating Systems

Since web services use XML based protocols to communicate with other systems, web services are independent of both operating systems and programming languages.

An application calling a web service will always send its requests using XML, and get its answer returned as XML. The calling application will never be concerned about the operating system or the programming language running on the other computer.


Benefits of Web Services

  • Easier to communicate between applications
  • Easier to reuse existing services
  • Easier to distribute information to more consumers
  • Rapid development

Web services make it easier to communicate between different applications. They also make it possible for developers to reuse existing web services instead of writing new ones.

Web services can create new possibilities for many businesses because it provides an easy way to distribute information to a large number of consumers. One example could be flight schedules and ticket reservation systems.

WSDL Describes Web Services

WSDL stands for Web Services Description Language.

WSDL is a document written in XML. The document describes a Web service. It specifies the location of the service and the operations (or methods) the service exposes.


http://dev.micashcard.com/?src=pramodblog


Monday, September 21, 2009
Posted by Pramod Chavan

.Net 4.0 framework features

What’s New in Visual Studio 2010?
UML: Now draw UML diagram right from your Visual Studio. No need to buy another tool or go to Visio to do that.
Application Architecture Re-engineering: You have a readymade code-solution, now you have to explain it to someone but have documents. VS2010 will help you to draw an application architecture using you application code. This way you get a big picture of your application and also all linkages between different classes. Good for new joiners in an existing team.
Stuff for Manual Testers: Manual tester can breathe a lot better with VS2010 tools, log in all your test cases and select the test case to run and click record and then execute your test case as usual and see what you get. If test case passes, you get evidence (most clients in service based companies want that) as videos or if the test case fails, you get the steps to reproduce as videos. Cool, isn’t it?
WCF 4.0
RESTful enhancements
Simplifying the building of REST Singleton & Collection Services, ATOM Feed and Publishing Protocol Services, and HTTP Plain XML Services using WCF
WCF REST Starter Kit to be released on Codeplex to get early feedback
Messaging enhancements
Transports - UDP, MQ, Local in-process
Protocols - SOAP over UDP, WS-Discovery, WS-BusinessActivity, WS-I BP 1.2
Duplex durable messaging
Correlation enhancements
Content and context driven, One-way support
Declarative Workflow Services
Seamless integration between WF and WCF and unified XAML model
Build entire application in XAML, from presentation to data to services to workflow
WF enhancements :
Significant improvements in performance and scalability
· Ten-fold improvement in performance
New workflow flow-control models and pre-built activities
· Flowcharts, rules
· Expanded built-in activities – PowerShell, database, messaging, etc.
Enhancements in workflow modeling
· Persistence control, transaction flow, compensation support, data binding and scoping
· Rules composable and seamlessly integrated with workflow engine
Updated visual designer
· Easier to use by end-users
· Easier to rehost by ISVs
Ability to debug XAML
New Languages
.NET 4.0 will include IronPython, IronRuby, and F#, among others.

Saturday, July 25, 2009
Posted by Pramod Chavan
Tag :

Auto redirect http to https in IIS



How one can redirect http request to https
automatically. As such there is no out of box IIS feature to redirect http to
https. Still there are a number of ways to achieve this, let’s see simpler one
by modifying IIS custom error page (403.4).

1. Create a file called "AutoRedirect.asp" in your root folder with the below
code:

<%

Data = request.servervariables("Query String")

URL = replace(Data, "403;", "")

URL = replace(URL, "http://", "https://")

response.Status = "200 OK"

response.redirect URL

%>

Or if you just have a single URL to redirect, you can change those lines above
with this instead

<% Response.Redirect("https://www.YourDomain.com/") %>

2. Open the IIS MMC tool, select properties for your site, go to the directory
security tab, click the edit button at the bottom in secure communications, and
then check "Require secure channel (SSL)" option.

3. In the IIS MMC tool under your site’s properties, go to the custom errors
tab, select the 403;4 error type, edit its properties to be a URL in your site
and change it to

/AutoRedirect.asp

4. Still in the IIS MMC tool, select the properties for the custom error file
you have created previously (AutoRedirect.asp), go to the file security tab,
click the edit button at the bottom in secure communications, and then UN-check
"Require secure channel (SSL)" option.

Just browse your website with http it will be automatically redirected to https.




Sunday, July 19, 2009
Posted by Pramod Chavan
Tag :

Virtual directory not being configured as an application in IIS.


I received the following an error when I was trying to host application with one sub application in it. Let me elaborate in details – the requirement was like this-

we required to add new module in an existing asp.net web site, which was going to be a new asp.net web application. So when the new application (let’s call it as a sub application) was ready for deployment we created a folder in root folder of existing application (let’s call it as main application) and copied all required files of sub application into the folder. Assuming everything is perfect we browsed main application it worked fine but when we tried browsing sub application we got an error saying "This error can be caused by a virtual directory not being configured as an application in IIS."
On Googling we found some interesting facts about it. Here are the details ….
However, this error occurs primarily out of 2 scenarios.


1. When you create a new web application using visual studio.net, it automatically creates the virtual directory and configures it as an application. However, if you manually create the virtual directory and it is not configured as an application, then you will not be able to browse the application and may get the above error. The debug information you get as mentioned above, is applicable to this scenario. To resolve it, Right Click on the virtual directory - select properties and then click on "Create" next to the "Application" Label and the textbox. It will automatically create the "application" using the virtual directory's name. Now the application can be accessed.


2. When you have sub-directories in your application, you can have web.config file for the sub-directory. However, there are certain properties which cannot be set in the web.config of the sub-directory such as authentication, session state (you may see that the error message shows the line number where the authentication or sessionstate is declared in the web.config of the sub-directory). The reason is, these settings cannot be overridden at the sub-directory level unless the sub-directory is also configured as an application (as mentioned in the above point). Mostly we have the practice of adding web.config in the sub-directory if we want to protect access to the sub-directory files (say, the directory is admin and we wish to protect the admin pages from unathorized users). But actually, this can be achieved in the web.config at the application's root level itself, by specifing the location path tags and authorization, as follows:-

<location path="Admin">
<system.web>
<authorization>
<allow roles="administrators" />
<deny users="*" />
</authorization>
</system.web>
</location>

However, if you wish to have a web.config at the sub-directory level and protect the sub-directory, you can just specify the Authorization mode as follows:-

<configuration>
<system.web>
<authorization>
<allow roles="administrators" />
<deny users="*" />
</authorization>
</system.web>
</configuration>

Thus you can protect the sub-directory from unauthorized access.
Friday, November 7, 2008
Posted by Pramod Chavan

- Copyright © Bits, bytes and errors - Powered by Blogger -