Python hisses – 1

I just started learning Python, and came across two handy functions.

We want to know to a basic thing about an object. What are its attributes and methods?

The answer is dir(object). It will return all the attributes and methods, including the inherited ones.

To dig deeper, what if we want to know what arguments a function takes in? The function might be related to the object we just inspected. To get the arguments do the following:

import inspect

def func(arg1, arg2, arg3=5):
pass

inspect.getargspec(func)
(['arg1', 'arg2', 'arg3'], None, None, (5,))

Hope this helps you sometime!

The Rails rails

Development in Rails has steep learning in comparison to PHP. In Rails, things are scattered and one needs to take care of many things at once. Sometimes you will need four terminals!

Here are some things you will or may need in your journey on the “Rails” tracks.

1. Learning Rails

Tutorials by Micheal Hartl covers Ruby on Rails in depth. It is best way to dive deep in Rails as well as Ruby. Check out the site. The FAQs cover more info on the framework. To cover the basics, video tutorials from Lynda.com are great too.

2. Installation

I suggest you use Linux if you are developing on Rails. Refer this for installation, if you don’t want to follow the instructions given in tutorials.

3. Brief Working

Once you create a new app with Rails, you will find three most important directories in “app” directory: models, controllers, views. This is expected as rails is MVC (Models, views, controllers) framework.
Models define characteristics, attributes and functions of an object of your system. Example: A book is a model; Name and author are its attributes, when observed in Object oriented way.
Controllers are responsible for catching requests from your client, talking to your databases and serving what was asked by the user. Example: When you click “show me all books” on a website, an action (let’s say all_books function) of a particular controller, say BooksController, is called. Ruby code in the function fetch all books from the database and serves an HTML page. The views define this HTML code.

4. Connecting with HTML

The data in controller function is stored in variables which are accessible in the related view file.
By default, If you have “something_controller”, you will have directory named “something” in views. The HTML file will be named after the controller action. Rails follows these conventions, but you can change the configuration. Usually, we work with prioritizing “Convention over Configuration”.
Usually, the extension of the view file is html.erb. (Another one is html.haml). “erb” stands for Embedded Ruby, signifying its sync with Html. Using syntax like <%= @variable %> the variables are accessed in views.

5. Database configuration

Config directory will help you set environment variables and database. Check auto-generated config/database.yml.

6. Console and Server

You can experiment with Ruby and your entire system from the console.

rails c

Before writing code in models or controller, initially you can experiment it here. Lastly, by starting your server you can see your app at http://localhost:3000. Start the server by typing:

rails s

7. Gems

You can plug many readily available libraries by specifying them in “Gemfile” and bundling them.

8. More Resources

Railscast by Ryan Bates covers almost everything related to Rails in a modular fashion.
These link will drop you to official documentation and will be handy at times.
You may also need the official guide for Ruby Language.

Image

Make New Year Happy

With advent of 2015, our hopes and expectations from the new era takes height. Dream high, work hard and achieve success. Live like a bird, fly with freedom, observe the beauty of the world and find all that’s awesome in everyone.

People make new year’s resolution showing strong urge for deliverance. Below are the most important things to follow and be happy.

Promise yourself:

  1. To be so strong that nothing can disturb your peace of mind.
  2. To be too large for worry, too noble for anger, too strong for fear and too happy to permit the presence of trouble.
  3. To talk health, happiness and prosperity to every person you meet.
  4. To look at the sunny side of everything and make my optimism come true.
  5. To think only of the Best, to work only for the Best and to expect only the Best.
  6. To forget the mistakes of the past and press on the greater achievements of the future.
  7. To wear a cheerful expression at all times and give a smile to every living creature you meet.
  8. To give so much time improving yourself that you have no time criticizing others.
  9. To think well of yourself and to proclaim this fact to the world, not in loud words, but in great deeds.
  10. To have faith in the universe and yourself 🙂

Much of the above inspiration is from The Secret by Rhonda Byrne.

Start working hard from today and never waste time.

“One thing you can learn by watching the clock: It passes the time by keeping its hands busy.”

You don’t need read inspirational stuff everyday. Remember words from Andy Dufresne in The Shawshank Redemption:

“Salvation lies within”

Let’s make this New Year Happy in real sense. Let’s make Earth a happier place to live.

Token based registration/Invitations in a nutshell (Ruby on Rails)

This post is all about registrations methods used by various sites. Mainly secure (token) based registration. Majorly, I will be providing links for many related resources and solutions. Here, I will summarize many scenarios applicable to apps based on Ruby on Rails.

Firstly, you might want to go through Token Based Authentication in Rails published by Envy Labs. It describes the most easiest way through which an app can implement “token” system. Though it stores the tokens in database, similar approach can be adopted after integrating with some cryptographic functions (about which I’ll discuss later). As an important link shared on the same page HTTP Token Access Authentication scheme, defines the exact terminology and protocol. One may have to go through certain pages, but its quite worthy. If you short descriptions, jump to google and put such questions. Many will help you find a way.

If your app is widely distributed or hosted on different platform you will need to your webapp to respond to several APIs or even android applications. This blog defines exact solution to do so. It is applicable to people using Devise gem for user registration, authentication.

There can situation where you want to distribute the token to people using invitation models. For instance, lets say your app’s is in  beta phase, and you want limited people to use it, you can implement Beta invitation model. With little bit of brain storming, similar approach can be followed when you have to let your user invite other people.

Many websites have tedious registration process. It drains the user before he/she can peacefully enjoy pretty short facilities. If you don’t want the user to close your site for such reasons, Lazy registration can be quite useful. Here without registering the user can enjoy the features, may be in limited extent.

Okay, coming to “token” system again, much of inspiration can be taken from devise_invitable gem.

But what if we don’t want to store the tokens into database. Of course we need to make the token and sign on it such a way that when a user uses it for the purpose, we are sure of two things:

  1. That the token is the same as what was given to that particular user.
  2. the token is not fake or is not generated by false means or by unauthorized person.

The above two factors can be achieved the following (respectively):

  1. Encrypt the token with user specific data, which can be verified again when the distributed token is used.
  2. Link a unique sign on the token, by which you will come to know about its origin.

By this, it follows that by encrypting and signing all problems can be halted. But should should be done first encryption or signing? Not that, simply signing and encrypting will work, some papers found faults in it and have added much to provide more security.

For encryption-decryption many gems and scripts are used. Though what is contained in this list is related to ActiveRecord, but it can roughly guide you in right direction.

I am assuming that you have atleast little knowledge of Public Key Infrastructure (PKI). Manual encrypting may involve using simple RSA Algorithm (or gem) or broader PGP (or its gem). Got confused? Resolve the difference between two. You might also want to use OpenSSL for generating the public and private keys. Recently I found two blogs : Encrypting Sensitive Data With Ruby (on Rails) and its extension. Both are well written to suceed in guiding someone step by step. The third one by the same person, describes about generating RSA Key Pairs in Ruby. If you are not a cryptographer, you might get confused in understanding difference in PGP and OpenSSL, and there applications.

For now, I am not going further into using encrypting-decrypting scripts, and hash functions. I may write about exact information in the next post or write in continuation.

Concluding on a lighter note, and going back to the title “Invitations”, you can invite friends from Facebook as given here. Gmail is most interesting tool we are using today.  If you want to import gmail contacts you need to wirte some scripts by your own  or use some “contacts” interface.

Hope all links help you in some or the other way.