Skip to content

Commit

Permalink
Merge pull request #1 from nicqrocks/beta
Browse files Browse the repository at this point in the history
Working version
  • Loading branch information
nicqrocks authored Jun 28, 2016
2 parents c74d561 + ca07e3c commit b921383
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -25,20 +20,11 @@ This will be the method to send a message from. It will be overloaded so that it
#Using a hash.
my %info = (
username => "perl6-bot",
icon => ":robot_face:",
icon_emoji => ":robot_face:",
text => "Beep, boop. *excited robot sounds*"
);
$slack.send(%info);
#Or possibly...
#Without a hash.
$slack.send(
username => "perl6-bot",
icon => ":robot_face:",
text => "Beep, boop. *excited robot sounds*"
);
#Or if you just want to send something...
#Just a string.
Expand Down
28 changes: 24 additions & 4 deletions lib/WebService/Slack/webhook.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@ 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.
multi method new(Str:D $var) { self.bless(:url($var)); }
multi method new($other?) { die "No Slack integration given!"; }
#Using a hash for the info.
multi method send(%info) {
say "Entering hash method.";
#Make sure the data is valid.
unless all %info.values.map({$_ ~~ Str|Any}) {
die "Bad data in key-value pairs.\nGot: {%info.pairs}";
}

#Setup the data to be sent.
my %header = :Content-type("application/json");
my $body = Buf.new(to-json(%info).ords);

#Send the request.
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) {
say "Entering string method.";
my %info = (:text($msg));
self.send(%info);
}
}
11 changes: 1 addition & 10 deletions t/02-make.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use lib 'lib';
use WebService::Slack::webhook;

#Make some vars
my $fake-url = "https://host.slack.com/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()},
Expand All @@ -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"),
'WebService::Slack::webhook',
'Good object can be made with path';
"$file-name".IO.unlink; #Remove the file.


done-testing;
20 changes: 20 additions & 0 deletions t/03-send.t
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit b921383

Please sign in to comment.