diff --git a/README.md b/README.md index b2cff2c..7cd440f 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,6 @@ The `.new` method will require an 'incoming webhook' integration link from Slack ``` #String containing the URL. my $slack = WebService::Slack::webhook.new(url => "$url"); - -#Or... - -#Path to a file with the URL. -my $slack = WebService::Slack::webhook.new(path => "/path/to/file"); ``` ###`.send` @@ -30,15 +25,6 @@ my %info = ( ); $slack.send(%info); -#Or possibly... - -#Without a hash. -$slack.send( - username => "perl6-bot", - icon_emoji => ":robot_face:", - text => "Beep, boop. *excited robot sounds*" -); - #Or if you just want to send something... #Just a string. diff --git a/lib/WebService/Slack/webhook.pm6 b/lib/WebService/Slack/webhook.pm6 index 8adc73e..331081b 100755 --- a/lib/WebService/Slack/webhook.pm6 +++ b/lib/WebService/Slack/webhook.pm6 @@ -6,32 +6,13 @@ use JSON::Fast; #Make the class. class WebService::Slack::webhook { #Make some vars. - has Str $!url; + has Str $.url is required where {$_ ~~ /https\:\/\/hooks\.slack\.com\/services\//}; - #Make some methods to build the object. - #Take in a string as the URL to use. - multi method new(Str:D :$url!) { - if ($url ~~ /https\:\/\/hooks\.slack\.com\/services\//) { - self.bless(:url($url)); - } - else { die "Bad Slack integration URL '$url'" } - } - #Take in the path of a file that has the URL. - multi method new(IO::Path:D :$path!) { - if ($path ~~ :f & :r) { - self.bless(:url($path.lines[0])); - } - else { die "Cannot read from the file '$path'"} - } - #Fail. - multi method new($other?) { die "No Slack integration URL given!"; } - - - #Now for the sending methods. #Using a hash for the info. - multi method send(*%info) { + multi method send(%info) { + say "Entering hash method."; #Make sure the data is valid. - unless all %info.values.map({$_ ~~ Str | Any}) { + unless all %info.values.map({$_ ~~ Str|Any}) { die "Bad data in key-value pairs.\nGot: {%info.pairs}"; } @@ -40,12 +21,14 @@ class WebService::Slack::webhook { my $body = Buf.new(to-json(%info).ords); #Send the request. - my $resp = Net::HTTP::POST($!url, :%header, :$body); + say $.url.perl ~ "\n" ~ %header.perl ~ "\n" ~ %info.perl; + my $resp = Net::HTTP::POST($.url, :%header, :$body); } #using a string for the info. multi method send(Str $msg) { - my %info = (text => $msg); - callwith(%info); + say "Entering string method."; + my %info = (:text($msg)); + self.send(%info); } } diff --git a/t/02-make.t b/t/02-make.t index 89e9bf0..668dbe5 100755 --- a/t/02-make.t +++ b/t/02-make.t @@ -6,8 +6,7 @@ use lib 'lib'; use WebService::Slack::webhook; #Make some vars -my $fake-url = "https://hooks.slack.com/services/example"; -my $file-name = ".slackurl"; +my $fake-url = "https://hooks.slack.com/services/example.html"; #Make a bad object. dies-ok {WebService::Slack::webhook.new()}, @@ -18,12 +17,4 @@ isa-ok WebService::Slack::webhook.new(url => "$fake-url"), 'WebService::Slack::webhook', 'Good object can be made with url'; -#Make a good object with a path. -"$file-name".IO.spurt($fake-url); #Make the file. -isa-ok WebService::Slack::webhook.new(path => "$file-name".IO), - 'WebService::Slack::webhook', - 'Good object can be made with path'; -"$file-name".IO.unlink; #Remove the file. - - done-testing; diff --git a/t/03-send.t b/t/03-send.t new file mode 100755 index 0000000..371412d --- /dev/null +++ b/t/03-send.t @@ -0,0 +1,20 @@ +#!/usr/bin/env perl6 + +use Test; +use lib 'lib'; + +use WebService::Slack::webhook; + +#Make some vars +my $fake-url = "https://hooks.slack.com/services/example/integration/url"; + +my $slack = WebService::Slack::webhook.new(url => $fake-url); +my %info = ( + text => "This is a test with a hash", + username => "test-bot" +); + +ok $slack.send("This is a test with a string"), 'Send works with string arg'; +ok $slack.send(%info), 'Send works with hash arg'; + +done-testing;