Hi, I have just created a couple of ActiveResource classes that are being used to communicate with a rest api. In every request I am having to place an api key as an additional param e.g. @book = Book.find(params[:id], :params => {:key => ''7348d92f82836eb9d5f69c0''}) Is anyone aware of a dry''er solution that will allow me define the additional param in the class below self.site or something similar? Thanks, Scott -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-Feb-25 20:37 UTC
Re: ActiveResource, how to add a param to every request?
On 2/25/08, Scott A S <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > I have just created a couple of ActiveResource classes that are being > used to communicate with a rest api. > > In every request I am having to place an api key as an additional param > e.g. > @book = Book.find(params[:id], :params => {:key => > ''7348d92f82836eb9d5f69c0''}) > > Is anyone aware of a dry''er solution that will allow me define the > additional param in the class below self.site or something similar?Maybe something like this (untested): class Book < ActiveRecord::Base cattr_accessor :api_key def find(*args, &blk) with_options(:key => class.api_key) do super end end end Then somewhere (in config?) Book.api_key = ''7348d92f82836eb9d5f69c0'' -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cool, Cheers Rick> > class Book < ActiveRecord::Base > > cattr_accessor :api_key > > def find(*args, &blk) > with_options(:key => class.api_key) do > super > end > end> -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
After looking into a way to allow an additional param to also be sent for create and update requests, I have come across a patch that does exactly this. http://dev.rubyonrails.org/ticket/10088 Does anyone know when this is likely to make its way into edge? Cheers, Scott Scott A S wrote:> Hi, > > I have just created a couple of ActiveResource classes that are being > used to communicate with a rest api. > > In every request I am having to place an api key as an additional param > e.g. > @book = Book.find(params[:id], :params => {:key => > ''7348d92f82836eb9d5f69c0''}) > > Is anyone aware of a dry''er solution that will allow me define the > additional param in the class below self.site or something similar? > > Thanks, > Scott-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---