twing 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: 396287e147546985d9d366d2b59969ec4c20e0d2
4
+ data.tar.gz: 8a606152540372504227b51c3c03101c5e3e58c9
5
+ SHA512:
6
+ metadata.gz: de1c5efb57719a6651eca83fe932331e33d487f330b9a812f2878597f46798686cf7122674cc0590915cb9fe75dcc59d9d908be4b76a3f82e309552cb26415e7
7
+ data.tar.gz: fd5330e7d68991815c1457b79724b5d39a91565f72528cb5008e2777bc2fd891b2fdeaa104fd99f035e501f2c4cbb26930ef6fa66de60c9bfd71efe1b20c7e20
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor
10
+ /test
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at flum.1025@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in twing.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twing (0.1.0)
5
+ hashie
6
+ redis-namespace
7
+ redis-objects
8
+ twitter
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.5.2)
14
+ public_suffix (>= 2.0.2, < 4.0)
15
+ buftok (0.2.0)
16
+ coderay (1.1.2)
17
+ domain_name (0.5.20180417)
18
+ unf (>= 0.0.5, < 1.0.0)
19
+ equalizer (0.0.11)
20
+ hashie (3.6.0)
21
+ http (3.3.0)
22
+ addressable (~> 2.3)
23
+ http-cookie (~> 1.0)
24
+ http-form_data (~> 2.0)
25
+ http_parser.rb (~> 0.6.0)
26
+ http-cookie (1.0.3)
27
+ domain_name (~> 0.5)
28
+ http-form_data (2.1.1)
29
+ http_parser.rb (0.6.0)
30
+ memoizable (0.4.2)
31
+ thread_safe (~> 0.3, >= 0.3.1)
32
+ method_source (0.9.0)
33
+ multipart-post (2.0.0)
34
+ naught (1.1.0)
35
+ pry (0.11.3)
36
+ coderay (~> 1.1.0)
37
+ method_source (~> 0.9.0)
38
+ public_suffix (3.0.3)
39
+ rake (10.5.0)
40
+ redis (4.0.2)
41
+ redis-namespace (1.6.0)
42
+ redis (>= 3.0.4)
43
+ redis-objects (1.4.0)
44
+ redis (~> 4.0)
45
+ simple_oauth (0.3.1)
46
+ thread_safe (0.3.6)
47
+ twitter (6.2.0)
48
+ addressable (~> 2.3)
49
+ buftok (~> 0.2.0)
50
+ equalizer (~> 0.0.11)
51
+ http (~> 3.0)
52
+ http-form_data (~> 2.0)
53
+ http_parser.rb (~> 0.6.0)
54
+ memoizable (~> 0.4.0)
55
+ multipart-post (~> 2.0)
56
+ naught (~> 1.0)
57
+ simple_oauth (~> 0.3.0)
58
+ unf (0.1.4)
59
+ unf_ext
60
+ unf_ext (0.0.7.5)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler (~> 1.16)
67
+ pry
68
+ rake (~> 10.0)
69
+ twing!
70
+
71
+ BUNDLED WITH
72
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 flum1025
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,118 @@
1
+ # Twing
2
+
3
+ Pluginable Twitter Application
4
+
5
+ ## Installation
6
+
7
+ $ gem install twing
8
+
9
+ ## Usage
10
+
11
+ $ twing -s setting.yml
12
+
13
+ ```shell
14
+ $ bundle exec twing -s ./setting.yml --help
15
+ Usage: twing [options]
16
+ -h, --help help
17
+ -s, --setting VALUES setting file path
18
+ -d, --debug debug mode
19
+ --log-dir VALUES log dir
20
+ --home-timeline start streamer with home_timeline
21
+ --filter start streamer with filter
22
+ --user start streamer with user
23
+ --worker start worker
24
+ --standalone standalone mode
25
+ -p, --pouring VALUES direct pouring tweet
26
+ ```
27
+
28
+ ### Configuration File
29
+
30
+ Configuration file is YAML format file.
31
+
32
+ Example
33
+
34
+ ```yaml
35
+ debug: false
36
+ log_dir: /tmp/log
37
+ mode: home_timeline
38
+ twitter:
39
+ api_key:
40
+ consumer_key: CONSUMER_KEY
41
+ consumer_secret: CONSUMER_SECRET
42
+ access_token: ACCESS_TOKEN
43
+ access_token_secret: ACCESS_TOKEN_SECRET
44
+ home_timeline_options:
45
+ query:
46
+ timeline_count: 200
47
+ exclude_replies: false
48
+ interval: 60
49
+ filter_options:
50
+ follow: '111111111,222222222'
51
+ redis:
52
+ config:
53
+ host: localhost
54
+ port: 6379
55
+ namespace: hoge
56
+ require:
57
+ - require_module_name
58
+ modules:
59
+ module_name:
60
+ module_setting: hoge
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ 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).
68
+
69
+ ### Make a new plugin
70
+
71
+ There are two ways.
72
+
73
+ - Single file
74
+ - gem
75
+
76
+ #### Single file
77
+
78
+ Example
79
+
80
+ ```ruby:test_module.rb
81
+ require 'twing/modules/base'
82
+
83
+ class NewPlugin < Twing::Modules::Base
84
+ def on_message(object)
85
+ case object
86
+ when Twitter::Tweet
87
+ puts object.text
88
+ end
89
+ end
90
+ end
91
+
92
+ Twing.on_init do |app|
93
+ app.receivers.add(NewPlugin)
94
+ end
95
+ ```
96
+
97
+ Add values to the following section of the configuration file.
98
+
99
+ ```yaml
100
+ require:
101
+ - path_to_test_module.rb
102
+ ```
103
+
104
+ #### gem
105
+
106
+ REF: [twing_earch](https://github.com/flum1025/twing_earch)
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/flum1025/twing. 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.
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
115
+
116
+ ## Code of Conduct
117
+
118
+ Everyone interacting in the Twing project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/flum1025/twing/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twing"
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
data/exe/twing ADDED
@@ -0,0 +1,3 @@
1
+ require 'twing'
2
+
3
+ Twing.new.start
data/lib/twing/cli.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'hashie'
2
+ require 'twing/initializer'
3
+
4
+ class Twing
5
+ class Cli
6
+ SettingFileNotFound = Class.new(StandardError)
7
+
8
+ attr_reader :initializer
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ @setting_loader = Initializer.new(true)
13
+ @initializer = Initializer.new
14
+ @setting = {}
15
+ regist_setting_loader
16
+ regist_initializer
17
+ end
18
+
19
+ def parse
20
+ argv = @setting_loader.init(ARGV)
21
+
22
+ raise SettingFileNotFound if @setting.size.zero?
23
+
24
+ load_plugins
25
+ @app.init_modules
26
+ @initializer.init(argv)
27
+
28
+ cli_options = @initializer.options
29
+ setting = Hashie::Mash.new @setting.merge(cli_options)
30
+ options = @setting.reduce({}) do |acc, (k, v)|
31
+ key = k.to_sym
32
+ acc[key] = v unless cli_options.keys.include?(key)
33
+ acc
34
+ end
35
+ @initializer.call(options, setting)
36
+
37
+ setting
38
+ end
39
+
40
+ private
41
+
42
+ def load_plugins
43
+ if @setting&.require.is_a?(Array)
44
+ @setting.require.each { |file| require file }
45
+ end
46
+ end
47
+
48
+ def regist_setting_loader
49
+ @setting_loader.add(:help, '-h', '--help', 'help') do
50
+ puts @setting_loader.optparse.help
51
+ puts @initializer.optparse.help.each_line.drop(1).join
52
+ exit
53
+ end
54
+ @setting_loader.add(:setting, '-s VALUES', '--setting VALUES', 'setting file path') do |file|
55
+ @setting = Hashie::Mash.load(file)
56
+ end
57
+ end
58
+
59
+ def regist_initializer
60
+ @initializer.add(:debug, '-d', '--debug', 'debug mode')
61
+ @initializer.add(:log_dir, '--log-dir VALUES', 'log dir')
62
+ @initializer.add(:home_timeline, '--home-timeline', 'start streamer with home_timeline')
63
+ @initializer.add(:filter, '--filter', 'start streamer with filter')
64
+ @initializer.add(:user, '--user', 'start streamer with user')
65
+ @initializer.add(:worker, '--worker', 'start worker')
66
+ @initializer.add(:standalone, '--standalone', 'standalone mode')
67
+ @initializer.add(:pouring, '-p', '--pouring VALUES', 'direct pouring tweet') do |v|
68
+ Twing.after_init do |app|
69
+ app.pouring(v)
70
+ exit
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,24 @@
1
+ class Twing
2
+ class Cursor
3
+ def initialize(key, use_redis = true)
4
+ @key = key
5
+ @use_redis = use_redis
6
+
7
+ if @use_redis
8
+ @redis = Redis.current
9
+ else
10
+ @cursor = nil
11
+ end
12
+ end
13
+
14
+ def get
15
+ return @redis.get(@key) if @use_redis
16
+ @cursor
17
+ end
18
+
19
+ def set(value)
20
+ return @redis.set(@key, value) if @use_redis
21
+ @cursor = value
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ require 'optparse'
2
+
3
+ class Twing
4
+ class Initializer
5
+ attr_reader :optparse, :options
6
+
7
+ def initialize(pre = false)
8
+ @pre = pre
9
+ @optparse = OptionParser.new
10
+ @optparse.version = Twing::VERSION
11
+ @options = {}
12
+ @callbacks = {}
13
+ end
14
+
15
+ def add(key, *args, &block)
16
+ @optparse.on(*args) { |v| @options[key] = v }
17
+ @callbacks[key] = block if block
18
+ end
19
+
20
+ def init(argv = ARGV, left = [])
21
+ @optparse.parse(argv - left)
22
+ call(@options)
23
+ left
24
+ rescue OptionParser::InvalidOption => e
25
+ raise e unless @pre
26
+ e.args.each do |arg|
27
+ next_item = argv[argv.index(arg) + 1]
28
+ left += e.args
29
+ left << next_item if !next_item.nil? && !next_item.start_with?(?-)
30
+ end
31
+ init(argv, left)
32
+ end
33
+
34
+ def call(options, root_options = options)
35
+ options.each do |key, value|
36
+ @callbacks[key].call(value, root_options) if @callbacks[key]
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ require 'twing/utility/logger'
2
+
3
+ class Twing
4
+ module Modules
5
+ class Base
6
+ attr_reader :logger, :root_setting, :setting, :app
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ @logger = CustomLogger.new(app.logger, self.class.to_s)
11
+ @root_setting = app.setting
12
+ @setting = app.setting.modules[self.class.to_s.downcase]
13
+ end
14
+
15
+ def on_message(*args)
16
+ raise NotImplementedError.new("You must implement #{self.class}##{__method__}")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ require 'twing/utility/callback'
2
+
3
+ class Twing
4
+ module Modules
5
+ module Extend
6
+ @@callbacks = Callback.new
7
+
8
+ def callbacks
9
+ @@callbacks
10
+ end
11
+
12
+ def on_init(&block)
13
+ self.callbacks.add(:init, &block)
14
+ end
15
+
16
+ def after_init(&block)
17
+ self.callbacks.add(:after_init, &block)
18
+ end
19
+ end
20
+
21
+ def self.included(klass)
22
+ klass.extend(Extend)
23
+ end
24
+
25
+ def init_modules
26
+ self.class.callbacks.run(:init) do |callback|
27
+ callback.call(self)
28
+ end
29
+ end
30
+
31
+ def after_init
32
+ self.class.callbacks.run(:after_init) do |callback|
33
+ callback.call(self)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,63 @@
1
+ class Twing
2
+ module OperatingMode
3
+ private
4
+
5
+ def mode
6
+ return :home_timeline if setting.home_timeline
7
+ return :filter if setting.filter
8
+ return :user if setting.user
9
+ return :worker if setting.worker
10
+ end
11
+
12
+ def home_timeline
13
+ options = setting.twitter.home_timeline_options.query.to_h
14
+ options.merge!(count: options['timeline_count'])
15
+
16
+ loop do
17
+ logger.info('load new timeline')
18
+
19
+ since_id = @cursor.get
20
+
21
+ timeline = rest_client.home_timeline(
22
+ since_id ? options.merge(since_id: since_id) : options
23
+ ).each { |object| publish(object) }
24
+
25
+ @cursor.set(timeline.first.id) unless timeline.first.nil?
26
+
27
+ sleep setting.twitter.home_timeline_options.interval
28
+ end
29
+ rescue Twitter::Error::TooManyRequests => e
30
+ sleep e.rate_limit.reset_in
31
+ retry
32
+ rescue Twitter::Error::ServerError, EOFError, Errno::EPIPE
33
+ sleep 1
34
+ retry
35
+ end
36
+
37
+ def worker
38
+ @queue.process do |msg|
39
+ logger.debug("Message subscribe #{msg}")
40
+ object = msg2object(msg)
41
+ delivery(object)
42
+ end
43
+ end
44
+
45
+ def msg2object(msg)
46
+ data = JSON.parse(msg)
47
+ klass = Object.const_get(data['class'])
48
+ klass.new(JSON.parse(data['body'], { symbolize_names: true }))
49
+ end
50
+
51
+ def user
52
+ stream_client.user(setting.twitter.user_options || {}) do |object|
53
+ publish(object)
54
+ end
55
+ end
56
+
57
+ def filter
58
+ stream_client.filter(setting.twitter.filter_options || {}) do |object|
59
+ publish(object)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,19 @@
1
+ class Twing
2
+ class Queue
3
+ def initialize(name, namespace)
4
+ @name = name
5
+ @redis = Redis.current
6
+ end
7
+
8
+ def push(data)
9
+ @redis.rpush(@name, data)
10
+ end
11
+
12
+ def process
13
+ loop do
14
+ key, msg = @redis.blpop(@name, 0)
15
+ yield msg if msg
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ class Twing
2
+ class Receivers
3
+ attr_reader :receivers, :instances
4
+
5
+ def initialize
6
+ @receivers, @instances = [], []
7
+ end
8
+
9
+ def add(receiver)
10
+ @receivers << receiver
11
+ end
12
+
13
+ def init(app)
14
+ @instances = @receivers.map do |receiver|
15
+ receiver.new(app)
16
+ end
17
+ end
18
+
19
+ def run(&block)
20
+ @instances.each(&block)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ class Callback
2
+ attr_reader :callbacks
3
+
4
+ def initialize
5
+ @callbacks = Hash.new{|hash, key| hash[key] = []}
6
+ end
7
+
8
+ def add(type, &block)
9
+ @callbacks[type] << block
10
+ end
11
+
12
+ def run(type)
13
+ @callbacks[type].each do |callback|
14
+ yield callback
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ class Twing
2
+ module Modules
3
+ class CustomLogger
4
+ attr_reader :logger
5
+
6
+ def initialize(logger, prefix)
7
+ @logger = logger
8
+ @prefix = prefix
9
+ end
10
+
11
+ [:fatal, :error, :warn, :info, :debug].each do |method|
12
+ define_method method do |message|
13
+ @logger.send(method, @prefix) { message }
14
+ end
15
+ end
16
+
17
+ def method_missing(method, *args)
18
+ @logger.send(method, *args)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class Twing
2
+ VERSION = "0.1.0"
3
+ end
data/lib/twing.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'logger'
2
+ require 'twitter'
3
+ require 'redis-objects'
4
+ require 'redis-namespace'
5
+ require 'twing/version'
6
+ require 'twing/modules'
7
+ require 'twing/receivers'
8
+ require 'twing/cli'
9
+ require 'twing/operating_mode'
10
+ require 'twing/queue'
11
+ require 'twing/cursor'
12
+
13
+ class Twing
14
+ include Modules
15
+ include OperatingMode
16
+
17
+ LOGGER_FORMAT = '%Y-%m-%d %H:%M:%S.%L '
18
+ REDIS_KEY = 'home_timeline_streamer'
19
+
20
+ attr_accessor :logger
21
+ attr_reader :receivers, :cli, :setting, :rest_client, :stream_client
22
+
23
+ def initialize
24
+ @receivers = Receivers.new
25
+ @cli = Cli.new(self)
26
+ @setting = @cli.parse
27
+ @logger = generate_logger
28
+ @receivers.init(self)
29
+
30
+ @logger.debug("load plugins: #{@receivers.receivers}")
31
+
32
+ @rest_client = Twitter::REST::Client.new(setting.twitter.api_key)
33
+ @stream_client = Twitter::Streaming::Client.new(setting.twitter.api_key)
34
+
35
+ unless setting.standalone
36
+ Redis.current = Redis::Namespace.new(
37
+ setting.redis.namespace,
38
+ :redis => Redis.new(setting.redis.config)
39
+ )
40
+ @redis = Redis.current
41
+ @queue = Queue.new('queue', setting.redis.namespace)
42
+ end
43
+
44
+ @cursor = Cursor.new(REDIS_KEY, !setting.standalone)
45
+
46
+ after_init
47
+ end
48
+
49
+ def start
50
+ raise ArgumentError, 'mode is empty' if mode.nil?
51
+ logger.info("start mode=#{mode} standalone=#{setting.standalone}")
52
+ send(mode)
53
+ rescue Interrupt, SignalException
54
+ # do nothing
55
+ rescue Exception => ex
56
+ backtrace = ex.backtrace.dup
57
+ logger.error(<<~EOF)
58
+ #{backtrace.shift}: #{ex.message} (#{ex.class})
59
+ #{backtrace.join("\n")}
60
+ EOF
61
+ end
62
+
63
+ def pouring(tweet_id)
64
+ obj = rest_client.status(tweet_id)
65
+ delivery(obj)
66
+ end
67
+
68
+ private
69
+
70
+ def generate_logger
71
+ logdev = setting.log_dir ? File.join(setting.log_dir, "#{mode}.log") : STDOUT
72
+ logger = Logger.new(logdev, datetime_format: LOGGER_FORMAT)
73
+ logger.level = setting.debug ? Logger::DEBUG : Logger::INFO
74
+ logger
75
+ end
76
+
77
+ def publish(data)
78
+ logger.debug("Message publish #{data}")
79
+ if setting.standalone
80
+ delivery(data)
81
+ else
82
+ body =
83
+ case data
84
+ when Twitter::Streaming::Event
85
+ {
86
+ event: object.name,
87
+ source: object.source.to_h,
88
+ target: object.target.to_h,
89
+ target_object: object.target_object.to_h
90
+ }.to_json
91
+ when Twitter::Streaming::FriendList
92
+ data.to_json
93
+ else
94
+ data.to_h.to_json
95
+ end
96
+
97
+ @queue.push({
98
+ class: data.class.to_s,
99
+ body: body
100
+ }.to_json)
101
+ end
102
+ end
103
+
104
+ def delivery(data)
105
+ logger.debug("Message delivery #{data}")
106
+ @receivers.run do |receiver|
107
+ receiver.on_message(data)
108
+ end
109
+ end
110
+ end
data/twing.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'twing'
8
+ spec.version = Twing::VERSION
9
+ spec.authors = ['flum1025']
10
+ spec.email = ['flum.1025@gmail.com']
11
+ spec.summary = ''
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/flum1025/twing'
14
+ spec.license = 'MIT'
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
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.16'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_dependency 'twitter'
26
+ spec.add_dependency 'hashie'
27
+ spec.add_dependency 'redis-objects'
28
+ spec.add_dependency 'redis-namespace'
29
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - flum1025
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-19 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: pry
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: twitter
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: hashie
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
+ - !ruby/object:Gem::Dependency
84
+ name: redis-objects
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
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: redis-namespace
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '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'
111
+ description: ''
112
+ email:
113
+ - flum.1025@gmail.com
114
+ executables:
115
+ - twing
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - exe/twing
129
+ - lib/twing.rb
130
+ - lib/twing/cli.rb
131
+ - lib/twing/cursor.rb
132
+ - lib/twing/initializer.rb
133
+ - lib/twing/modules.rb
134
+ - lib/twing/modules/base.rb
135
+ - lib/twing/operating_mode.rb
136
+ - lib/twing/queue.rb
137
+ - lib/twing/receivers.rb
138
+ - lib/twing/utility/callback.rb
139
+ - lib/twing/utility/logger.rb
140
+ - lib/twing/version.rb
141
+ - twing.gemspec
142
+ homepage: https://github.com/flum1025/twing
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.6.8
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: ''
166
+ test_files: []