slackr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slackr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Risk I/O
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Slackr
2
+
3
+ A simple gem for sending messages to the Slack.com communications platform.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'slackr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install slackr
18
+
19
+ ## Usage
20
+
21
+
22
+
23
+ Send a message to slack:
24
+
25
+ ```
26
+ require 'slackr'
27
+ slack = Slackr::Client.new("my_team_name","my_api_key")
28
+ slack.say "this is a test"
29
+ ```
30
+
31
+ Say a message to slack using some customization:
32
+
33
+ ```
34
+ require 'slackr'
35
+ slack = Slackr::Client.new("my_team_name","my_api_key",{:icon_emoji => ":ghost:"})
36
+ slack.say("this is a test as a ghost")
37
+ slack.say("this is a test as a ghost with a custom name",{:username => "casper"}
38
+ ```
39
+
40
+ Available customizations include:
41
+
42
+ ```
43
+ # slack_options
44
+ # {
45
+ # "channel" => "#myroom",
46
+ # "username" => "my_bot_name",
47
+ # "icon_url" => "https://slack.com/img/icons/app-57.png",
48
+ # "icon_emoji" => ":ghost:"
49
+ # }
50
+ ```
51
+
52
+ ## TODO
53
+
54
+ 1. More/better tests
55
+ 2. Link parsing and attachments
56
+ 3. CLI
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/slackr.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "slackr/version"
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
@@ -0,0 +1,4 @@
1
+ module Slackr
2
+ class ServiceError < StandardError ; end
3
+ class ArgumentError < StandardError ; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Slackr
2
+ VERSION = "0.0.1"
3
+ end
data/slackr.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slackr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slackr"
8
+ spec.version = Slackr::VERSION
9
+ spec.authors = ["Jason Rohwedder"]
10
+ spec.email = ["jro@risk.io"]
11
+ spec.description = %q{Talk to slack.com chat platform from ruby}
12
+ spec.summary = %q{Send data into Slack in real-time, via the Incoming Webhooks API}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "webmock"
25
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slackr::Client do
4
+ let(:client) { Slackr::Client.new("team","token") }
5
+ subject { client }
6
+
7
+ describe "#initialize" do
8
+
9
+ end
10
+
11
+ describe "#say" do
12
+ it "should call Client#send_request with proper options" do
13
+ msg = "This is a test message"
14
+ opts = {"channel" => "#somewhere"}
15
+
16
+ subject.should_receive(:send_request).with(msg,opts)
17
+
18
+ subject.say msg,opts
19
+ end
20
+ end
21
+
22
+ describe "#setup_connection" do
23
+
24
+ end
25
+
26
+ describe "#service_url" do
27
+ it "should generate the right url" do
28
+ team = "my-team"
29
+ token = "my-token"
30
+ subject = Slackr::Client.new(team,token)
31
+
32
+ subject.send(:service_url).should eq "https://#{team}.slack.com/services/hooks/incoming-webhook?token=#{token}"
33
+ end
34
+ end
35
+
36
+ describe "#encode_message" do
37
+ it "should encode a basic message" do
38
+ msg = "this is my awesome message"
39
+
40
+ subject.send(:encode_message,msg,{}).should eq "payload={\"text\":\"#{msg}\"}"
41
+ end
42
+ it "should encode a message with options" do
43
+ msg = "this is my awesome message"
44
+ opts = {"channel" => "#awesometown"}
45
+
46
+ subject.send(:encode_message,msg,opts).should eq "payload={\"channel\":\"#awesometown\",\"text\":\"#{msg}\"}"
47
+ end
48
+ it "should encode a basic message when there are default options" do
49
+ msg = "this is my awesome message"
50
+
51
+ subject.stub(:default_options).and_return({"icon_emoji" => "ghost"})
52
+ subject.send(:encode_message,msg,{}).should eq "payload={\"icon_emoji\":\"ghost\",\"text\":\"#{msg}\"}"
53
+ end
54
+ it "should encode a message with option when there are default options present" do
55
+ msg = "this is my awesome message"
56
+ opts = {"channel" => "#awesometown"}
57
+
58
+ subject.stub(:default_options).and_return({"icon_emoji" => "ghost"})
59
+ subject.send(:encode_message,msg,opts).should eq "payload={\"icon_emoji\":\"ghost\",\"channel\":\"#awesometown\",\"text\":\"#{msg}\"}"
60
+ end
61
+ end
62
+
63
+ describe "#send_request" do
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'slackr'
5
+ require 'webmock/rspec'
6
+
7
+ RSpec.configure do |config|
8
+ # Use color in STDOUT
9
+ config.color_enabled = true
10
+
11
+ # Use color not only in STDOUT but also in pagers and files
12
+ config.tty = true
13
+
14
+ # Use the specified formatter
15
+ config.formatter = :documentation #:progress, :html, :textmate
16
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slackr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Rohwedder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Talk to slack.com chat platform from ruby
79
+ email:
80
+ - jro@risk.io
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/slackr.rb
91
+ - lib/slackr/errors.rb
92
+ - lib/slackr/version.rb
93
+ - slackr.gemspec
94
+ - spec/client_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: ''
97
+ licenses:
98
+ - MIT
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 3415245645866188118
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: 3415245645866188118
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.23
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Send data into Slack in real-time, via the Incoming Webhooks API
127
+ test_files:
128
+ - spec/client_spec.rb
129
+ - spec/spec_helper.rb