slackcat 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8998a4c43b391f21c4377190d0bfc9a5b186155a
4
- data.tar.gz: 9325c7a36decaba80e4b3bbb45aef94ef272ac0e
3
+ metadata.gz: d53ce3d01a0e4d82cc74c4b1190c9676b5f9c7d2
4
+ data.tar.gz: 6cf9c11682d18f38add4c9b24009a224181d96e1
5
5
  SHA512:
6
- metadata.gz: ea3a3d2c15820e024e53da67c6f26ec563ab92e8856deb087604427f9d4aff2eaf344f5850d7b15b7f654f5c38c41c0bac12d79183d3a5801435a00ecbc4f26c
7
- data.tar.gz: 5ebfbbdb284fd3a33e814cbf0266840c39936aab039e131ecf2bce8b2fc03d0ddf2d4136ecf4de8c4e1caf099593a06f85abc08cde6e91483425f54b9ea807d5
6
+ metadata.gz: 9e0ffdefff653d4ce2a32e055580dfe2820c210ff6d93d1f222e38f71fa3fd20d28f37ebab7f63852617f5d0d74c1307ec5f9b45911d4418a8e267afbe7569fd
7
+ data.tar.gz: 484d0339555de1aa0da775863209073b84b2fc4fd1ff204889b192f8379d312ea5ab8cc33c5ee87f9006d471c202eb3b25f1e8d92808e15805d2d9342c2aaf7e
data/README.md CHANGED
@@ -20,15 +20,14 @@ list filenames as arguments. Multiple files will be concatenated.
20
20
  Environment variables:
21
21
 
22
22
  * `SLACK_TOKEN`: your token from https://api.slack.com/.
23
- * `SLACK_CHANNELS`: default comma-separated list of channel names to share files
24
- * `SLACK_GROUPS`: default comma-separated list of private groups to share files
25
23
 
26
24
  Command-line options:
27
25
 
28
26
  ```
29
27
  --token, -k: Slack API token
30
- --channels, -c: Channels to share into
31
- --groups, -g: Private groups to share into
28
+ --channels, -c: Channels to share
29
+ --groups, -g: Private groups to share
30
+ --users, -u: Users (DMs) to share
32
31
  --filetype, -t: File type identifier
33
32
  --title, -T: Title of file
34
33
  --filename, -n: Filename of file
@@ -43,22 +42,25 @@ content will be shared to them.
43
42
 
44
43
  Upload contents of buffer:
45
44
 
46
- `:%! slackcat`
45
+ `:%! slackcat -c dev`
47
46
 
48
47
  ## Example usage from emacs
49
48
 
50
49
  Upload contents of region:
51
50
 
52
- `M-| slackcat`
51
+ `M-| slackcat -c dev`
53
52
 
54
53
  To make a named function:
55
54
 
56
55
  ```lisp
57
56
  (setenv "SLACK_TOKEN" "<your api token>")
58
- (setenv "SLACK_CHANNELS" "<channel list")
57
+
58
+ (defvar slackcat-bin "slackcat" "Command to invoke slackcat.")
59
+ (defvar slackcat-args "-c dev" "Default arguments to pass to slackcat.")
59
60
 
60
61
  (defun slackcat (&optional b e)
61
62
  "Upload contents of region to slack chat."
62
63
  (interactive "r")
63
- (shell-command-on-region b e "slackcat"))
64
+ (let ((args (read-from-minibuffer "slackcat args: " slackcat-args)))
65
+ (shell-command-on-region b e (format "%s %s" slackcat-bin args))))
64
66
  ```
data/bin/slackcat CHANGED
@@ -11,15 +11,36 @@ class Slackcat
11
11
  @token = token
12
12
  end
13
13
 
14
+ ## get a channel, group, im or user list
15
+ def get_objects(method, key)
16
+ self.class.get("/#{method}", query: { token: @token }).tap do |response|
17
+ raise "error retrieving #{thing} list: #{response.fetch('error', 'unknown error')}" unless response['ok']
18
+ end.fetch(key)
19
+ end
20
+
14
21
  def channels
15
- self.class.get('/channels.list', query: { token: @token }).tap do |response|
16
- raise "error retrieving channel list: #{response.fetch('error', 'unknown error')}" unless response['ok']
17
- end
22
+ @channels ||= get_objects('channels.list', 'channels')
18
23
  end
19
24
 
20
25
  def groups
21
- self.class.get('/groups.list', query: { token: @token }).tap do |response|
22
- raise "error retrieving group list: #{response.fetch('error', 'unknown error')}" unless response['ok']
26
+ @groups ||= get_objects('groups.list', 'groups')
27
+ end
28
+
29
+ def ims
30
+ @ims ||= get_objects('im.list', 'ims')
31
+ end
32
+
33
+ def users
34
+ @users ||= get_objects('users.list', 'members')
35
+ end
36
+
37
+ ## translate a username into an IM id
38
+ def im_for_user(username)
39
+ user = users.find do |user|
40
+ user['name'] == username
41
+ end
42
+ ims.find do |im|
43
+ im['user'] == user['id']
23
44
  end
24
45
  end
25
46
 
@@ -33,8 +54,9 @@ end
33
54
 
34
55
  opts = Trollop::options do
35
56
  opt :token, 'Slack API token', type: :string, short: 'k', default: ENV.fetch('SLACK_TOKEN', nil)
36
- opt :channels, 'Channels to share into', type: :string, short: 'c', default: ENV.fetch('SLACK_CHANNELS', nil)
37
- opt :groups, 'Groups to share into', type: :string, short: 'g', default: ENV.fetch('SLACK_GROUPS', nil)
57
+ opt :channels, 'Channels to share', type: :string, short: 'c', default: ''
58
+ opt :groups, 'Groups to share', type: :string, short: 'g', default: ''
59
+ opt :users, 'Users (DMs) to share', type: :string, short: 'u', default: ''
38
60
  opt :filetype, 'File type identifier', type: :string, short: 't'
39
61
  opt :title, 'Title of file', type: :string, short: 'T'
40
62
  opt :filename, 'Filename of file', type: :string, short: 'n'
@@ -47,25 +69,17 @@ opts[:filename] ||= ARGV.first
47
69
  raise 'set slack API token using SLACK_TOKEN or -k option' unless opts[:token]
48
70
  slack = Slackcat.new(opts[:token])
49
71
 
50
- ## get channel IDs
51
- channels = if opts[:channels]
52
- names = opts[:channels].gsub(/\#/, '').split(/[\s,]+/) # array of names with no #
53
- slack.channels['channels'].inject({}) do |hash, channel| # get channel list
54
- hash[channel['name']] = channel['id']; hash # index by name
55
- end.values_at(*names) # list of ids matching given names
72
+ channels = opts[:channels].split(/[\s,]+/).map do |name|
73
+ slack.channels.find { |channel| channel['name'] == name }.fetch('id')
56
74
  end
57
75
 
58
- ## get group IDs
59
- groups = if opts[:groups]
60
- names = opts[:groups].gsub(/\#/, '').split(/[\s,]+/) # array of names with no #, probably not an issue for groups, but whatever.
61
- slack.groups['groups'].inject({}) do |hash, group | # get channel list
62
- hash[group['name']] = group['id']; hash # index by name
63
- end.values_at(*names) # list of ids matching given names
76
+ groups = opts[:groups].split(/[\s,]+/).map do |name|
77
+ slack.groups.find { |group| group['name'] == name }.fetch('id')
64
78
  end
65
79
 
66
- ids = []
67
- ids.concat(channels) if channels
68
- ids.concat(groups) if groups
80
+ ims = opts[:users].split(/[\s,]+/).map do |name|
81
+ slack.im_for_user(name).fetch('id')
82
+ end
69
83
 
70
84
  params = {
71
85
  content: ARGF.read,
@@ -73,7 +87,7 @@ params = {
73
87
  filename: opts[:filename],
74
88
  title: opts[:title],
75
89
  initial_comment: opts[:initial_comment],
76
- channels: ids.join(","),
90
+ channels: (channels + groups + ims).join(","),
77
91
  }.select { |_, value| value }
78
92
 
79
93
  slack.upload(params)
@@ -1,3 +1,3 @@
1
1
  module Slackcat
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slackcat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-19 00:00:00.000000000 Z
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,4 +108,3 @@ signing_key:
108
108
  specification_version: 4
109
109
  summary: Upload a file to slack
110
110
  test_files: []
111
- has_rdoc: