Skip to content

Instantly share code, notes, and snippets.

@tadzik

tadzik/Woof.p6 Secret

Created October 1, 2018 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadzik/37a905d09db571ac02493814a986472f to your computer and use it in GitHub Desktop.
Save tadzik/37a905d09db571ac02493814a986472f to your computer and use it in GitHub Desktop.
# $, for "input attributes" becuase I have no better idea right now :)
use Woof; # rather than "use Moo? ;)"
class Webservice::Access::Thingy {
gets $,api-key is required; # has to be passed in to default construtor
gets $,timeout = 180; # has a default value
# Let's say we'd like to keep an endpoint as a hypothetical HTTP::Path, but we want to be able
# to be settable with a regular Str.
# With the current "meta" of writing classes, you'd probably make it to attributes:
# one of them required and one of them set in BUILD
has $.endpoint -> (Str $arg = 'web.servi.ce/v1/') {
HTTP::Path.new($arg);
}
# The block describes how http-client should be built.
# The assumption is that all the `gets` input attributes are already received, validated and set,
# so can be freely used here: which is not the case at least in Moo(se?)'s "default" blocks
has $!http-client -> {
LWP::UserAgent.new(:$,timeout);
}
has $!access-token -> {
# $,api-key remains available throughout the object's lifetime, but always means
# "that value that the object was configured with, nothing else"
$!http-client.get($.endpoint.child('access_token'), :key($,api-key));
}
# To be continued...?
}
@tadzik
Copy link
Author

tadzik commented Oct 1, 2018

Syntax ideas:

1425        +leont | tadzik: how about fixing that with a trait?
1426        +leont | has $!foo is constructed('bar') or some such? (names are hard)
1429      +pmurias | tadzik: has $!foo is built(-> :$arg-name {BuildItProperly.new($arg-name)})
1430      +pmurias | tadzik: has $!foo is built(-> :$arg-name! {BuildItProperly.new($arg-name)}) for a required one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment