[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Better way to do this? Currently using two method_missing calls...

Dan

8/3/2007 11:50:00 PM

Hi,

I'm building a wrapper around a web service and I'd like to have my
class simply pass through calls to the remote service (since the
remote service will be adding new objects and methods faster then I'll
be updating the library, I need my class to be accept anything).

For example, if my ruby class is named API, I want to be able to call
API.images.find_by_name and have my class pass along "images" and
"find_by_name" to the remove service. I've got this working using two
method_missing calls (one as a class method and one as an instance
method), but it looks sloppy and I was wondering if there was a better
way to do this. Any ideas?

class API
def self.method_missing(remote_object)
api = API.new
api.instance_variable_set("@remote_object", remote_object)
return api
end
def method_missing(remote_method, *args)
API.call(@remote_object, remote_method, args)
end
def self.call(remote_object, remote_method, args)
puts "#{remote_object}.#{remote_method} #{args}"
end
end

Thanks!
Dan

14 Answers

Xavier Noria

8/4/2007 12:03:00 AM

0

El Aug 4, 2007, a las 1:50 AM, Dan escribió:

> I'm building a wrapper around a web service and I'd like to have my
> class simply pass through calls to the remote service (since the
> remote service will be adding new objects and methods faster then I'll
> be updating the library, I need my class to be accept anything).

Wouldn't the standard delegator library suffice? The description in
the Pickaxe is more complete than the rdoc shown by ri.

-- fxn


Dan

8/4/2007 11:26:00 AM

0

Thanks for the reply. I'm probably being dense here, but I don't see
how Delegator would help in this scenario. Can you explain what you
mean?

Thanks!
Dan

On Aug 3, 8:02 pm, Xavier Noria <f...@hashref.com> wrote:
> El Aug 4, 2007, a las 1:50 AM, Dan escribi?:
>
> > I'm building a wrapper around a web service and I'd like to have my
> > class simply pass through calls to the remote service (since the
> > remote service will be adding new objects and methods faster then I'll
> > be updating the library, I need my class to be accept anything).
>
> Wouldn't the standard delegator library suffice? The description in
> the Pickaxe is more complete than the rdoc shown by ri.
>
> -- fxn


Xavier Noria

8/4/2007 1:27:00 PM

0

El Aug 4, 2007, a las 1:29 PM, Dan escribió:

> Thanks for the reply. I'm probably being dense here, but I don't see
> how Delegator would help in this scenario. Can you explain what you
> mean?

I guess the one being dense was me then :-).

You are dispatching everything to some object dynamically. That's
what the delegator library is made for and that's why it rang a bell
when you asked for alternatives to that implementation. It is just an
idea for you to see whether it is worthwile compared to your approach.

According to the code there are two levels of catchall: The first one
is a class-level catchall, it creates a new object out of something
like API.images. The second one dispatches to that object somehow.
Delegation would target the former so the class-level method_missing
would return an object that already has delegation builtin.

Without seeing the real intented usage I can't be more specific. It
could be that the way to call the remote service only uses symbols
and there's no real object locally. If that was the case the
delegator library wouldn't fit.

-- fxn


Dan

8/4/2007 2:20:00 PM

0

Ok, I see where you were going now. I guess I could go in that
direction, but since sending functions and objects to the web service
is just done via POST params, so I'd rather route all the requests to
the "call" class method on my API class. Any ideas on how to do this
without having to create a instance of the API class (or any class)
just to store the "object" name ?

Dan

On Aug 4, 9:27 am, Xavier Noria <f...@hashref.com> wrote:
> El Aug 4, 2007, a las 1:29 PM, Dan escribi?:
>
> > Thanks for the reply. I'm probably being dense here, but I don't see
> > how Delegator would help in this scenario. Can you explain what you
> > mean?
>
> I guess the one being dense was me then :-).
>
> You are dispatching everything to some object dynamically. That's
> what the delegator library is made for and that's why it rang a bell
> when you asked for alternatives to that implementation. It is just an
> idea for you to see whether it is worthwile compared to your approach.
>
> According to the code there are two levels of catchall: The first one
> is a class-level catchall, it creates a new object out of something
> like API.images. The second one dispatches to that object somehow.
> Delegation would target the former so the class-level method_missing
> would return an object that already has delegation builtin.
>
> Without seeing the real intented usage I can't be more specific. It
> could be that the way to call the remote service only uses symbols
> and there's no real object locally. If that was the case the
> delegator library wouldn't fit.
>
> -- fxn

Xavier Noria

8/4/2007 2:56:00 PM

0

El Aug 4, 2007, a las 4:20 PM, Dan escribió:

> Ok, I see where you were going now. I guess I could go in that
> direction, but since sending functions and objects to the web service
> is just done via POST params, so I'd rather route all the requests to
> the "call" class method on my API class. Any ideas on how to do this
> without having to create a instance of the API class (or any class)
> just to store the "object" name ?

The idea is then that something like

API.images.find_by_name(name)

translates to a POST request where "images" and "find_by_name" are
string values?

-- fxn


Dan

8/4/2007 3:02:00 PM

0

On Aug 4, 10:55 am, Xavier Noria <f...@hashref.com> wrote:
> El Aug 4, 2007, a las 4:20 PM, Dan escribi?:
>
> > Ok, I see where you were going now. I guess I could go in that
> > direction, but since sending functions and objects to the web service
> > is just done via POST params, so I'd rather route all the requests to
> > the "call" class method on my API class. Any ideas on how to do this
> > without having to create a instance of the API class (or any class)
> > just to store the "object" name ?
>
> The idea is then that something like
>
> API.images.find_by_name(name)
>
> translates to a POST request where "images" and "find_by_name" are
> string values?
>
> -- fxn

exactly

Xavier Noria

8/5/2007 7:09:00 PM

0

El Aug 4, 2007, a las 5:05 PM, Dan escribió:

>> The idea is then that something like
>>
>> API.images.find_by_name(name)
>>
>> translates to a POST request where "images" and "find_by_name" are
>> string values?
>
> exactly

Yeah, I would have written the same implementatcion than you to
support that usage.

Perhaps API.images would return a different class instead of API
itself with its own method missing depending on the role of that
API.images conceptually, but that would be essentially the same
approach anyway.

Looks fine to me.

-- fxn


Robert Klemme

8/6/2007 6:53:00 AM

0

2007/8/4, Dan <dan.gottlieb@gmail.com>:
> Ok, I see where you were going now. I guess I could go in that
> direction, but since sending functions and objects to the web service
> is just done via POST params, so I'd rather route all the requests to
> the "call" class method on my API class. Any ideas on how to do this
> without having to create a instance of the API class (or any class)
> just to store the "object" name ?

This is what I'd do (attached). Note, you need to know the number of
invocations in this example. The pattern could however use other
criteria for triggering the action.

Kind regards

robert

Dan

8/7/2007 10:12:00 AM

0

On Aug 5, 3:09 pm, Xavier Noria <f...@hashref.com> wrote:
> El Aug 4, 2007, a las 5:05 PM, Dan escribi?:
>
> >> The idea is then that something like
>
> >> API.images.find_by_name(name)
>
> >> translates to a POST request where "images" and "find_by_name" are
> >> string values?
>
> > exactly
>
> Yeah, I would have written the same implementatcion than you to
> support that usage.
>
> Perhaps API.images would return a different class instead of API
> itself with its own method missing depending on the role of that
> API.images conceptually, but that would be essentially the same
> approach anyway.
>
> Looks fine to me.
>
> -- fxn

Thanks - I appreciate your working this through with me.

Dan

8/7/2007 10:17:00 AM

0

On Aug 6, 2:53 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2007/8/4, Dan <dan.gottl...@gmail.com>:
>
> > Ok, I see where you were going now. I guess I could go in that
> > direction, but since sending functions and objects to the web service
> > is just done via POST params, so I'd rather route all the requests to
> > the "call" class method on my API class. Any ideas on how to do this
> > without having to create a instance of the API class (or any class)
> > just to store the "object" name ?
>
> This is what I'd do (attached). Note, you need to know the number of
> invocations in this example. The pattern could however use other
> criteria for triggering the action.
>
> Kind regards
>
> robert
>
> call-recorder.rb
> 1KDownload

Hi Robert,

Thanks for the reply. I was wondering if that there was a way to do
this sort of thing in ruby without having to create a class instance
just to store the method calls. However, it looks like that's not
possible, which I kind of understand. Not perfect for my situation,
but it should be good enough.

Dan