slack-progress 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b51479edb2c2459ae802ce4132f0321219d07d9
4
+ data.tar.gz: 487ad208178bcd568c7c5bcc489850812ad877f8
5
+ SHA512:
6
+ metadata.gz: 7b9c1e95f74e9a8ae722bf0948196576271cdeb0baa490cd90653d3c9f3380fbb966aaadcb74f1f2f7ffc4109e80ca00a12ce25b176928891ee1df0a6dc8b046
7
+ data.tar.gz: 5cdad3cc687948035afc3a23e34779b38d97ba7d9739c22712653738977cd83d94727d8ac4a6de153f55092312cd24c1b76b99fe426ed7f77cdeef1a5e51bae1
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ test_script.rb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.1
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in slack-progress.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ slack-progress (0.1.0)
5
+ faraday
6
+ json
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.3)
12
+ faraday (1.0.0)
13
+ multipart-post (>= 1.2, < 3)
14
+ json (2.3.0)
15
+ multipart-post (2.1.1)
16
+ rake (10.5.0)
17
+ rspec (3.9.0)
18
+ rspec-core (~> 3.9.0)
19
+ rspec-expectations (~> 3.9.0)
20
+ rspec-mocks (~> 3.9.0)
21
+ rspec-core (3.9.1)
22
+ rspec-support (~> 3.9.1)
23
+ rspec-expectations (3.9.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-mocks (3.9.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.9.0)
29
+ rspec-support (3.9.2)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 2.0)
36
+ rake (~> 10.0)
37
+ rspec (~> 3.0)
38
+ slack-progress!
39
+
40
+ BUNDLED WITH
41
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Supreet
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,58 @@
1
+ # SlackProgress
2
+
3
+ ## Demo
4
+
5
+ ![Demo](https://i.imgur.com/3BFrK53.gif)
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'slack-progress'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install slack-progress
23
+
24
+ ## Usage
25
+ Pre-requisites:
26
+ - A slack app for your workspace with `chat:write` permissions.
27
+ - Channel ID where messages should be posted (starts with 'C')
28
+ Then you can run the following
29
+ ```ruby
30
+ token = YOUR_BOT_TOKEN_HERE # Required
31
+ channel_id = YOUR_CHANNEL_ID_HERE # Required (Note: this is different from Channel name)
32
+ title = 'My Progress:' # Optional (Default is '')
33
+
34
+ p = SlackProgress.new(token, channel_id, {title: title})
35
+ thread_id = nil
36
+ count = 0
37
+
38
+ while count <= 50
39
+ response = p.update_progress(thread_id: thread_id, current_value: count, max_value: 50)
40
+ thread_id = response.thread_id
41
+ count += 1
42
+ sleep (1)
43
+ end
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ 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).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/supreet321/slack-progress.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "slack/progress"
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(__FILE__)
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,27 @@
1
+ require 'slack-progress/message_builder'
2
+ require 'slack-progress/options_parser'
3
+ require 'slack-progress/connection'
4
+
5
+ module SlackProgress
6
+ class Client
7
+
8
+ attr_reader :parsed_options
9
+
10
+ def initialize(token, channel_id, options = {})
11
+ raise ArgumentError, 'Token required' if token.nil?
12
+ raise ArgumentError, 'Channel ID required' if channel_id.nil?
13
+
14
+ @token = token
15
+ @channel_id = channel_id
16
+ @parsed_options = OptionsParser.new(options).parse
17
+ end
18
+
19
+ def update_progress(thread_id: nil, current_value:, max_value:)
20
+ message_builder = MessageBuilder.new(parsed_options.formatting_options)
21
+ text = message_builder.build_message(current_value, max_value)
22
+
23
+ connection = Connection.new(@token, @channel_id)
24
+ connection.send_request(text, thread_id)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,62 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SlackProgress
5
+ class Connection
6
+
7
+ NEW_MESSAGE_URL = 'https://slack.com/api/chat.postMessage'
8
+ UPDATE_MESSAGE_URL = 'https://slack.com/api/chat.update'
9
+
10
+ class Response < OpenStruct
11
+ def initialize(status, thread_id, body)
12
+ super(status: status, thread_id: thread_id, body: body)
13
+ end
14
+ end
15
+
16
+ def initialize(token, channel_id)
17
+ @token = token
18
+ @channel_id = channel_id
19
+ end
20
+
21
+ def send_request(text, thread_id)
22
+ url = thread_id.nil? ? NEW_MESSAGE_URL : UPDATE_MESSAGE_URL
23
+ data = build_form_data(text, thread_id)
24
+
25
+ raw_response = Faraday.post(url) do |req|
26
+ req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
27
+ req.body = URI.encode_www_form(data)
28
+ req.options[:timeout] = 2
29
+ req.options[:open_timeout] = 2
30
+ end
31
+
32
+ handle_errors(raw_response)
33
+ parsed_response(raw_response)
34
+ end
35
+
36
+ private
37
+
38
+ def build_form_data(text, thread_id)
39
+ form_data = { token: @token, channel: @channel_id, text: text }
40
+ form_data.merge!(ts: thread_id) if thread_id
41
+ form_data.merge!(@app_options.to_h)
42
+ form_data
43
+ end
44
+
45
+ def handle_errors(response)
46
+ raise SlackProgress::ConnectionError, "Received #{response.status}" unless response.success?
47
+
48
+ parsed_response = JSON.parse(response.body)
49
+ if parsed_response['ok'] == false
50
+ raise SlackProgress::InvalidRequestError, "Error: #{parsed_response['error']}"
51
+ end
52
+ end
53
+
54
+ def parsed_response(response)
55
+ body = response.body
56
+ status = response.status
57
+ thread_id = JSON.parse(body).fetch('ts', nil)
58
+
59
+ Response.new(status, thread_id, body)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,4 @@
1
+ module SlackProgress
2
+ class InvalidRequestError < StandardError; end
3
+ class ConnectionError < StandardError; end
4
+ end
@@ -0,0 +1,26 @@
1
+ module SlackProgress
2
+ class MessageBuilder
3
+
4
+ def initialize(formatting_options)
5
+ @formatting_options = formatting_options
6
+ end
7
+
8
+ def build_message(current_value, max_value)
9
+ num_boxes = 10
10
+
11
+ ratio = current_value.to_f / max_value.to_f
12
+ percentage = (ratio * 100).round(1)
13
+
14
+ fill_count = (percentage / num_boxes).round
15
+ remaining_count = num_boxes - fill_count
16
+
17
+ message = @formatting_options.title
18
+ message += "\n"
19
+ message += (@formatting_options.completed_filler) * fill_count
20
+ message += (@formatting_options.remaining_filler) * remaining_count
21
+ message += " #{current_value} / #{max_value}"
22
+ message += " (#{percentage} %) finished."
23
+ message
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ module SlackProgress
2
+ class OptionsParser
3
+
4
+ def initialize(options = {})
5
+ @options = options
6
+ end
7
+
8
+ def parse
9
+ OpenStruct.new(
10
+ app_options: app_options,
11
+ formatting_options: formatting_options,
12
+ )
13
+ end
14
+
15
+ private
16
+
17
+ def app_options
18
+ OpenStruct.new(
19
+ # username: username,
20
+ # icon_emoji: icon_emoji,
21
+ )
22
+ end
23
+
24
+ def formatting_options
25
+ OpenStruct.new(
26
+ remaining_filler: remaining_filler,
27
+ completed_filler: completed_filler,
28
+ title: title,
29
+ )
30
+ end
31
+
32
+ def remaining_filler
33
+ @options[:remaining_filler] || ':white_medium_square:'
34
+ end
35
+
36
+ def completed_filler
37
+ @options[:completed_filler] || ':black_medium_square:'
38
+ end
39
+
40
+ def title
41
+ @options[:title] || ''
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module SlackProgress
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'slack-progress/version'
2
+ require 'slack-progress/client'
3
+ require 'slack-progress/error'
4
+
5
+ module SlackProgress
6
+ def self.new(token, channel_id, options={})
7
+ SlackProgress::Client.new(token, channel_id, options)
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "slack-progress/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "slack-progress"
7
+ spec.version = SlackProgress::VERSION
8
+ spec.authors = ["Supreet"]
9
+ spec.email = ["supreets123@gmail.com"]
10
+
11
+ spec.summary = %q{Send progress updates to Slack}
12
+ spec.description = %q{Send progress updates to Slack}
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.0"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_dependency "faraday"
29
+ spec.add_dependency "json"
30
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slack-progress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Supreet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-12 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Send progress updates to Slack
84
+ email:
85
+ - supreets123@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/slack-progress.rb
101
+ - lib/slack-progress/client.rb
102
+ - lib/slack-progress/connection.rb
103
+ - lib/slack-progress/error.rb
104
+ - lib/slack-progress/message_builder.rb
105
+ - lib/slack-progress/options_parser.rb
106
+ - lib/slack-progress/version.rb
107
+ - slack-progress.gemspec
108
+ homepage:
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.6.14
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Send progress updates to Slack
132
+ test_files: []