Skip to content

Commit

Permalink
Clean and Simple
Browse files Browse the repository at this point in the history
This version cuts out certain "features" that would make things
much more complex and just caused restrictions to the person
using the module. This version though is the first that works
properly without errors.
  • Loading branch information
nicqrocks committed Jun 28, 2016
1 parent 24c317b commit ca07e3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 50 deletions.
14 changes: 0 additions & 14 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 @@ -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.
Expand Down
35 changes: 9 additions & 26 deletions lib/WebService/Slack/webhook.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}

Expand All @@ -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);
}
}
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://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()},
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".IO),
'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 ca07e3c

Please sign in to comment.