operationcode-slack 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ operationcode-slack
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ addons:
3
+ code_climate:
4
+ repo_token: 3fedf540189b9a4cab8cd906f464d2db431b8d76888d74828c80db4c14c2f065
5
+
6
+ after_success:
7
+ - bundle exec codeclimate-test-reporter
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at rrein@akamai.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in operationcode-slack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 RickR
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # [Operationcode::Slack](https://github.com/OperationCode/operationcode-slack)
2
+
3
+ [![Build Status](https://travis-ci.org/OperationCode/operationcode-slack.svg?branch=master)](https://travis-ci.org/OperationCode/operationcode-slack)
4
+ [![Code Climate](https://codeclimate.com/github/OperationCode/operationcode-slack/badges/gpa.svg)](https://codeclimate.com/github/OperationCode/operationcode-slack)
5
+ [![Test Coverage](https://codeclimate.com/github/OperationCode/operationcode-slack/badges/coverage.svg)](https://codeclimate.com/github/OperationCode/operationcode-slack/coverage)
6
+
7
+ This gem provides a common interface to slack for the Operation Code platform.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'operationcode-slack', git: 'https://github.com/OperationCode/operationcode-slack'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ * Require the gem in your application like so:
24
+ ```ruby
25
+ require 'operationcode/slack'
26
+ ```
27
+
28
+ You can now call the slack API like so
29
+
30
+ ```ruby
31
+ Operationcode::Slack::Api::ChannelInvite.post(with_data: { channel_id: '1234', user: 'ID1234' })
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ To add a new API endpoint simply add a new class in ```lib/operationcode/slack/api.rb```. The name of the class determines its URL.
41
+ For example - the users info endpoint URL is https://slack.com/api/users.invite. The class name should be ```UsersInvite```
42
+
43
+ If you need more advanced funcationality please define your class in ```lib/operationcode/slack/api/```. See ```OauthAccess.rb``` for an example.
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/OperationCode/operationcode-slack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "operationcode/slack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require 'operationcode/slack/version'
2
+ require 'operationcode/slack/user'
3
+ require 'operationcode/slack/api'
4
+ require 'operationcode/slack/im'
5
+ require 'operationcode/slack/im/interactive'
6
+ require 'active_support/all'
@@ -0,0 +1,42 @@
1
+ require 'httparty'
2
+
3
+ # Require all API classes
4
+ Dir[File.join(File.dirname(__FILE__), 'api', '**/*.rb')].sort.each { |file| require file }
5
+
6
+ module Operationcode
7
+ module Slack
8
+ class Api
9
+ SLACK_API_BASE = 'https://slack.com/api/'
10
+
11
+ # To add addtional API endpoints define a new class here. The class name should be capitalized based on the location of the
12
+ # slack API path's '.' For example - the users info endpoint URL is https://slack.com/api/users.invite.
13
+ # The class name should be `UsersInvite` If you just need to post data that should be all you need.
14
+ # If you'd like to add addtional methods to the class define it in its own file in the api dir (see OauthAccess)
15
+ class ChannelsInvite < Operationcode::Slack::Api; end
16
+ class UsersInfo < Operationcode::Slack::Api; end
17
+ class ImOpen < Operationcode::Slack::Api; end
18
+ class ChatPostMessage < Operationcode::Slack::Api; end
19
+ class ChatUpdate < Operationcode::Slack::Api; end
20
+
21
+ def self.post(with_data:)
22
+ HTTParty.post(api_url, body: with_data)
23
+ end
24
+
25
+ def self.api_url
26
+ SLACK_API_BASE + convert_class_name_to_api_name
27
+ end
28
+
29
+ private
30
+
31
+ # Slack API methods are in the format of 'group.method', (eg chat.postMessage) and are case sensitive.
32
+ # This ugly method just converts ruby class names to the correct URL format
33
+ def self.convert_class_name_to_api_name
34
+ self.name.to_s.split('::').last.match(/(.*?[a-z])([A-Z].*)/)
35
+ group = $1
36
+ method = $2
37
+
38
+ "#{group.downcase}.#{method.camelize(:lower)}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ module Operationcode
2
+ module Slack
3
+ class Api
4
+ class OauthAccess < Operationcode::Slack::Api
5
+ def self.post(with_data:)
6
+ with_data[:client_secret] = ENV.fetch('SLACK_CLIENT_SECRET')
7
+ with_data[:client_id] = ENV.fetch('SLACK_CLIENT_ID')
8
+ super(with_data: with_data)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ module Operationcode
2
+ module Slack
3
+ class Im
4
+ attr_reader :payload
5
+
6
+ def initialize(user: nil, channel: nil, text: '')
7
+ @user = user
8
+ @channel = channel
9
+ @text = text
10
+
11
+ @payload = api_params
12
+
13
+ raise(ArgumentError, 'either user or channel must be provided') if @user.nil? && @channel.nil?
14
+ end
15
+
16
+ def deliver(text = @text)
17
+ resolve_channel!
18
+ set_payload_for!(text)
19
+ Operationcode::Slack::Api::ChatPostMessage.post(with_data: @payload)
20
+ end
21
+
22
+ def make_interactive_with!(interactive_message)
23
+ @payload['attachments'] = interactive_message.payload.to_json
24
+ end
25
+
26
+ private
27
+
28
+ def set_payload_for!(text)
29
+ @payload[:text] = text
30
+ end
31
+
32
+ def resolve_channel!
33
+ if @channel.nil?
34
+ im = Operationcode::Slack::Api::ImOpen.post(with_data: { token: ENV.fetch('SLACK_TOKEN'), user: @user })
35
+ raise("Resolving channel failed: #{im}") if im['ok'] == false
36
+ @payload['channel'] = im['channel']['id']
37
+ end
38
+ end
39
+
40
+ def api_params
41
+ {
42
+ token: ENV.fetch('SLACK_TOKEN'),
43
+ as_user: false,
44
+ username: 'operationcodebot',
45
+ 'channel' => @channel
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ module Operationcode
2
+ module Slack
3
+ class Im
4
+ class Interactive
5
+ REQUIRED_ACTION_KEYS = %i(name text value)
6
+ VALID_STYLES = %i(default primary danger)
7
+
8
+ def initialize(text:, id:, color: '#3AA3E3', actions:)
9
+ @text = text
10
+ @id = id
11
+ @color = color
12
+ @actions = actions
13
+
14
+ validate_actions!
15
+ end
16
+
17
+ def validate_actions!
18
+ raise(ArgumentError, 'actions must be an array') unless @actions.is_a? Array
19
+ raise(ArgumentError, 'actions cannot be empty') if @actions.empty?
20
+ @actions.each { |a| raise ArgumentError, "action #{a} is invalid" unless a.keys == REQUIRED_ACTION_KEYS }
21
+ end
22
+
23
+ def payload
24
+ [
25
+ {
26
+ text: @text,
27
+ fallback: @text,
28
+ callback_id: @id,
29
+ color: @color,
30
+ attachment_type: 'default',
31
+ actions: action_payload
32
+ }
33
+ ]
34
+ end
35
+
36
+ def action_payload
37
+ @actions.map do |action|
38
+ action[:style] = :default if action[:style].nil?
39
+ {
40
+ name: action[:name],
41
+ text: action[:text],
42
+ value: action[:value],
43
+ style: action[:style],
44
+ type: :button
45
+ }
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ module Operationcode
2
+ module Slack
3
+ class User
4
+ attr_reader :id, :name
5
+
6
+ def initialize(data)
7
+ # If we get a string should we go look the users info up?
8
+ if data.is_a? String
9
+ @id = data
10
+ populate_name!
11
+ else
12
+ @id = data['id']
13
+ @name = data['name']
14
+ end
15
+ end
16
+
17
+ def populate_name!
18
+ res = Operationcode::Slack::Api::UsersInfo.post(with_data: { user: @id })
19
+ @name = res['user']['real_name'] if res['user']
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Operationcode
2
+ module Slack
3
+ VERSION = '0.6.0'
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'operationcode/slack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'operationcode-slack'
8
+ spec.version = Operationcode::Slack::VERSION
9
+ spec.authors = ['RickR']
10
+ spec.email = ["rrein@akamai.com"]
11
+
12
+ spec.summary = %q{Operation Code Slack library}
13
+ spec.description = %q{This is a gem to interact with the Slack for all apps on the Opeartion Code platform}
14
+ spec.homepage = 'https://github.com/OperationCode/operationcode-slack'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.12'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rb-readline'
25
+ spec.add_development_dependency 'minitest', '~> 5.0'
26
+ spec.add_development_dependency 'mocha'
27
+ spec.add_development_dependency 'simplecov'
28
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
29
+ spec.add_dependency 'httparty'
30
+ spec.add_dependency 'activesupport'
31
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: operationcode-slack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - RickR
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rb-readline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: httparty
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: This is a gem to interact with the Slack for all apps on the Opeartion
140
+ Code platform
141
+ email:
142
+ - rrein@akamai.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".codeclimate.yml"
148
+ - ".gitignore"
149
+ - ".rubocop.yml"
150
+ - ".ruby-gemset"
151
+ - ".ruby-version"
152
+ - ".travis.yml"
153
+ - CODE_OF_CONDUCT.md
154
+ - Gemfile
155
+ - LICENSE.txt
156
+ - README.md
157
+ - Rakefile
158
+ - bin/console
159
+ - bin/setup
160
+ - lib/operationcode/slack.rb
161
+ - lib/operationcode/slack/api.rb
162
+ - lib/operationcode/slack/api/OauthAccess.rb
163
+ - lib/operationcode/slack/im.rb
164
+ - lib/operationcode/slack/im/interactive.rb
165
+ - lib/operationcode/slack/user.rb
166
+ - lib/operationcode/slack/version.rb
167
+ - operationcode-slack.gemspec
168
+ homepage: https://github.com/OperationCode/operationcode-slack
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 2.5.1
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: Operation Code Slack library
192
+ test_files: []