slackr 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,6 +38,10 @@ Retrieve the channel list:
38
38
  slack.channels.list # returns a hash of channel objects
39
39
  ```
40
40
 
41
+ Upload a file (Note: You must specify channels by their ID, not name. You can send to multiple by using a comma separated list):
42
+ ```
43
+ slack.upload '/path/to/a/file.txt', {'channels' => 'C026VKGP7'}
44
+
41
45
  Available customizations include:
42
46
 
43
47
  ```
@@ -3,10 +3,12 @@ require "slackr/errors"
3
3
  require "slackr/connection"
4
4
  require "slackr/webhooks/incoming"
5
5
  require "slackr/channel"
6
+ require "slackr/file_uploader"
6
7
 
7
8
  # slack = Slackr.connect("teamX", "token124", {"channel" => "#myroom", "username" => "systems_bot"})
8
9
  # slack.say "hello world" => posts 'hello world' to the myroom channel as the systems_bot user
9
10
  # slack.say "hello", {"channel" => "#room2", "username" => "joke_bot"} => posts 'hello' to the room2 channel as the joke_but user
11
+ # slack.upload "/path/to/a/file.txt", {"channels" => "X026VKGP7"} => uploads a file to the channel specified by ID
10
12
  # slack.channels.list => returns a hash of channel objects
11
13
  module Slackr
12
14
  extend self
@@ -25,6 +27,10 @@ module Slackr
25
27
  Slackr::IncomingWebhook.say(connection, text, options)
26
28
  end
27
29
 
30
+ def upload(filepath, options = {})
31
+ Slackr::FileUploader.new(connection, filepath, options).upload
32
+ end
33
+
28
34
  def channels
29
35
  Slackr::Channel.init(connection)
30
36
  end
@@ -0,0 +1,56 @@
1
+ require "net/http"
2
+ require "net/https"
3
+ require 'net/http/post/multipart'
4
+ require "uri"
5
+
6
+ module Slackr
7
+ class FileUploader
8
+
9
+ attr_accessor :connection
10
+ attr_reader :channels, :filetype, :filename, :title, :comment, :filepath
11
+
12
+ def initialize(connection, filepath, options={})
13
+ @connection = connection
14
+ @filepath = filepath
15
+ @channels = options['channels']
16
+ @filetype = options['filetype']
17
+ @filename = options['filename']
18
+ @title = options['title']
19
+ @comment = options['initial_comment']
20
+ end
21
+
22
+ def upload
23
+ response = connection.http_request(multipart)
24
+ if response.code != "200"
25
+ raise Slackr::ServiceError, "Slack.com - #{response.code} - #{response.body}"
26
+ end
27
+
28
+ true
29
+ end
30
+
31
+ private
32
+
33
+ def multipart
34
+ Net::HTTP::Post::Multipart.new(service_url.path,
35
+ {
36
+ 'file' => UploadIO.new(File.open(filepath), "application/data"),
37
+ 'token' => connection.token
38
+ }.merge(additional_params))
39
+ end
40
+
41
+ def additional_params
42
+ {}.tap do |hash|
43
+ hash['channels'] = channels if channels
44
+ hash['filetype'] = filetype if filetype
45
+ hash['filename'] = filename if filename
46
+ hash['title'] = title if title
47
+ hash['initial_comment'] = comment if comment
48
+ end
49
+ end
50
+
51
+ def service_url
52
+ URI.parse("#{connection.base_url}/api/files.upload")
53
+ end
54
+ end
55
+ end
56
+
@@ -1,3 +1,3 @@
1
1
  module Slackr
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "multipart-post", "~> 2.0.0"
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rake", "~> 10.1.1", ">= 10.1.1"
23
24
  spec.add_development_dependency "rspec", "~> 2.14.1", ">= 2.14.1"
@@ -0,0 +1 @@
1
+ Hello world!
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slackr::FileUploader do
4
+ let(:connection) { Slackr::Connection.new("team", "token", {}) }
5
+
6
+ before do
7
+ connection.send(:setup_connection)
8
+
9
+ stub_request(:post, "https://team.slack.com/api/files.upload").
10
+ with(:body => "-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"file\"; filename=\"sample_file.txt\"\r\nContent-Length: 13\r\nContent-Type: application/data\r\nContent-Transfer-Encoding: binary\r\n\r\nHello world!\n\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\ntoken\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"channels\"\r\n\r\n123456789\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"filetype\"\r\n\r\ntxt\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nsome custom title\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"initial_comment\"\r\n\r\nsome kind of comment\r\n-------------RubyMultipartPost--\r\n\r\n",
11
+ :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'725', 'Content-Type'=>'multipart/form-data; boundary=-----------RubyMultipartPost', 'User-Agent'=>'Ruby'}).
12
+ to_return(:status => 200, :body => "", :headers => {})
13
+
14
+ # 1.9.3 fix
15
+ stub_request(:post, "https://team.slack.com/api/files.upload").
16
+ with(:body => "-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"file\"; filename=\"sample_file.txt\"\r\nContent-Length: 13\r\nContent-Type: application/data\r\nContent-Transfer-Encoding: binary\r\n\r\nHello world!\n\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\ntoken\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"channels\"\r\n\r\n123456789\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"filetype\"\r\n\r\ntxt\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nsome custom title\r\n-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"initial_comment\"\r\n\r\nsome kind of comment\r\n-------------RubyMultipartPost--\r\n\r\n",
17
+ :headers => {'Accept'=>'*/*', 'Content-Length'=>'725', 'Content-Type'=>'multipart/form-data; boundary=-----------RubyMultipartPost', 'User-Agent'=>'Ruby'}).
18
+ to_return(:status => 200, :body => "", :headers => {})
19
+ end
20
+
21
+ describe "#upload" do
22
+ let(:filepath) { "spec/fixtures/sample_file.txt" }
23
+ let(:options) { {'channels' => '123456789', 'filetype' => 'txt', 'title' => 'some custom title', 'initial_comment' => 'some kind of comment'} }
24
+
25
+ subject { described_class.new(connection, filepath, options) }
26
+
27
+ it "should upload the file to the specified channel" do
28
+ expect(subject.upload).to eq true
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: slackr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jason Rohwedder
@@ -10,30 +10,45 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-09-13 00:00:00.000000000 Z
13
+ date: 2014-09-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- type: :development
17
16
  prerelease: false
18
- name: bundler
17
+ name: multipart-post
18
+ type: :runtime
19
19
  requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.0.0
24
+ none: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
20
30
  none: false
31
+ - !ruby/object:Gem::Dependency
32
+ prerelease: false
33
+ name: bundler
34
+ type: :development
35
+ requirement: !ruby/object:Gem::Requirement
21
36
  requirements:
22
37
  - - ~>
23
38
  - !ruby/object:Gem::Version
24
39
  version: '1.3'
25
- version_requirements: !ruby/object:Gem::Requirement
26
40
  none: false
41
+ version_requirements: !ruby/object:Gem::Requirement
27
42
  requirements:
28
43
  - - ~>
29
44
  - !ruby/object:Gem::Version
30
45
  version: '1.3'
46
+ none: false
31
47
  - !ruby/object:Gem::Dependency
32
- type: :development
33
48
  prerelease: false
34
49
  name: rake
50
+ type: :development
35
51
  requirement: !ruby/object:Gem::Requirement
36
- none: false
37
52
  requirements:
38
53
  - - ~>
39
54
  - !ruby/object:Gem::Version
@@ -41,8 +56,8 @@ dependencies:
41
56
  - - ! '>='
42
57
  - !ruby/object:Gem::Version
43
58
  version: 10.1.1
44
- version_requirements: !ruby/object:Gem::Requirement
45
59
  none: false
60
+ version_requirements: !ruby/object:Gem::Requirement
46
61
  requirements:
47
62
  - - ~>
48
63
  - !ruby/object:Gem::Version
@@ -50,12 +65,12 @@ dependencies:
50
65
  - - ! '>='
51
66
  - !ruby/object:Gem::Version
52
67
  version: 10.1.1
68
+ none: false
53
69
  - !ruby/object:Gem::Dependency
54
- type: :development
55
70
  prerelease: false
56
71
  name: rspec
72
+ type: :development
57
73
  requirement: !ruby/object:Gem::Requirement
58
- none: false
59
74
  requirements:
60
75
  - - ~>
61
76
  - !ruby/object:Gem::Version
@@ -63,8 +78,8 @@ dependencies:
63
78
  - - ! '>='
64
79
  - !ruby/object:Gem::Version
65
80
  version: 2.14.1
66
- version_requirements: !ruby/object:Gem::Requirement
67
81
  none: false
82
+ version_requirements: !ruby/object:Gem::Requirement
68
83
  requirements:
69
84
  - - ~>
70
85
  - !ruby/object:Gem::Version
@@ -72,12 +87,12 @@ dependencies:
72
87
  - - ! '>='
73
88
  - !ruby/object:Gem::Version
74
89
  version: 2.14.1
90
+ none: false
75
91
  - !ruby/object:Gem::Dependency
76
- type: :development
77
92
  prerelease: false
78
93
  name: webmock
94
+ type: :development
79
95
  requirement: !ruby/object:Gem::Requirement
80
- none: false
81
96
  requirements:
82
97
  - - ~>
83
98
  - !ruby/object:Gem::Version
@@ -85,8 +100,8 @@ dependencies:
85
100
  - - ! '>='
86
101
  - !ruby/object:Gem::Version
87
102
  version: 1.17.4
88
- version_requirements: !ruby/object:Gem::Requirement
89
103
  none: false
104
+ version_requirements: !ruby/object:Gem::Requirement
90
105
  requirements:
91
106
  - - ~>
92
107
  - !ruby/object:Gem::Version
@@ -94,6 +109,7 @@ dependencies:
94
109
  - - ! '>='
95
110
  - !ruby/object:Gem::Version
96
111
  version: 1.17.4
112
+ none: false
97
113
  description: Talk to slack.com chat platform from ruby
98
114
  email:
99
115
  - jro@risk.io
@@ -112,11 +128,14 @@ files:
112
128
  - lib/slackr/channel.rb
113
129
  - lib/slackr/connection.rb
114
130
  - lib/slackr/errors.rb
131
+ - lib/slackr/file_uploader.rb
115
132
  - lib/slackr/version.rb
116
133
  - lib/slackr/webhooks/incoming.rb
117
134
  - slackr.gemspec
135
+ - spec/fixtures/sample_file.txt
118
136
  - spec/slackr/channel_spec.rb
119
137
  - spec/slackr/connection_spec.rb
138
+ - spec/slackr/file_uploader_spec.rb
120
139
  - spec/slackr/webhooks/incoming_spec.rb
121
140
  - spec/slackr_spec.rb
122
141
  - spec/spec_helper.rb
@@ -128,17 +147,23 @@ rdoc_options: []
128
147
  require_paths:
129
148
  - lib
130
149
  required_ruby_version: !ruby/object:Gem::Requirement
131
- none: false
132
150
  requirements:
133
151
  - - ! '>='
134
152
  - !ruby/object:Gem::Version
153
+ segments:
154
+ - 0
155
+ hash: -3557734913826783238
135
156
  version: '0'
136
- required_rubygems_version: !ruby/object:Gem::Requirement
137
157
  none: false
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
159
  requirements:
139
160
  - - ! '>='
140
161
  - !ruby/object:Gem::Version
162
+ segments:
163
+ - 0
164
+ hash: -3557734913826783238
141
165
  version: '0'
166
+ none: false
142
167
  requirements: []
143
168
  rubyforge_project:
144
169
  rubygems_version: 1.8.23.2
@@ -146,8 +171,10 @@ signing_key:
146
171
  specification_version: 3
147
172
  summary: Send data into Slack in real-time, via the Incoming Webhooks API
148
173
  test_files:
174
+ - spec/fixtures/sample_file.txt
149
175
  - spec/slackr/channel_spec.rb
150
176
  - spec/slackr/connection_spec.rb
177
+ - spec/slackr/file_uploader_spec.rb
151
178
  - spec/slackr/webhooks/incoming_spec.rb
152
179
  - spec/slackr_spec.rb
153
180
  - spec/spec_helper.rb