slackr 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/slackr.rb +1 -62
- data/lib/slackr/version.rb +1 -1
- data/lib/slackr/webhook.rb +62 -0
- data/spec/{client_spec.rb → webhook_spec.rb} +14 -24
- metadata +28 -27
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Send a message to slack:
|
|
24
24
|
|
25
25
|
```
|
26
26
|
require 'slackr'
|
27
|
-
slack = Slackr::
|
27
|
+
slack = Slackr::Webhook.new("my_team_name","my_api_key")
|
28
28
|
slack.say "this is a test"
|
29
29
|
```
|
30
30
|
|
@@ -32,7 +32,7 @@ Say a message to slack using some customization:
|
|
32
32
|
|
33
33
|
```
|
34
34
|
require 'slackr'
|
35
|
-
slack = Slackr::
|
35
|
+
slack = Slackr::Webhook.new("my_team_name","my_api_key",{:icon_emoji => ":ghost:"})
|
36
36
|
slack.say("this is a test as a ghost")
|
37
37
|
slack.say("this is a test as a ghost with a custom name",{:username => "casper"}
|
38
38
|
```
|
data/lib/slackr.rb
CHANGED
@@ -1,64 +1,3 @@
|
|
1
1
|
require "slackr/version"
|
2
2
|
require "slackr/errors"
|
3
|
-
|
4
|
-
require "net/http"
|
5
|
-
require "net/https" # Obsolete as of what ruby version?
|
6
|
-
require "uri"
|
7
|
-
require "json"
|
8
|
-
|
9
|
-
# slack_options
|
10
|
-
# {
|
11
|
-
# "channel" => "#myroom",
|
12
|
-
# "username" => "my_bot_name",
|
13
|
-
# "icon_url" => "https://slack.com/img/icons/app-57.png",
|
14
|
-
# "icon_emoji" => ":ghost:"
|
15
|
-
# }
|
16
|
-
|
17
|
-
module Slackr
|
18
|
-
|
19
|
-
class Client
|
20
|
-
attr_reader :http, :uri, :default_options
|
21
|
-
|
22
|
-
def initialize(team,token,options={})
|
23
|
-
raise Slackr::ArgumentError, "Team required" if team.nil?
|
24
|
-
raise Slackr::ArgumentError, "Token required" if token.nil?
|
25
|
-
|
26
|
-
@team = team
|
27
|
-
@token = token
|
28
|
-
@default_options = options
|
29
|
-
|
30
|
-
setup_connection
|
31
|
-
end
|
32
|
-
|
33
|
-
def say(text="",options={})
|
34
|
-
# reformat links, etc here
|
35
|
-
send_request(text,options)
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
def setup_connection
|
40
|
-
@uri = URI.parse(service_url)
|
41
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
42
|
-
@http.use_ssl = true
|
43
|
-
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
44
|
-
end
|
45
|
-
|
46
|
-
def service_url
|
47
|
-
"https://#{@team}.slack.com/services/hooks/incoming-webhook?token=#{@token}"
|
48
|
-
end
|
49
|
-
|
50
|
-
def encode_message(text,options)
|
51
|
-
"payload=#{default_options.merge(options).merge({"text" => text}).to_json.to_s}"
|
52
|
-
end
|
53
|
-
|
54
|
-
def send_request(text,options)
|
55
|
-
request = Net::HTTP::Post.new(uri.request_uri)
|
56
|
-
request.body = encode_message(text,options)
|
57
|
-
response = http.request(request)
|
58
|
-
unless response.code == "200"
|
59
|
-
raise Slackr::ServiceError, "Slack.com - #{response.code} - #{response.body}"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
3
|
+
require "slackr/webhook"
|
data/lib/slackr/version.rb
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "net/https" # Obsolete as of what ruby version?
|
3
|
+
require "uri"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
# slack_options
|
7
|
+
# {
|
8
|
+
# "channel" => "#myroom",
|
9
|
+
# "username" => "my_bot_name",
|
10
|
+
# "icon_url" => "https://slack.com/img/icons/app-57.png",
|
11
|
+
# "icon_emoji" => ":ghost:"
|
12
|
+
# }
|
13
|
+
|
14
|
+
module Slackr
|
15
|
+
|
16
|
+
class Webhook
|
17
|
+
attr_reader :http, :uri, :default_options
|
18
|
+
|
19
|
+
def initialize(team,token,options={})
|
20
|
+
raise Slackr::ArgumentError, "Team required" if team.nil?
|
21
|
+
raise Slackr::ArgumentError, "Token required" if token.nil?
|
22
|
+
|
23
|
+
@team = team
|
24
|
+
@token = token
|
25
|
+
@default_options = options
|
26
|
+
|
27
|
+
setup_connection
|
28
|
+
end
|
29
|
+
|
30
|
+
def say(text="",options={})
|
31
|
+
# reformat links, etc here
|
32
|
+
send_request(text,options)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def setup_connection
|
38
|
+
@uri = URI.parse(service_url)
|
39
|
+
@http = Net::HTTP.new(@uri.host, @uri.port)
|
40
|
+
@http.use_ssl = true
|
41
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
42
|
+
end
|
43
|
+
|
44
|
+
def service_url
|
45
|
+
"https://#{@team}.slack.com/services/hooks/incoming-webhook?token=#{@token}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def encode_message(text,options)
|
49
|
+
"payload=#{default_options.merge(options).merge({"text" => text}).to_json.to_s}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def send_request(text,options)
|
53
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
54
|
+
request.body = encode_message(text,options)
|
55
|
+
response = http.request(request)
|
56
|
+
unless response.code == "200"
|
57
|
+
raise Slackr::ServiceError, "Slack.com - #{response.code} - #{response.body}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
@@ -1,34 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Slackr::
|
4
|
-
let(:client) { Slackr::
|
3
|
+
describe Slackr::Webhook do
|
4
|
+
let(:client) { Slackr::Webhook.new("team","token") }
|
5
5
|
subject { client }
|
6
6
|
|
7
|
-
describe "#initialize" do
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
7
|
describe "#say" do
|
12
|
-
it "should call
|
13
|
-
msg
|
8
|
+
it "should call Webhook#send_request with proper options" do
|
9
|
+
msg = "This is a test message"
|
14
10
|
opts = {"channel" => "#somewhere"}
|
15
|
-
|
11
|
+
|
16
12
|
subject.should_receive(:send_request).with(msg,opts)
|
17
|
-
|
18
13
|
subject.say msg,opts
|
19
14
|
end
|
20
15
|
end
|
21
16
|
|
22
|
-
describe "#setup_connection" do
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
17
|
describe "#service_url" do
|
27
18
|
it "should generate the right url" do
|
28
|
-
team
|
29
|
-
token
|
30
|
-
subject = Slackr::
|
31
|
-
|
19
|
+
team = "my-team"
|
20
|
+
token = "my-token"
|
21
|
+
subject = Slackr::Webhook.new(team,token)
|
22
|
+
|
32
23
|
subject.send(:service_url).should eq "https://#{team}.slack.com/services/hooks/incoming-webhook?token=#{token}"
|
33
24
|
end
|
34
25
|
end
|
@@ -39,20 +30,23 @@ describe Slackr::Client do
|
|
39
30
|
|
40
31
|
subject.send(:encode_message,msg,{}).should eq "payload={\"text\":\"#{msg}\"}"
|
41
32
|
end
|
33
|
+
|
42
34
|
it "should encode a message with options" do
|
43
|
-
msg
|
35
|
+
msg = "this is my awesome message"
|
44
36
|
opts = {"channel" => "#awesometown"}
|
45
37
|
|
46
38
|
subject.send(:encode_message,msg,opts).should eq "payload={\"channel\":\"#awesometown\",\"text\":\"#{msg}\"}"
|
47
39
|
end
|
40
|
+
|
48
41
|
it "should encode a basic message when there are default options" do
|
49
42
|
msg = "this is my awesome message"
|
50
43
|
|
51
44
|
subject.stub(:default_options).and_return({"icon_emoji" => "ghost"})
|
52
45
|
subject.send(:encode_message,msg,{}).should eq "payload={\"icon_emoji\":\"ghost\",\"text\":\"#{msg}\"}"
|
53
46
|
end
|
47
|
+
|
54
48
|
it "should encode a message with option when there are default options present" do
|
55
|
-
msg
|
49
|
+
msg = "this is my awesome message"
|
56
50
|
opts = {"channel" => "#awesometown"}
|
57
51
|
|
58
52
|
subject.stub(:default_options).and_return({"icon_emoji" => "ghost"})
|
@@ -60,8 +54,4 @@ describe Slackr::Client do
|
|
60
54
|
end
|
61
55
|
end
|
62
56
|
|
63
|
-
describe "#send_request" do
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
57
|
end
|
metadata
CHANGED
@@ -1,80 +1,80 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slackr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
5
4
|
prerelease:
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jason Rohwedder
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
+
prerelease: false
|
15
16
|
name: bundler
|
17
|
+
type: :development
|
16
18
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
19
|
requirements:
|
19
20
|
- - ~>
|
20
21
|
- !ruby/object:Gem::Version
|
21
22
|
version: '1.3'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
23
|
none: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
25
|
requirements:
|
27
26
|
- - ~>
|
28
27
|
- !ruby/object:Gem::Version
|
29
28
|
version: '1.3'
|
29
|
+
none: false
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
+
prerelease: false
|
31
32
|
name: rake
|
33
|
+
type: :development
|
32
34
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
35
|
requirements:
|
35
36
|
- - ! '>='
|
36
37
|
- !ruby/object:Gem::Version
|
37
38
|
version: '0'
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
39
|
none: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
41
|
requirements:
|
43
42
|
- - ! '>='
|
44
43
|
- !ruby/object:Gem::Version
|
45
44
|
version: '0'
|
45
|
+
none: false
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
|
+
prerelease: false
|
47
48
|
name: rspec
|
49
|
+
type: :development
|
48
50
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
51
|
requirements:
|
51
52
|
- - ! '>='
|
52
53
|
- !ruby/object:Gem::Version
|
53
54
|
version: '0'
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
55
|
none: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
57
|
requirements:
|
59
58
|
- - ! '>='
|
60
59
|
- !ruby/object:Gem::Version
|
61
60
|
version: '0'
|
61
|
+
none: false
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
+
prerelease: false
|
63
64
|
name: webmock
|
65
|
+
type: :development
|
64
66
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
67
|
requirements:
|
67
68
|
- - ! '>='
|
68
69
|
- !ruby/object:Gem::Version
|
69
70
|
version: '0'
|
70
|
-
type: :development
|
71
|
-
prerelease: false
|
72
|
-
version_requirements: !ruby/object:Gem::Requirement
|
73
71
|
none: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
73
|
requirements:
|
75
74
|
- - ! '>='
|
76
75
|
- !ruby/object:Gem::Version
|
77
76
|
version: '0'
|
77
|
+
none: false
|
78
78
|
description: Talk to slack.com chat platform from ruby
|
79
79
|
email:
|
80
80
|
- jro@risk.io
|
@@ -90,9 +90,10 @@ files:
|
|
90
90
|
- lib/slackr.rb
|
91
91
|
- lib/slackr/errors.rb
|
92
92
|
- lib/slackr/version.rb
|
93
|
+
- lib/slackr/webhook.rb
|
93
94
|
- slackr.gemspec
|
94
|
-
- spec/client_spec.rb
|
95
95
|
- spec/spec_helper.rb
|
96
|
+
- spec/webhook_spec.rb
|
96
97
|
homepage: ''
|
97
98
|
licenses:
|
98
99
|
- MIT
|
@@ -101,29 +102,29 @@ rdoc_options: []
|
|
101
102
|
require_paths:
|
102
103
|
- lib
|
103
104
|
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
-
none: false
|
105
105
|
requirements:
|
106
106
|
- - ! '>='
|
107
107
|
- !ruby/object:Gem::Version
|
108
|
-
version: '0'
|
109
108
|
segments:
|
110
109
|
- 0
|
111
|
-
hash:
|
112
|
-
|
110
|
+
hash: 4548143177958109987
|
111
|
+
version: '0'
|
113
112
|
none: false
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - ! '>='
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
117
|
segments:
|
119
118
|
- 0
|
120
|
-
hash:
|
119
|
+
hash: 4548143177958109987
|
120
|
+
version: '0'
|
121
|
+
none: false
|
121
122
|
requirements: []
|
122
123
|
rubyforge_project:
|
123
|
-
rubygems_version: 1.8.
|
124
|
+
rubygems_version: 1.8.28
|
124
125
|
signing_key:
|
125
126
|
specification_version: 3
|
126
127
|
summary: Send data into Slack in real-time, via the Incoming Webhooks API
|
127
128
|
test_files:
|
128
|
-
- spec/client_spec.rb
|
129
129
|
- spec/spec_helper.rb
|
130
|
+
- spec/webhook_spec.rb
|