r/learnruby • • Sep 13 '14
How do API wrappers utilize instance methods?
I wasn't sure how to phrase the title, but I'm learning how to write a good API wrapper right now. Design and implementation, really.
I keep seeing method chaining that looks like this:
client.users.show? :screen_name=>'some_user' #http://twitter.com/users/show.json?screen_name=some_userTaken from https://github.com/hayesdavis/grackle
Which is neat but I'm unsure how that users method is defined. I don't see a method definition for users. How are those chains built because I see that style everywhere but I can't figure out how they're doing it.
I'm only really familiar with segregating code responsibilities using modules and classes so I'd have something like this:
list_o_users = Client::Users.search('query')or
user = Client::Users.new list_o_users[0]Or something like that.
Let me know if anymore information is needed. I'm not sure what the technical terms would be for building those method chains.
Any and all help is appreciated.
View all comments
Show parent comment
1
u/[deleted] Sep 14 '14
I'll see what I can do. But trickiness like that seems common. What I've just done for my wrapper was take an instance method and assign it to a particular part of the URI.
Seems to work well enough... but I'm still really curious.