views:

63

answers:

3

I have the following class that I'd like access to within certain controllers in my app:

class Spreedly
  include HTTParty
  base_uri 'https://example.com/api/1234'
  basic_auth 'user', 'xyz124'
  headers 'Accept' => 'text/xml'
  headers 'Content-Type' => 'text/xml'
  format :xml  
end

Where would I put that class so that I could then access it in a controller like Spreedly.post(xyz)?

+2  A: 

You can simply place this in a file called spreedly.rb in the lib/ directory. It'll be auto-loaded by rails and will be available for use.

Siddhartha Reddy
Actually, if you put them on lib/, you may need to add a file called spreedy.rb on your config/initializers, that requires it for all your app: require 'spreedy.rb¡
egarcia
A: 

True, but there is nothing stopping you from putting it in your app/models directory.

And to me it seems a better place to put it - because your are accessing data like any other model in your app, and when you reference Spreedly.post etc from your controllers another programmer will naturally (imho) look in the app/models directory first before the /lib ..

Jason Cale
A: 

Anything that needs to be referenced in multiple places or anything that is large, I put in lib. If it is tiny, and if it is used in a single model, I will put it in the same model file.

cgr