chatwork_to 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +28 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +7 -0
- data/bin/chatwork_to +4 -0
- data/chatwork_to.gemspec +33 -0
- data/chatwork_to.yml.example +14 -0
- data/lib/chatwork_to/client.rb +102 -0
- data/lib/chatwork_to/config.rb +44 -0
- data/lib/chatwork_to/notifier.rb +23 -0
- data/lib/chatwork_to/notifiers/simple.rb +24 -0
- data/lib/chatwork_to/notifiers/slack.rb +69 -0
- data/lib/chatwork_to/process.rb +87 -0
- data/lib/chatwork_to/response.rb +63 -0
- data/lib/chatwork_to/version.rb +3 -0
- data/lib/chatwork_to.rb +14 -0
- metadata +204 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 150334bc99e0afeebdf86419dae2a714cffb2b77
|
4
|
+
data.tar.gz: d06633cff98c372bd13831c6ee72b8166e4b691a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38c1b628234132f278233fcb5aa3d1bdae9fb32bca2aa2355b6c44cccae35f048b4e2ca3faa02ca2e3a55fd6acdca86ff7e3273f58e3b64eef87a76b936f0eea
|
7
|
+
data.tar.gz: 5f0eb9252718081586f701ce6ba501586e214b630de8a6033fb34c58df821b7056d7b6b48bd67a1d1f01524ebd102114c90970b8ad3401691d3f3d8e77fbd65c
|
data/.gitignore
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
|
24
|
+
# local gems
|
25
|
+
vendor/bundle/*
|
26
|
+
|
27
|
+
# configuration file
|
28
|
+
chatwork_to.yml
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 nalabjp
|
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,69 @@
|
|
1
|
+
# ChatworkTo
|
2
|
+
|
3
|
+
ChatWorkTo can transfer ChatWork messages via notifier.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'chatwork_to'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install chatwork_to
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
### Setup chatwork_to.yml
|
21
|
+
Create configuration file named `chatwork_to.yml` in `./` or `~/` directory. Required configuration are:
|
22
|
+
```
|
23
|
+
chatwork:
|
24
|
+
email: 'email'
|
25
|
+
pass: 'password'
|
26
|
+
rooms:
|
27
|
+
- 'room id'
|
28
|
+
```
|
29
|
+
`rooms` is array for multiple room id.
|
30
|
+
|
31
|
+
## Notifiers
|
32
|
+
### Simple
|
33
|
+
Simple Notifier which is based on Ruby Logger is output to `$stdout`.
|
34
|
+
```
|
35
|
+
notifiers:
|
36
|
+
-
|
37
|
+
name: simple
|
38
|
+
```
|
39
|
+
If used `io` option, logger output to file. `rotate`(Default:`weekly`) option is compliant with the rotate option of Ruby Logger.
|
40
|
+
```
|
41
|
+
notifiers:
|
42
|
+
-
|
43
|
+
name: simple
|
44
|
+
io: '~/logs/chatwork_to.log'
|
45
|
+
rotate: weekly
|
46
|
+
```
|
47
|
+
|
48
|
+
### Slack
|
49
|
+
Slack Notifier forwards to Slack messages of ChatWork via Slack API.
|
50
|
+
```
|
51
|
+
notifiers:
|
52
|
+
-
|
53
|
+
name: slack
|
54
|
+
token: 'API authentication token'
|
55
|
+
channel: 'public channel or private group or @user'
|
56
|
+
```
|
57
|
+
|
58
|
+
## Run
|
59
|
+
```
|
60
|
+
$ chatwork_to
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it ( https://github.com/[my-github-username]/chatwork_to/fork )
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/chatwork_to
ADDED
data/chatwork_to.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'chatwork_to/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "chatwork_to"
|
8
|
+
spec.version = ChatworkTo::VERSION
|
9
|
+
spec.authors = ["nalabjp"]
|
10
|
+
spec.email = ["nalabjp@gmail.com"]
|
11
|
+
spec.summary = %q{Transfer ChatWork messages.}
|
12
|
+
spec.description = %q{ChatWorkTo can transfer ChatWork messages via notifier.}
|
13
|
+
spec.homepage = "https://github.com/nalabjp/chatwork_to"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_dependency "activesupport", '~> 4.1.4'
|
22
|
+
spec.add_dependency "mechanize", '~> 2.7.3'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
#spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
spec.add_development_dependency "pry-doc"
|
29
|
+
spec.add_development_dependency "pry-stack_explorer"
|
30
|
+
spec.add_development_dependency "pry-byebug"
|
31
|
+
spec.add_development_dependency "pry-rescue"
|
32
|
+
spec.add_development_dependency "awesome_print"
|
33
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
chatwork:
|
2
|
+
email: 'mail_address_here'
|
3
|
+
pass: 'password_here'
|
4
|
+
rooms:
|
5
|
+
- 'room_id_here'
|
6
|
+
notifiers:
|
7
|
+
-
|
8
|
+
name: simple
|
9
|
+
# io: '/path/to/log_file'
|
10
|
+
# rotate: 'Ruby Logger rotate option'
|
11
|
+
#-
|
12
|
+
# name: slack
|
13
|
+
# token: 'token_here' # API authentication token
|
14
|
+
# channel: 'channel_here' # public channel or private group or @user
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module ChatworkTo
|
2
|
+
class Client
|
3
|
+
CHATWORK_URL = 'https://www.chatwork.com'
|
4
|
+
CHATWORK_ALL_JA = 'chatwork_all_ja.min.js'
|
5
|
+
CONSTANTS_REGEXP = {
|
6
|
+
myid: /var myid( *)=( *)'(.*)'/,
|
7
|
+
ln: /var LANGUAGE( *)=( *)'(.*)'/,
|
8
|
+
_t: /var ACCESS_TOKEN( *)=( *)'(.*)'/,
|
9
|
+
_v: /var client_ver( *)=( *)'(.*)'/,
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def initialize(email, pass)
|
13
|
+
@config = {}
|
14
|
+
@chatwork_email = email
|
15
|
+
@chatwork_pass = pass
|
16
|
+
@mechanize = ::Mechanize.new
|
17
|
+
prepare
|
18
|
+
end
|
19
|
+
|
20
|
+
def init_load
|
21
|
+
JSON.parse(@mechanize.get(build_init_load_url).body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_chat(room_id, last_chat_id)
|
25
|
+
JSON.parse(@mechanize.get(build_load_chat_url(room_id, last_chat_id)).body)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def prepare
|
30
|
+
login
|
31
|
+
parse_script_tag
|
32
|
+
raise NotEnoughParameterError, 'Parameter is not enough' unless verify?
|
33
|
+
end
|
34
|
+
|
35
|
+
def login
|
36
|
+
@mechanize.get(CHATWORK_URL)
|
37
|
+
@mechanize.page.form_with(name: 'login') do |form|
|
38
|
+
form.field_with(name: 'email').value = @chatwork_email
|
39
|
+
form.field_with(name: 'password').value = @chatwork_pass
|
40
|
+
form.click_button
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse_script_tag
|
45
|
+
find_const = false
|
46
|
+
find_js_file = false
|
47
|
+
@mechanize.page.search('script').each do |element|
|
48
|
+
if element.children.present?
|
49
|
+
unless find_const
|
50
|
+
CONSTANTS_REGEXP.each do |key, regexp|
|
51
|
+
element.inner_text =~ regexp
|
52
|
+
break if $&.nil?
|
53
|
+
@config[key] = $3
|
54
|
+
find_const = true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
unless find_js_file
|
59
|
+
chatwork_all_ja_path = element.values.find { |item| item.include?(CHATWORK_ALL_JA) }
|
60
|
+
if chatwork_all_ja_path.present?
|
61
|
+
Net::HTTP.get(URI.parse("#{CHATWORK_URL}/#{chatwork_all_ja_path.sub(/^(\.?)\//, '')}")) =~ /a.api_version=(\d);/
|
62
|
+
if $&.present?
|
63
|
+
@config[:_av] = $1
|
64
|
+
find_js_file = true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
break if find_const && find_js_file
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def verify?
|
74
|
+
%I( myid ln _t _v _av ).all? { |item| @config[item].present? }
|
75
|
+
end
|
76
|
+
|
77
|
+
def build_init_load_url
|
78
|
+
uri = URI.parse(CHATWORK_URL)
|
79
|
+
uri.path = '/gateway.php'
|
80
|
+
uri.query = {
|
81
|
+
cmd: 'init_load',
|
82
|
+
rid: 0,
|
83
|
+
type: nil,
|
84
|
+
new: 1,
|
85
|
+
}.merge(@config).to_query
|
86
|
+
uri.to_s
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_load_chat_url(room_id, last_chat_id)
|
90
|
+
uri = URI.parse(CHATWORK_URL)
|
91
|
+
uri.path = '/gateway.php'
|
92
|
+
uri.query = {
|
93
|
+
cmd: 'load_chat',
|
94
|
+
room_id: room_id,
|
95
|
+
last_chat_id: last_chat_id,
|
96
|
+
}.merge(@config).to_query
|
97
|
+
uri.to_s
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class NotEnoughParameterError < StandardError; end;
|
102
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ChatworkTo
|
2
|
+
class Config
|
3
|
+
attr_reader :chatwork_email, :chatwork_pass, :rooms, :notifiers
|
4
|
+
|
5
|
+
def initialize(opts)
|
6
|
+
require_options!(opts)
|
7
|
+
@chatwork_email = opts['chatwork']['email']
|
8
|
+
@chatwork_pass = opts['chatwork']['pass']
|
9
|
+
@rooms = Array[*opts['chatwork']['rooms']].map(&:to_s)
|
10
|
+
@notifiers = Array[*opts['notifiers']]
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def require_options!(opts)
|
15
|
+
raise InvalideConfiguration, 'Require configureation: chatwork' if opts['chatwork'].blank?
|
16
|
+
raise InvalideConfiguration, 'Require configureation: chatwork.email' if opts['chatwork']['email'].blank?
|
17
|
+
raise InvalideConfiguration, 'Require configureation: chatwork.pass' if opts['chatwork']['pass'].blank?
|
18
|
+
raise InvalideConfiguration, 'Require configureation: chatwork.rooms' if opts['chatwork']['rooms'].blank?
|
19
|
+
raise InvalideConfiguration, 'Require configureation: notifiers' if opts['notifiers'].blank?
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def load(yaml = nil)
|
24
|
+
conf_hash = load_yaml(yaml)
|
25
|
+
new(conf_hash) if conf_hash.present?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def default_confs
|
30
|
+
%w( ./ ~/ ).map{ |dir| dir.concat('chatwork_to.yml') }
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_yaml(yaml = nil)
|
34
|
+
default_confs.unshift(yaml).compact.each do |yml|
|
35
|
+
config = YAML.load_file(yml) rescue nil
|
36
|
+
return config unless config.nil?
|
37
|
+
end
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class InvalideConfiguration < StandardError; end;
|
44
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/notifiers/*.rb"].each do |path|
|
2
|
+
require path
|
3
|
+
end
|
4
|
+
|
5
|
+
module ChatworkTo
|
6
|
+
class Notifier
|
7
|
+
def initialize(notifiers)
|
8
|
+
@notifiers = []
|
9
|
+
notifiers.each do |notifier|
|
10
|
+
name = notifier.delete('name')
|
11
|
+
@notifiers << "ChatworkTo::Notifiers::#{name.classify}".constantize.new(notifier)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def notify(hash)
|
16
|
+
@notifiers.each { |n| n.notify(hash) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def info(message)
|
20
|
+
@notifiers.each { |n| n.info(message) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ChatworkTo
|
2
|
+
module Notifiers
|
3
|
+
class Simple
|
4
|
+
def initialize(opts = {})
|
5
|
+
opts = {'io' => $stdout, 'rotation' => 'weekly'}.merge(opts)
|
6
|
+
if opts['io'].is_a?(String)
|
7
|
+
opts['io'] = File.expand_path(opts['io'])
|
8
|
+
FileUtils.mkdir_p(File.dirname(opts['io']))
|
9
|
+
File.open(opts['io'], 'a')
|
10
|
+
end
|
11
|
+
@logger = Logger.new(opts['io'], opts['rotation'])
|
12
|
+
@logger.level = Logger::DEBUG
|
13
|
+
end
|
14
|
+
|
15
|
+
def notify(hash)
|
16
|
+
@logger.debug({chat_list: hash['chat_list'], room: hash['room']}.inspect)
|
17
|
+
end
|
18
|
+
|
19
|
+
def info(message)
|
20
|
+
@logger.info(message)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module ChatworkTo
|
2
|
+
module Notifiers
|
3
|
+
class Slack
|
4
|
+
def initialize(opts = {})
|
5
|
+
@debug = !!opts.delete('debug')
|
6
|
+
build_uri
|
7
|
+
build_default_query(opts)
|
8
|
+
end
|
9
|
+
|
10
|
+
def notify(hash)
|
11
|
+
hash['chat_list'].each do |chat|
|
12
|
+
text, attachment = request_body(chat, hash['room'], hash['users'])
|
13
|
+
exec_notify(post_request(text: text, attachment: attachment))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def info(message)
|
18
|
+
exec_notify(post_request(text: decorate(message)))
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def build_uri
|
23
|
+
@uri = URI.parse('https://slack.com/api/chat.postMessage')
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_default_query(opts)
|
27
|
+
@default_query = {username: 'ChatworkTo', token: opts['token'], channel: opts['channel'], parse: 'full'}
|
28
|
+
end
|
29
|
+
|
30
|
+
def request_body(chat, room, users)
|
31
|
+
fid = chat['aid'].to_s
|
32
|
+
fname = users[fid]['name']
|
33
|
+
rid = room['id']
|
34
|
+
rname = room['name'].presence || fname
|
35
|
+
msg = chat['msg']
|
36
|
+
text = "Message from #{fname} (id:#{fid})"
|
37
|
+
attachment = {}
|
38
|
+
attachment[:fields] = [
|
39
|
+
{
|
40
|
+
fallback: 'chatwork message',
|
41
|
+
title: "#{rname} (rid:#{rid})",
|
42
|
+
value: decorate(msg),
|
43
|
+
short: false,
|
44
|
+
}
|
45
|
+
]
|
46
|
+
[text, attachment]
|
47
|
+
end
|
48
|
+
|
49
|
+
def post_request(hash)
|
50
|
+
req = Net::HTTP::Post.new(@uri.path)
|
51
|
+
req.body = @default_query.merge({text: hash[:text], attachments: [hash[:attachment]].to_json}).to_query
|
52
|
+
req
|
53
|
+
end
|
54
|
+
|
55
|
+
def exec_notify(req)
|
56
|
+
https = Net::HTTP.new(@uri.hostname, @uri.port)
|
57
|
+
https.use_ssl = true
|
58
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
59
|
+
https.set_debug_output($stdout) if @debug
|
60
|
+
res = https.start { |h| h.request(req) }
|
61
|
+
$stdout.puts(res.body) if @debug
|
62
|
+
end
|
63
|
+
|
64
|
+
def decorate(text)
|
65
|
+
text
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ChatworkTo
|
2
|
+
class Process
|
3
|
+
CONTINUOUS_ERROR_LIMIT = 10
|
4
|
+
def initialize(yaml = nil)
|
5
|
+
@yaml = yaml
|
6
|
+
prepare
|
7
|
+
end
|
8
|
+
|
9
|
+
def run
|
10
|
+
info('Info: ChatworkTo start.')
|
11
|
+
loop do
|
12
|
+
@rooms.each do |rid, data|
|
13
|
+
@response.store(rid, @client.load_chat(rid, @response.last_chat_id(rid)))
|
14
|
+
if @response.success?(rid)
|
15
|
+
if @response.notify?(rid)
|
16
|
+
notify(
|
17
|
+
{
|
18
|
+
'chat_list' => @response.chat_list(rid),
|
19
|
+
'room' => {
|
20
|
+
'id' => rid,
|
21
|
+
'name' => data['n']
|
22
|
+
},
|
23
|
+
'users' => @all_users
|
24
|
+
}
|
25
|
+
)
|
26
|
+
end
|
27
|
+
@error_count = 0
|
28
|
+
else
|
29
|
+
init_client
|
30
|
+
info('Warn: Restart client')
|
31
|
+
@error_count += 1
|
32
|
+
break
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
sleep @interval
|
37
|
+
if @error_count > CONTINUOUS_ERROR_LIMIT
|
38
|
+
info("Error: Exit caused by #{CONTINUOUS_ERROR_LIMIT} continuous error has occurred.")
|
39
|
+
break
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def init_config
|
46
|
+
@config = Config.load(@yaml)
|
47
|
+
end
|
48
|
+
|
49
|
+
def init_client
|
50
|
+
@client = Client.new(@config.chatwork_email, @config.chatwork_pass)
|
51
|
+
end
|
52
|
+
|
53
|
+
def init_response
|
54
|
+
@response = Response.new(@config.rooms)
|
55
|
+
end
|
56
|
+
|
57
|
+
def init_notifier
|
58
|
+
@notifier = Notifier.new(@config.notifiers)
|
59
|
+
end
|
60
|
+
|
61
|
+
def prepare
|
62
|
+
init_config
|
63
|
+
init_client
|
64
|
+
init_response
|
65
|
+
init_notifier
|
66
|
+
prepare_vars
|
67
|
+
end
|
68
|
+
|
69
|
+
def prepare_vars
|
70
|
+
@interval = 1.minutes
|
71
|
+
@error_count = 0
|
72
|
+
data = @client.init_load
|
73
|
+
@all_users = data['result']['contact_dat']
|
74
|
+
rooms = data['result']['room_dat']
|
75
|
+
@rooms = {}
|
76
|
+
@config.rooms.each { |id| @rooms[id] = rooms[id] }
|
77
|
+
end
|
78
|
+
|
79
|
+
def notify(hash)
|
80
|
+
@notifier.notify(hash)
|
81
|
+
end
|
82
|
+
|
83
|
+
def info(message)
|
84
|
+
@notifier.info(message)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ChatworkTo
|
2
|
+
class Response
|
3
|
+
def initialize(rooms)
|
4
|
+
@responses = {}
|
5
|
+
rooms.each do |rid|
|
6
|
+
@responses[rid] = default_response
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def store(room_id, response)
|
11
|
+
old = @responses[room_id]
|
12
|
+
|
13
|
+
current = default_response.merge({ 'success' => response['status']['success'] })
|
14
|
+
if current['success']
|
15
|
+
current['chat_list'] = response['result']['chat_list']
|
16
|
+
if old['last_chat_id'].eql?(0)
|
17
|
+
# init_load
|
18
|
+
if current['chat_list'].present?
|
19
|
+
current['last_chat_id'] = current['chat_list'].last.fetch('id')
|
20
|
+
end
|
21
|
+
else
|
22
|
+
if current['chat_list'].present?
|
23
|
+
# updated
|
24
|
+
current['last_chat_id'] = current['chat_list'].last.fetch('id')
|
25
|
+
current['notify'] = true
|
26
|
+
else
|
27
|
+
# not updated
|
28
|
+
current['last_chat_id'] = old['last_chat_id']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
else
|
32
|
+
current['error_message'] = response['status']['message']
|
33
|
+
end
|
34
|
+
|
35
|
+
@responses[room_id] = current
|
36
|
+
end
|
37
|
+
|
38
|
+
def success?(room_id)
|
39
|
+
@responses[room_id]['success']
|
40
|
+
end
|
41
|
+
|
42
|
+
def notify?(room_id)
|
43
|
+
@responses[room_id]['notify']
|
44
|
+
end
|
45
|
+
|
46
|
+
def last_chat_id(room_id)
|
47
|
+
@responses[room_id]['last_chat_id']
|
48
|
+
end
|
49
|
+
|
50
|
+
def chat_list(room_id)
|
51
|
+
@responses[room_id]['chat_list']
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def default_response
|
56
|
+
{
|
57
|
+
'success' => false,
|
58
|
+
'notify' => false,
|
59
|
+
'last_chat_id' => 0,
|
60
|
+
}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/chatwork_to.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'active_support/inflector'
|
4
|
+
require 'mechanize'
|
5
|
+
|
6
|
+
require 'chatwork_to/version'
|
7
|
+
require 'chatwork_to/config'
|
8
|
+
require 'chatwork_to/client'
|
9
|
+
require 'chatwork_to/notifier'
|
10
|
+
require 'chatwork_to/response'
|
11
|
+
require 'chatwork_to/process'
|
12
|
+
|
13
|
+
module ChatworkTo
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chatwork_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nalabjp
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mechanize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.7.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.7.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: pry
|
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: pry-doc
|
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: pry-stack_explorer
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
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: pry-rescue
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: awesome_print
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: ChatWorkTo can transfer ChatWork messages via notifier.
|
154
|
+
email:
|
155
|
+
- nalabjp@gmail.com
|
156
|
+
executables:
|
157
|
+
- chatwork_to
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".travis.yml"
|
163
|
+
- Gemfile
|
164
|
+
- LICENSE.txt
|
165
|
+
- README.md
|
166
|
+
- Rakefile
|
167
|
+
- bin/chatwork_to
|
168
|
+
- chatwork_to.gemspec
|
169
|
+
- chatwork_to.yml.example
|
170
|
+
- lib/chatwork_to.rb
|
171
|
+
- lib/chatwork_to/client.rb
|
172
|
+
- lib/chatwork_to/config.rb
|
173
|
+
- lib/chatwork_to/notifier.rb
|
174
|
+
- lib/chatwork_to/notifiers/simple.rb
|
175
|
+
- lib/chatwork_to/notifiers/slack.rb
|
176
|
+
- lib/chatwork_to/process.rb
|
177
|
+
- lib/chatwork_to/response.rb
|
178
|
+
- lib/chatwork_to/version.rb
|
179
|
+
homepage: https://github.com/nalabjp/chatwork_to
|
180
|
+
licenses:
|
181
|
+
- MIT
|
182
|
+
metadata: {}
|
183
|
+
post_install_message:
|
184
|
+
rdoc_options: []
|
185
|
+
require_paths:
|
186
|
+
- lib
|
187
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
188
|
+
requirements:
|
189
|
+
- - ">="
|
190
|
+
- !ruby/object:Gem::Version
|
191
|
+
version: '0'
|
192
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
193
|
+
requirements:
|
194
|
+
- - ">="
|
195
|
+
- !ruby/object:Gem::Version
|
196
|
+
version: '0'
|
197
|
+
requirements: []
|
198
|
+
rubyforge_project:
|
199
|
+
rubygems_version: 2.2.2
|
200
|
+
signing_key:
|
201
|
+
specification_version: 4
|
202
|
+
summary: Transfer ChatWork messages.
|
203
|
+
test_files: []
|
204
|
+
has_rdoc:
|