Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: is my api wrapper abstraction ok (is it perlish?)

by Arunbear (Parson)
on Jan 19, 2013 at 18:01 UTC ( #1014222=note: print w/ replies, xml ) Need Help??


in reply to is my api wrapper abstraction ok (is it perlish?)

OOP is about hiding low level operations. It's about delegating, not micro-managing. E.g. at a restaurant you give the waiter your order, and they bring your meal. The consumer code above would be like the waiter bringing back the cooking tools and letting you prepare the meal.

Here is a (hopefully) more obvious interface (and a simpler implementation)

package main; use strict; use v5.10; my $obj = My::StackExchange->new; my $uri; $uri = $obj->build_uri( type => 'answers', ids => [123,134,145], comments => 1, options => { order => "desc", sort => "votes" } ); say $uri; $uri = $obj->build_uri( type => 'answers', options => { order => "desc", sort => "votes" } ); say $uri; package My::StackExchange; use URI::FromHash qw( uri ); sub new { bless {} } sub build_uri { my ($self, %arg) = @_; #TODO validate input my @paths = ($arg{type}); if ( $arg{ids} ) { push @paths, join(';', @{ $arg{ids} }); } if ( $arg{comments} ) { push @paths, 'comments'; } return uri( scheme => 'http', host => 'api.stackexchange.com', path => \@paths, query => $arg{options}, ); } __DATA__ http://api.stackexchange.com/answers/123;134;145/comments?sort=votes;o +rder=desc http://api.stackexchange.com/answers?sort=votes;order=desc


Comment on Re: is my api wrapper abstraction ok (is it perlish?)
Download Code
Re^2: is my api wrapper abstraction ok (is it perlish?)
by gideondsouza (Pilgrim) on Jan 20, 2013 at 05:24 UTC

    Thanks for your reply. I see. Yours is an interesting abstraction.

    Also, there are urls like this: "/answers/{ids}/comments" where it returns comments on the answers returned by ids. So then I'd have to introduce another parameter or type for it.

      Also, there are urls like this: "/answers/{ids}/comments" ...
      But isn't that the same as the first example above that returns
      http://api.stackexchange.com/answers/123;134;145/comments?sort=votes;o +rder=desc
      minus the query parameters? The code example I gave already handles that case.
Re^2: is my api wrapper abstraction ok (is it perlish?) (interface)
by tye (Cardinal) on Jan 20, 2013 at 17:30 UTC

    Much saner. :)

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014222]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-07-13 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls