slacking 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 226004f4bac0af53510170e39010e56cb75707d2
4
+ data.tar.gz: 736175a5789aaee2f9721f706e0c0d83fca149eb
5
+ SHA512:
6
+ metadata.gz: 7b6830b6546003af331d4657184a6c81740ad75c9e2289bc9e5d3726c23df627f5d609e9507cd6d3f8a3fe05ad54c6b3b67e1047cd367960aabc60e6c82be22d
7
+ data.tar.gz: 131036821066f0c5dd17a56aad90b6005e22323ccb73335faab21e05ae7f3ce374aca49290d1e144e39a53d7ca64eed99e12a03373097cd41164a5cc3e8d8405
data/.gitignore ADDED
@@ -0,0 +1,21 @@
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
18
+ lib/config
19
+ lib/profiles
20
+ *.DS_Store
21
+ lib/user_profiles
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'minitest'
5
+ gem 'mocha'
6
+ gem 'webmock', :require => 'webmock/minitest'
7
+ end
8
+
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Adam Ryan
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,51 @@
1
+ # Slacking
2
+
3
+ Slacking is a small Ruby CLI to post to slack chat room with different various profiles (impersonating other users, i.e. Tim Lincecum)
4
+
5
+ ## Dependencies
6
+
7
+ - Ruby 1.9 or better
8
+
9
+ ## Installation
10
+
11
+ Install with RubyGems.
12
+
13
+ ```
14
+ $ gem install slacking
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Post messages to Slack
20
+
21
+ ```
22
+ $ slacking
23
+ ```
24
+
25
+ This will create a session that will allow you to use and reuse multiple profiles to post messages to slack. You will need your slack api token.
26
+
27
+ ### Profiles
28
+
29
+ Profiles are stored in `~/.slacking_profiles`
30
+
31
+ Config (slack api token) is stored in `~/.slacking_config`
32
+
33
+ You can change these locations by running Slacking using Environment Variables
34
+
35
+ `PROFILES_DIR, CONFIG_DIR, HOME`
36
+
37
+ ### Editing Profiles
38
+
39
+ Profiles can be edited by using either a `--image` or `--channel` tag when specifying the username
40
+
41
+ ## License
42
+
43
+ Slacking is MIT licensed (see LICENSE.txt)
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( http://github.com/<my-github-username>/slacking/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'bundler/gem_tasks'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ end
10
+
11
+ task :default => :test
data/bin/slacking ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'slacking'
5
+ rescue LoadError
6
+ executable = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
7
+ $:.unshift File.join(File.dirname(executable), '..', 'lib')
8
+ require 'slacking'
9
+ end
10
+
11
+ Slacking.run
data/lib/slacking.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'slacking/http_client'
2
+ require 'slacking/config'
3
+ require 'slacking/help'
4
+ require 'slacking/io'
5
+ require 'slacking/profile'
6
+ require 'slacking/version'
7
+
8
+ module Slacking
9
+ class << self
10
+ include IO
11
+ include Config
12
+ include Profile
13
+ include Help
14
+
15
+ def run
16
+ @token = get_slack_token
17
+ listen
18
+ end
19
+
20
+ protected
21
+
22
+ def client
23
+ @client ||= Slacking::HttpClient.new(@token)
24
+ end
25
+
26
+ private
27
+
28
+ def listen
29
+ profile = initialize_profile
30
+ client.post_to_slack(profile) if profile
31
+
32
+ return if ENV['TEST']
33
+ listen
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ require 'slacking/file_management'
2
+
3
+ module Slacking
4
+ module Config
5
+ include FileManagement
6
+
7
+ protected
8
+
9
+ def get_slack_token(write_options = 'a+')
10
+ create_config_dir
11
+ config = read_config_from_disk || {}
12
+
13
+ unless config[:token]
14
+ puts "Please enter your slack api token:\n"
15
+ config.merge!(token: get_action)
16
+ write_config_to_disk(config)
17
+ end
18
+
19
+ config[:token]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,72 @@
1
+ require 'fileutils'
2
+ require 'yaml'
3
+
4
+ module Slacking
5
+ module FileManagement
6
+
7
+ private
8
+
9
+ def write_config_to_disk(data)
10
+ File.open("#{config_dir}/config.yml", 'w+') do |f|
11
+ f.write YAML.dump(data)
12
+ end
13
+ end
14
+
15
+ def read_config_from_disk
16
+ files = File.open("#{config_dir}/config.yml", 'a+') do |f|
17
+ YAML.load f
18
+ end
19
+ end
20
+
21
+ def write_profile_to_disk(data)
22
+ File.open("#{profiles_dir}/#{@profile_name}.yml", 'w+') do |f|
23
+ f.write YAML.dump(data)
24
+ end
25
+ end
26
+
27
+ def read_profile_from_disk
28
+ files = File.open("#{profiles_dir}/#{@profile_name}.yml", 'a+') do |f|
29
+ YAML.load f
30
+ end
31
+ end
32
+
33
+ def create_config_dir
34
+ return if File.directory? config_dir
35
+ if File.exist? config_dir
36
+ raise "Not a directory: #{config_dir}"
37
+ else
38
+ Dir.mkdir config_dir
39
+ end
40
+ end
41
+
42
+ def create_profiles_dir
43
+ return if File.directory? profiles_dir
44
+ if File.exist? profiles_dir
45
+ raise "Not a directory: #{profiles_dir}"
46
+ else
47
+ Dir.mkdir profiles_dir
48
+ end
49
+ end
50
+
51
+ def profiles_dir
52
+ @profiles_dir ||= ENV['PROFILES_DIR'] || File.join(home_dir, '.slacking')
53
+ end
54
+
55
+ def config_dir
56
+ @config_dir ||= ENV['CONFIG_DIR'] || File.join(home_dir, '.slacking_config')
57
+ end
58
+
59
+ def home_dir
60
+ @home_dir ||= begin
61
+ home = ENV['HOME']
62
+ home = ENV['USERPROFILE'] unless home
63
+ if !home && (ENV['HOMEDRIVE'] && ENV['HOMEPATH'])
64
+ home = File.join(ENV['HOMEDRIVE'], ENV['HOMEPATH'])
65
+ end
66
+ home = File.expand_path('~') unless home
67
+ home = 'C:/' if !home && RUBY_PLATFORM =~ /mswin|mingw/
68
+ home
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,11 @@
1
+ module Slacking
2
+ module Help
3
+ def display_help
4
+ puts "----"
5
+ puts "Type [username] to create a new profile, or use an existing one"
6
+ puts "Type [username] --image to change an existing profile's image"
7
+ puts "Type [username] --channel to change an existing profile's slack channel"
8
+ puts '----'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,60 @@
1
+ require 'slacking/config'
2
+ require 'slacking/io'
3
+ require "net/http"
4
+ require "uri"
5
+ require 'json'
6
+
7
+ module Slacking
8
+ class HttpClient
9
+ include IO
10
+ include Config
11
+
12
+ def initialize(token)
13
+ @token = token
14
+ end
15
+
16
+ def post_to_slack(profile)
17
+ @profile = profile
18
+ url = URI.parse(slack_url)
19
+
20
+ request = Net::HTTP::Post.new("#{url.path}?#{url.query}")
21
+ request.body = request_body
22
+ request.add_field 'Content-Type', 'application/json'
23
+
24
+ http = Net::HTTP.new(url.host, url.port)
25
+ http.use_ssl = true
26
+
27
+ handle_response http.request(request)
28
+ end
29
+
30
+ protected
31
+
32
+ def request_body
33
+ {
34
+ text: message,
35
+ channel: "##{@profile[:channel]}".sub('##', '#'),
36
+ icon_url: @profile[:icon_url],
37
+ username: @profile[:username],
38
+ }.to_json
39
+ end
40
+
41
+ private
42
+
43
+ def message
44
+ puts "What would you like to say to #{@profile[:channel]}, #{@profile[:username]}?"
45
+ get_action
46
+ end
47
+
48
+ def handle_response(response)
49
+ if response.body == 'No hooks'
50
+ @token = get_slack_token('w+')
51
+ else
52
+ puts response.body
53
+ end
54
+ end
55
+
56
+ def slack_url
57
+ "https://byliner.slack.com/services/hooks/incoming-webhook?token=#{@token}"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,11 @@
1
+ module Slacking
2
+ module IO
3
+
4
+ protected
5
+
6
+ def get_action
7
+ gets.chomp
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,72 @@
1
+ require 'slacking/file_management'
2
+
3
+ module Slacking
4
+ module Profile
5
+ include FileManagement
6
+
7
+ PROFILES_PATH = "#{File.expand_path(File.dirname(__FILE__))}/user_profiles".freeze
8
+ VALID_ATTRIBUTES = %w(image channel)
9
+
10
+ protected
11
+
12
+ def initialize_profile
13
+ puts "Which profile would you like to use? (type 'help' for help, or 'quit' to quit)"
14
+ @profile_name, options = get_action.split('--').map(&:strip)
15
+
16
+ profile = if @profile_name == 'help'
17
+ display_help
18
+ options = 'help'
19
+ nil
20
+ elsif @profile_name == 'quit'
21
+ exit
22
+ else
23
+ fetch_profile
24
+ end
25
+
26
+ if options && VALID_ATTRIBUTES.include?(options)
27
+ profile = change_profile_attribute(profile, options)
28
+ end
29
+
30
+ if profile
31
+ profile.delete(:is_new)
32
+ write_profile_to_disk profile
33
+ end
34
+
35
+ profile if options.nil?
36
+ end
37
+
38
+ def fetch_profile
39
+ create_profiles_dir
40
+
41
+ profile = read_profile_from_disk
42
+
43
+ if !profile
44
+ puts "Creating a new profile, #{@profile_name}."
45
+ profile = generate_profile(username: @profile_name)
46
+ end
47
+
48
+ profile
49
+ end
50
+
51
+ def generate_profile(profile)
52
+ puts "Please enter an icon url:"
53
+ profile[:icon_url] = get_action
54
+
55
+ puts "Please enter the slack channel you would like to post to:"
56
+ profile[:channel] = get_action
57
+
58
+ profile[:is_new] = true
59
+
60
+ profile
61
+ end
62
+
63
+ def change_profile_attribute(profile, attribute)
64
+ if !profile.delete(:is_new)
65
+ puts "Please enter a new #{attribute}:"
66
+ profile[attribute.to_sym] = get_action
67
+ end
68
+
69
+ profile
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module Slacking
2
+ VERSION = "0.0.2"
3
+ end
data/slacking.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slacking/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slacking"
8
+ spec.version = Slacking::VERSION
9
+ spec.authors = ["Adam Ryan"]
10
+ spec.email = ["adam.g.ryan@gmail.com"]
11
+ spec.summary = %q{Post comments to slack as different users}
12
+ spec.description = %q{Create profiles you can use to post messages to slack}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = ['slacking']
18
+ spec.test_files = spec.files.grep(%r{^(test)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class SlackingTest < Minitest::Test
4
+
5
+ def setup
6
+ ENV['TEST'] = 'true'
7
+ ENV['HOME'] = File.dirname(__FILE__).split('/')[0..-2].join('/') + '/support'
8
+ Slacking.stubs(:get_action).returns('xhibit')
9
+ Slacking::HttpClient.any_instance.stubs(:get_action).returns('message')
10
+ end
11
+
12
+ def test_profile
13
+ stub_request(:post, "https://byliner.slack.com/services/hooks/incoming-webhook?token=xhibit").
14
+ with(:body => "{\"text\":\"message\",\"channel\":\"#xhibit\",\"icon_url\":\"xhibit\",\"username\":\"xhibit\"}",
15
+ :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
16
+ to_return(:status => 200, :body => "", :headers => {})
17
+
18
+ Slacking.run
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :username: xhibit
3
+ :icon_url: xhibit
4
+ :channel: xhibit
@@ -0,0 +1,2 @@
1
+ ---
2
+ :token: xhibit
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require :test
4
+
5
+ require 'minitest/autorun'
6
+ require 'webmock/minitest'
7
+ require 'mocha/setup'
8
+ require 'slacking'
9
+
10
+ # Support files
11
+ Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
12
+ require file
13
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slacking
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Adam Ryan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-05 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Create profiles you can use to post messages to slack
42
+ email:
43
+ - adam.g.ryan@gmail.com
44
+ executables:
45
+ - slacking
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/slacking
55
+ - lib/slacking.rb
56
+ - lib/slacking/config.rb
57
+ - lib/slacking/file_management.rb
58
+ - lib/slacking/help.rb
59
+ - lib/slacking/http_client.rb
60
+ - lib/slacking/io.rb
61
+ - lib/slacking/profile.rb
62
+ - lib/slacking/version.rb
63
+ - slacking.gemspec
64
+ - test/slacking/slacking_test.rb
65
+ - test/support/.slacking/xhibit.yml
66
+ - test/support/.slacking_config/config.yml
67
+ - test/test_helper.rb
68
+ homepage: ''
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.2.0
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Post comments to slack as different users
92
+ test_files:
93
+ - test/slacking/slacking_test.rb
94
+ - test/support/.slacking/xhibit.yml
95
+ - test/support/.slacking_config/config.yml
96
+ - test/test_helper.rb