Configurable ActiveResource Site Variable
Documentation on ActiveResource is fairly thin on the ground.
If you are using ActiveResource in the real world it is likely that you will want to use different webservices depending on what environment you are in. (Staging, Development, CI, Production etc.)
Heres how to do it. This assumes the REST api has been put under the api namespace and has been setup with HTTP Authentication.
raw_config = File.read(RAILS_ROOT + "/config/config.yml") GLOBAL = YAML.load(raw_config) APP_CONFIG = GLOBAL[Rails.env]
development: webservice: http://username:password@localhost:3001/api/ test: webservice: http://username:password@localhost:3001/api/ staging: webservice: https://username:firstname.lastname@example.org/api/ production: webservice: https://username:email@example.com/api/
class IncomeType < ActiveResource::Base self.site = APP_CONFIG['webservice'] end
Here is a great railscast on the subject.