correios_slack_bot 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 690a0c724e087f96829438a8cec166479fe141b2
4
+ data.tar.gz: 6723e1aee73dfd77efce0395459903b2421f337e
5
+ SHA512:
6
+ metadata.gz: 603cefd1e1132ccb394fc1cad4d8b83391b8caeedb89893e196a82497fbca3aa95b72d21364f99622a0267fd9c18cb7b9865026d12d1e74c05f2b7715ec9ad4f
7
+ data.tar.gz: 7d6b8745ea5d7f521f411a00e39251a91693506247a3b741c5850f553f6727c829cb96441ac1cc42a508299ce7e009b546a3a07a3131d548cf0ba23f8e052de8
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ *.pid
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ correios_slack_bot
@@ -0,0 +1 @@
1
+ ruby-2.3.1
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
6
+ after_script: CODECLIMATE_REPO_TOKEN=$CODECLIMATE_REPO_TOKEN bundle exec codeclimate-test-reporter
7
+ notifications:
8
+ slack:
9
+ secure: qR7VlOWb+ZNq6+e1IQTVoFjfqDFO0qNhHmHN1HH2ZiBlWRu9w0cemRpQdw8t5U/UICkIha0TwVLTQQIp/BrBXBb8qm/8UfE20YWamRaXF/DspudkHpiexRUJ9x33xPMP/jKYahbPImiovFR2RTLsrz1612kiW+OvvVBx1bAXyCrg+ZY2AOD+jYJfvDMNNm2U7uNquP4QQIuLlJ3H8qdY1Xbs/XPitP/69BZdZbcJRbOL8xplIHmYWfF/KrYFO5qnlBZYAraFp1l8cte5U38YIMzyBYllv5Jnjr26b5nBejampuUhPeI4cMAoux17yvBeekgEi9svThaIvBEgQQ8OnLoYnY1ueFESwDfz2YeiOhMRGt0BTpcmmGJkx1O5ny45sy8YK83fB8TYqeu82UXbcnB5sdpEAoVC5ETlSHM8iz+3LWDsaRBU8Vem9TUwn1VPIzIYxlo97UO6KbyPsXaeNyu2G5CHoR7o+IpdiR3qrWqHoEbZSjzPPqcuFmwK8zOq5j4K8JmH2Zj2ZKkBi3MtlzKUu1knHH+qO6oqMRsD5EboJmzA7OLyTv4+FWHIWgUWtKfs0N8O6cBPGaxadWgvmjdE++AunF7BZWnYuCTXfS2Z9jjDv7FOrpZIuki1CRmGQg/iTp6+dfounU09R+2AM9Pn5u6JpdqhQjI+dJe92tg=
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in correios_slack_bot.gemspec
4
+ gemspec
5
+ group :test do
6
+ gem 'simplecov'
7
+ gem 'codeclimate-test-reporter', '~> 1.0.0'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Pedro Pazello
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.
@@ -0,0 +1,41 @@
1
+ # CorreiosSlackBot
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/correios_slack_bot`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'correios_slack_bot'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install correios_slack_bot
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/correios_slack_bot.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "correios_slack_bot"
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
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "daemons"
4
+ Daemons.run('lib/run.rb')
@@ -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,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'correios_slack_bot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "correios_slack_bot"
8
+ spec.version = CorreiosSlackBot::VERSION
9
+ spec.authors = ["Pedro Pazello"]
10
+ spec.email = ["pedropazello.rj@gmail.com"]
11
+
12
+ spec.summary = %q{
13
+ Slack integration with Correios package log web site. Search for a package log and alert when
14
+ this package is waiting for you.
15
+ }
16
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
17
+ spec.homepage = "https://github.com/pedropazello/correios_slack_bot"
18
+ spec.license = "MIT"
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "bin"
23
+ spec.executables = ["correios_slack_bot"]
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "vcr", "~> 3.0"
30
+ spec.add_development_dependency "webmock", "~> 2.1"
31
+ spec.add_development_dependency "pry", "~> 0.10.4"
32
+ spec.add_dependency "httparty", "~> 0.14.0"
33
+ spec.add_dependency "nokogiri", "~> 1.6"
34
+ spec.add_dependency "slack-ruby-bot", "~> 0.9.0"
35
+ spec.add_dependency "celluloid-io", "~> 0.17.3"
36
+ spec.add_dependency "daemons", "~> 1.2"
37
+ end
@@ -0,0 +1,44 @@
1
+ require "correios_slack_bot/version"
2
+ require "correios_slack_bot/correios"
3
+ require "correios_slack_bot/correios_monitor"
4
+ require "slack-ruby-bot"
5
+
6
+ module CorreiosSlackBot
7
+ class Bot < SlackRubyBot::Bot
8
+ match /^exibe o pacote (?<package_code>\w*)$/ do |client, data, match|
9
+ package_log = CorreiosSlackBot::Correios.new(match[:package_code]).package_log
10
+ result = package_log.map do |log|
11
+ {
12
+ title: "#{log[:date]} - #{log[:origin]}",
13
+ text: "#{log[:description]} (#{log[:status]})"
14
+ }
15
+ end
16
+ client.web_client.chat_postMessage(
17
+ channel: data.channel,
18
+ as_user: true,
19
+ attachments: result
20
+ )
21
+ end
22
+
23
+ match /^rastreia o pacote (?<package_code>\w*)$/ do |client, data, match|
24
+ client.say(text: 'Beleza, qualquer coisa te falo!', channel: data.channel)
25
+ monitor = CorreiosSlackBot::CorreiosMonitor.new(match[:package_code])
26
+ loop do
27
+ sleep 30
28
+ last_change = monitor.last_change
29
+
30
+ if monitor.package_log_changed?
31
+ monitor.known_package_log = monitor.correios_package_log
32
+ client.web_client.chat_postMessage(
33
+ channel: data.channel,
34
+ as_user: true,
35
+ attachments: [
36
+ title: "Última atualização : #{last_change[:date]} - #{last_change[:origin]}",
37
+ text: "#{last_change[:description]} (#{last_change[:status]})"
38
+ ]
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,25 @@
1
+ require 'httparty'
2
+ require 'correios_slack_bot/correios_scraper'
3
+
4
+ module CorreiosSlackBot
5
+ class Correios
6
+ BASE_URL = "http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&"
7
+ def initialize(package_code)
8
+ @package_code = package_code
9
+ end
10
+
11
+ def package_log
12
+ correios_scraper.parsed_package_log
13
+ end
14
+
15
+ private
16
+
17
+ def correios_scraper
18
+ @correios_scraper ||= CorreiosScraper.new(correios_response)
19
+ end
20
+
21
+ def correios_response
22
+ HTTParty.get("#{BASE_URL}P_COD_UNI=#{@package_code}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module CorreiosSlackBot
2
+ class CorreiosMonitor
3
+ attr_accessor :package_code, :known_package_log
4
+ def initialize(package_code, options = {})
5
+ @package_code = package_code
6
+ @known_package_log = options[:from_package_log] || []
7
+ end
8
+
9
+ def last_change
10
+ correios_package_log[0]
11
+ end
12
+
13
+ def package_log_changed?
14
+ (correios_package_log - known_package_log).size > 0
15
+ end
16
+
17
+ def correios_package_log
18
+ CorreiosSlackBot::Correios.new(package_code).package_log
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ require 'nokogiri'
2
+
3
+ module CorreiosSlackBot
4
+ class CorreiosScraper
5
+ def initialize(correios_package_log_page)
6
+ @doc = Nokogiri::HTML(correios_package_log_page)
7
+ end
8
+
9
+ def parsed_package_log
10
+ package_log_lines.map.with_index do |line, index|
11
+ next if only_status_line?(line)
12
+ {
13
+ date: find_date(line),
14
+ origin: find_origin(line),
15
+ status: find_status(line),
16
+ description: find_description(index)
17
+ }
18
+ end.compact
19
+ end
20
+
21
+ private
22
+
23
+ def package_log_lines
24
+ @doc.xpath('//table//tr').drop(1)
25
+ end
26
+
27
+ def only_status_line?(line)
28
+ line.children[1].nil?
29
+ end
30
+
31
+ def find_date(line)
32
+ line.children[0].text
33
+ end
34
+
35
+ def find_origin(line)
36
+ line.children[1].text
37
+ end
38
+
39
+ def find_status(line)
40
+ line.children[2].text
41
+ end
42
+
43
+ def find_description(current_index)
44
+ find_current_line_description(current_index)&.children&.text
45
+ end
46
+
47
+ def find_current_line_description(current_index)
48
+ package_log_lines[current_index + 1]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module CorreiosSlackBot
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'correios_slack_bot'
2
+ CorreiosSlackBot::Bot.run
metadata ADDED
@@ -0,0 +1,219 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: correios_slack_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Pazello
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-24 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: httparty
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.14.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.14.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.6'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.6'
125
+ - !ruby/object:Gem::Dependency
126
+ name: slack-ruby-bot
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.9.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.9.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: celluloid-io
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.17.3
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.17.3
153
+ - !ruby/object:Gem::Dependency
154
+ name: daemons
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.2'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.2'
167
+ description:
168
+ email:
169
+ - pedropazello.rj@gmail.com
170
+ executables:
171
+ - correios_slack_bot
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".ruby-gemset"
178
+ - ".ruby-version"
179
+ - ".travis.yml"
180
+ - Gemfile
181
+ - LICENSE.txt
182
+ - README.md
183
+ - Rakefile
184
+ - bin/console
185
+ - bin/correios_slack_bot
186
+ - bin/setup
187
+ - correios_slack_bot.gemspec
188
+ - lib/correios_slack_bot.rb
189
+ - lib/correios_slack_bot/correios.rb
190
+ - lib/correios_slack_bot/correios_monitor.rb
191
+ - lib/correios_slack_bot/correios_scraper.rb
192
+ - lib/correios_slack_bot/version.rb
193
+ - lib/run.rb
194
+ homepage: https://github.com/pedropazello/correios_slack_bot
195
+ licenses:
196
+ - MIT
197
+ metadata: {}
198
+ post_install_message:
199
+ rdoc_options: []
200
+ require_paths:
201
+ - lib
202
+ required_ruby_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ required_rubygems_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ requirements: []
213
+ rubyforge_project:
214
+ rubygems_version: 2.6.3
215
+ signing_key:
216
+ specification_version: 4
217
+ summary: Slack integration with Correios package log web site. Search for a package
218
+ log and alert when this package is waiting for you.
219
+ test_files: []