Including common code among multiple web services issue?


#1

Hi all -

I’m implementing some web services and every one has at the very least a
“find_newer_than” method.

In my models, I’ve split things up like this and it works great.


class XyzService < WebService
web_service_api XyzApi
… service specific methods go here…
end

class WebService < ActionWebService::Base
def find_newer_than(created_at)
real_class.find(:all,
:conditions => [‘created_at >= ?’,
TimeZone[‘London’].unadjust(created_at)])
end

def real_class
Class.const_get(self.class.to_s.gsub(/Service$/, ‘’))
end

end

I’d like to do the same for the API’s, but can’t quite get there. If I
try the same logic as above then when I go to /api/invoke all the
methods show up as being of the “WebService” class.

Right now I’ve got this and it works. I’d like to move that api_method
line into a common file though. Since subclassing the Api’s another
level didn’t work I’ve tried both require and load, but require doesn’t
work at all, and load gives me an unknown method error for
Object#api_method.


class XyzApi < ActionWebService::API::Base

api_method :find_newer_than,
:expects => [{:created_at => :datetime}],
:returns => [[Class.const_get(self.name.to_s.gsub(/Api$/, ‘’))]]

end

So, what I really want is to move that code into it’s own file and get
ruby to simply insert it as is into each of my files and then parse
it.

Similar to PHP’s include() function.

Suggestions?

THanks!

-philip