From e3c619494601a8628b8a27bd855fa9b1506c0728 Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Fri, 29 Jul 2016 19:21:20 -0400 Subject: Add examples; fix up docs --- examples/03-github-notifications.p6 | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/03-github-notifications.p6 (limited to 'examples/03-github-notifications.p6') diff --git a/examples/03-github-notifications.p6 b/examples/03-github-notifications.p6 new file mode 100644 index 0000000..8dec6d2 --- /dev/null +++ b/examples/03-github-notifications.p6 @@ -0,0 +1,38 @@ +use lib ; + +use IRC::Client; +use HTTP::Tinyish; +use JSON::Fast; + +class GitHub::Notifications does IRC::Client::Plugin { + has Str:D $.token = %*ENV; + has $!ua = HTTP::Tinyish.new; + constant $API_URL = 'https://api.github.com/notifications'; + + method irc-connected ($) { + start react { + whenever self!notification.grep(* > 0) -> $num { + $.irc.send: :where + :text("You have $num unread notifications!") + :notice; + } + } + } + + method !notification { + supply { + loop { + my $res = $!ua.get: $API_URL, :headers{ :Authorization("token $!token") }; + $res and emit +grep *., |from-json $res; + sleep $res || 60; + } + } + } +} + +.run with IRC::Client.new: + :nick + :host + :channels<#zofbot> + :debug + :plugins(GitHub::Notifications.new) -- cgit v1.1