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

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 => '', path => \@paths, query => $arg{options}, ); } __DATA__;134;145/comments?sort=votes;o +rder=desc;order=desc

Replies are listed 'Best First'.
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;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 (Sage) on Jan 20, 2013 at 17:30 UTC

    Much saner. :)

    - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014222]
and the sunlight beams...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-01-20 15:46 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.