aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2016-09-12 07:23:56 -0400
committerTeddy Wing2016-09-12 07:27:37 -0400
commitb40212e4214b41e2265cf42680a7a995c04f6efc (patch)
treeab6360c0d43468b356352f1326a1f911845b5487
parenta98444dc1a469da2b50cdddc52262daab94c51bb (diff)
downloadirssi-slack-profile-b40212e4214b41e2265cf42680a7a995c04f6efc.tar.bz2
Make the HTTP request subroutine more generic
Allow any Slack API method to be requested instead of just the `users.list` method. This will enable us to use other Slack API methods leveraging the same HTTP code. The `fetch_users_list` subroutine now calls out to `slack_api` in order to retrieve its data.
-rw-r--r--slack_profile.pl19
1 files changed, 13 insertions, 6 deletions
diff --git a/slack_profile.pl b/slack_profile.pl
index a7e5ac6..4812dbf 100644
--- a/slack_profile.pl
+++ b/slack_profile.pl
@@ -83,15 +83,15 @@ sub users_list_cache {
Irssi::get_irssi_dir() . '/scripts/slack_profile-users.list.plstore';
}
-sub fetch_users_list {
+sub slack_api {
my $token = Irssi::settings_get_str('slack_profile_token');
die 'Requires a Slack API token. Generate one from ' .
'https://api.slack.com/docs/oauth-test-tokens. ' .
'Set it with `/set slack_profile_token TOKEN`.' if !$token;
- Irssi::print('Fetching users list from Slack. This could take a while...');
+ my ($method, $args) = @_;
- my $url = URI->new('https://slack.com/api/users.list');
+ my $url = URI->new("https://slack.com/api/$method");
$url->query_form(token => $token);
my $http = HTTP::Tiny->new(
@@ -106,12 +106,11 @@ sub fetch_users_list {
my $payload = decode_json($resp->{'content'});
if ($payload->{'ok'}) {
- @users_list = @{$payload->{'members'}};
- store \@users_list, users_list_cache;
+ return $payload;
}
else {
Irssi::print("Error from the Slack API: $payload->{'error'}");
- die 'Unable to retrieve users from the Slack API';
+ die 'Unable to retrieve data from the Slack API';
}
}
else {
@@ -120,6 +119,14 @@ sub fetch_users_list {
}
}
+sub fetch_users_list {
+ Irssi::print('Fetching users list from Slack. This could take a while...');
+
+ my $resp = slack_api('users.list');
+ @users_list = @{$resp->{'members'}};
+ store \@users_list, users_list_cache;
+}
+
sub find_user {
my ($username) = @_;