Instead of using hard-coded values in the application, better to keep those in the configuration file and while needed just parse it into an array and use it in your application. Here I am going to discuss how to create a custom configuration file and load it as per the environment(developement, testing, production) in ror(Rails3).
While building an application in rails3, some times we need to use hard-coded values. While modifying those values we need to search it in our application which is not an optimum way. So better to keep these hard-coded values in one place and use them in our application. Please find the following example.
Here I am going to integrate twitter API in my application. I have created an application in my twitter account and got its api keys. Now I am going to put it in my code (in the twitts action of PublicController).
class PublicController < ApplicationController
def twitts
   require 'rubygems'
   require 'twitter'
   Twitter.configure do |config|
      config.consumer_key = 'UfALvg2rD6BiYx7lhD8'
      config.consumer_secret = 'VxwPldnK9jJhdwNrqwVdIE53svslTd9e6yvnKv727'
      config.oauth_token = '352940157-IbRWcPZWsXYZRjvFUkJLMpQaIA3VZLQfJtYR'
      config.oauth_token_secret = 'I9VokHULqwjzvhNKo4IrXBL7ihyyeeHu8f37wu'
   end
 
# code to perform your task with twitter API
end
end
In the above example, I have used the hard-coded values in my application. Now in the following, we will place these values in a configuration file in the config folder.
For that in rails3, we have to create an YML file in the config folder. Here I have created a YML file named as twitter.yml(config\twitter.yml). Please find the content of that file in the followings.
# Developement env development: consumer_key: UfALvg2rD6BiYx7lhD8 consumer_secret: VxwPldnK9jJhdwNrqwVdIE53svslTd9e6yvnKv727 oauth_token: 352940157-IbRWcPZWsXYZRjvFUkJLMpQaIA3VZLQfJtYR oauth_token_secret: I9VokHULqwjzvhNKo4IrXBL7ihyyeeHu8f37wu #test env test: consumer_key: [Testing CONSUMER KEY] consumer_secret: [Testing CONSUMER SECRET] oauth_token: [Testing OUATH TOKEN] oauth_token_secret: [Testing OUATH TOKEN SECRET] #production env production: consumer_key: [Production CONSUMER KEY] consumer_secret: [Production CONSUMER SECRET] oauth_token: [Production OUATH TOKEN] oauth_token_secret: [Production OUATH TOKEN SECRET]
After that we need to use these values in our code. So here is the modified code for twitts action of PublicController.
class PublicController < ApplicationController
   def twitts
      require 'rubygems'
      require 'twitter'
      # load the twitter.yml and parse the value into TWITT_CONFIG array as per the environment
      TWITT_CONFIG = YAML.load_file("#{Rails.root}/config/twitter.yml")[RAILS_ENV]
      Twitter.configure do |config|
         config.consumer_key = TWITT_CONFIG['consumer_key']
         config.consumer_secret = TWITT_CONFIG['consumer_secret']
         config.oauth_token = TWITT_CONFIG['oauth_token']
         config.oauth_token_secret = TWITT_CONFIG['oauth_token_secret']
      end
      # code to perform your task with twitter API
   end
end
Now when required you can change the hard-coded values in the twitter.yml without searching through out your codebase and it automatically reflects in your application.
