Friday, October 19, 2012

Devise , omniauth and rails application

Alright this is for all folks out there who think devise and omniauth will make your life easy to make you app login to fb , nope its not easy and required a lot of google search and experimentation here and there to make things happen .

So the requirement was such to allow to facebook login from the app , I chose omniauth facebook plugin for same .

Gems you need to define in your app are --



please notice the git links given beside the gems , believe me the default gems do not work for you . I had to google , regoogle try and find out stuff and finally !!! this is the correct  combination of gems which you need for your tasks

Ok execute --

 bundle install

And configure devise as provided in devise documentation

https://github.com/plataformatec/devise

Create omniauth.rb in configuration/initializers directory

place following code in the file



We will need to create one more model name Authentications to store authentication details of a user , in case he arrives from facebook

 rails g model Authentications user_id:integer provider:string uid:string token:string

Maintain one to many relationship between authentications and user so here goes code accordingly

in authentications.rb

belongs_to user

in users.rb

 has_many :authentications, :dependent => :delete_all

We need to add one callback controller function for omniauth to call . For the same purpose we will make one controller Authentications in our application

 rails g controller Authentications

Create a method name "create" with in controller called while callback


I am maintaining a session variable over here to distinguish a fb session than a normal login session , you can follow some other approach of urs if you want

create apply_omniauth in user.rb



Configure route for Authentications Controller in routes.rb

match '/auth/facebook/callback' =>  'authentications#create'

Finally place a link in your application.html.erb


<%= link_to "Sign in with Facebook", '/auth/facebook/'>


Alright so from here on you can successfully let people sign in with there fb accounts to your application

Monday, May 9, 2011

Blackberry Development, An initiation

Blackberry Development, An initiation :-

Blackberry development is a hot topic now a days especially due to its own Blackberry Enterprise Server feature. Blackberry is surely a platform to look forward to due to great security features known to every body. I had been looking into this aspect since last some days and have done some decent hands on.
Developers.blackberry.com is the place to start from, unlike Android development, I found the documentations provided on blackberry website to be a little difficult to understand, but this is the place where you will get all the resources and documents .

Blackberry development can be of two forms: -
1)      Blackberry Webworks
2)      Blackberry java development

Blackberry Webworks is for the developers who are looking at browser based apps using the traditional html, CSS and javascript concepts

Blackberry java development is for the developers who want to develop native java client applications to perform some critical tasks like some database based operations or filesystem based operation

I have explored java development side of blackberry, Blackberry development is based on JAVA ME concepts. Rather any JAVA ME app can run on blackberry successfully, but additional to it blackberry has got some its own application frameworks, GUI frameworks and lot of other things to rely upon.  

For the blackberry java development kit to get installed on your system, following steps need to be followed: -

1)      download Blackberry java plugin from http://us.blackberry.com/developers/javaappdev/devtools.jsp
2)      Make sure your system has JDK 1.6 and a system with more than two JDKs configured can be a problem , if you have both JDK 1.5 and 1.6 better uninstall 1.5
3)      Double click on the set up and it will do the rest for you , you can change the installation directory if  you want


How to start application development for Blackberry Java

n      There are many sample application available with the plugin installed in your system ,
n      You can refer the documentation provided on site for reference – there are some GUI develop guides available that should be enough to start with

GUI development for Blackberry: -

            There are two parts for any blackberry project – An application and screen. Application is the point from where the initiation of the project starts. You define your default screen and initiation parameters here

A Screen class is what is rendered as a screen on your blackberry app.

Here is my sample login screen, how I had designed: -
1:  VerticalFieldManager vfm = new VerticalFieldManager();  
2:  LabelField labelScreen = new LabelField("LABEL_NAME",FIELD_HCENTER);  
3:  labelScreen.setFont(getFont().derive(Font.ITALIC));  
4:  labelScreen.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 3, 0)));  
5:  labelScreen.setMargin(0,0,20,0);  
6:  vfm.add(labelScreen);  
7:  vfm.setBorder(Border.STYLE_SOLID, BorderFactory.createSimpleBorder(new XYEdges(2,2,2,2)));  
8:  vfm.setMargin(20, 20, 20, 20);  
9:  HorizontalFieldManager hfmLogin = new HorizontalFieldManager();  
10:  LabelField labelLogin = new LabelField("LOGIN_LABEL : -");  
11:  labelLogin.setFont(getFont().derive(Font.BOLD));  
12:  hfmLogin.add(labelLogin);  
13:  EditField loginId =new EditField();  
14:  loginId.setBorder(BorderFactory.createBevelBorder(new XYEdges(TOPMOST, RIGHTMOST, BOTTOMMOST, LEFTMOST)));  
15:  loginId.setMargin(0, 0, 20, 0);  
16:  hfmLogin.add(loginId);  
17:  vfm.add(hfmLogin);  
18:  HorizontalFieldManager hfmPass = new HorizontalFieldManager();  
19:  LabelField labelPass = new LabelField("PASSWORD_LABEL : - ");  
20:  labelPass.setFont(getFont().derive(Font.BOLD));  
21:  hfmPass.add(labelPass);  
22:  PasswordEditField password = new PasswordEditField();  
23:  password.setBorder(BorderFactory.createBevelBorder(new XYEdges(TOPMOST, RIGHTMOST, BOTTOMMOST, LEFTMOST)));  
24:  password.setMargin(0,0,20,0);  
25:  hfmPass.add(password);  
26:  vfm.add(hfmPass);  
27:  ButtonField submitLogin = new ButtonField("Login",FIELD_HCENTER);  
28:  submitLogin.setEnabled(true);  
29:  vfm.add(submitLogin);  
30:  submitLogin.setCommand(new Command(new CommandHandler() {  
31:  public void execute(ReadOnlyCommandMetadata metadata, Object context) {  
32:     UiApplication.getUiApplication().pushScreen(new MyScreen());  
33:           }  
34:        }));  
35:  this.add(vfm);  

As can be inferred from the code above, I have used Vertical layout. There are three types of layouts available: -

1)      Vertical
2)      Horizontal
3)      Flow
4)      Grid

I have assigned action to my submit button by assigning it a command, here I am pushing a new screen onto the application stack on the action of button click

This is it for GUI part of Blackberry; I dealt with the database part as well. But I think that will need one more blog entry

Reference Site: -
            http://developers.blackberry.com

Tuesday, January 11, 2011

A little About Spring Framework

Had been Going through this framework since many days, so just tried consolidating whatever I observed in my own language. Spring works on a very interesting concept
Dependency Injection , A little related to Factory pattern. Here I am putting a little relevance of relation between Spring and factory pattern
Factory pattern :- One object to conrol the creation of and/or access to other objects

Let us see some practical example

public class A implements {
               int number;
        A( int no ){
               this.number = no;
               }

          public int getNumber(){
                return a;
          }

}

public class B {
        String abc ;
          B(String efg){
          this.abc = efg;
        }

       public String getAbc(){
     return abc;
     }

}
Now some class C needs to use A and B , 

public class C{

A a = new A(1);
  B b = new B("Some thing");

}

IF factory pattern is used , it is responsible for all the initializations
and formalities which are required for creating an object

public class ObjFactory{

public static A getA(int input){
           return new A(input);
          }

      public static B getB(String input){
         return new B(input);
          }
}

code for client class is :- 
public class C{

public static void main(String args[]){
int input = 0;
ObjFactory factory = new ObjFactory();
A a = factory.getA(1);
B b = factory.getB("Some String");
}
}

So technically we are centralizing the generation of object , this will reduce our work in sense , any alteration in the way object is generated if needs to be done can be done in factory instead touching client code
Same is the concept of Spring framework

Dependency Injection :- Instead of you declaring the objects , the instances are injected to the client code , DI is achieved in Spring using an xml , called beans.xml , which works for you to instantiate all the beans in your application


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
     <bean name="A"
          class="examples.spring.A"
          singleton="true">
          <constructor-arg>
               <value>"Smriti"</value>
          </constructor-arg>  
     </bean>
     <bean name="B"
          class="examples.spring.B"
          singleton="true">
          <constructor-arg>
               <value>12<value>
          </constructor-arg>
     </bean>
</beans>

In the above xml file we are passing arguments to the constructor with the help of value element which can be used for any java native types , let it be int, long , spring , double etc.


InputStream is = new FileInputStream("src/examples/spring/beans.xml");
BeanFactory factory = new XmlBeanFactory(is);

A a = (A)factory.getBean(10);
int result = a.getNumber();

This is about the basic idea for Spring.
Please pardon me for using very generic examples , those are basic for general understanding only

references :-
http://www.devx.com/Java/Article/21665
http://www.oodesign.com/factory-pattern.html