pleiades 0.1.1 → 0.1.2

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -2
  3. data/README.md +23 -0
  4. data/Rakefile +6 -0
  5. data/lib/pleiades/core.rb +1 -0
  6. data/lib/pleiades/core/client.rb +52 -0
  7. data/lib/pleiades/core/client/wrapper.rb +55 -0
  8. data/lib/pleiades/core/command.rb +11 -7
  9. data/lib/pleiades/core/command/base_command.rb +1 -16
  10. data/lib/pleiades/core/command/executor.rb +16 -0
  11. data/lib/pleiades/core/command/factory.rb +50 -0
  12. data/lib/pleiades/core/command/router.rb +28 -27
  13. data/lib/pleiades/core/command/routing/event_judge_methods.rb +51 -0
  14. data/lib/pleiades/core/command/routing/event_proccessor.rb +122 -0
  15. data/lib/pleiades/core/command/routing/nest_blocks.rb +238 -0
  16. data/lib/pleiades/core/command/routing/path_builder.rb +18 -0
  17. data/lib/pleiades/core/command/routing/reflection.rb +13 -0
  18. data/lib/pleiades/core/command/routing/result.rb +47 -0
  19. data/lib/pleiades/core/command/routing/route_refine.rb +49 -0
  20. data/lib/pleiades/core/command/routing/validator.rb +72 -0
  21. data/lib/pleiades/core/command/routing_proxy.rb +126 -0
  22. data/lib/pleiades/core/config.rb +24 -17
  23. data/lib/pleiades/core/constants.rb +10 -7
  24. data/lib/pleiades/core/util.rb +9 -7
  25. data/lib/pleiades/core_ext/line/bot/event/base.rb +16 -7
  26. data/lib/pleiades/core_ext/line/bot/event/postback.rb +6 -4
  27. data/lib/pleiades/generators/pleiades/command/command_generator.rb +58 -51
  28. data/lib/pleiades/generators/pleiades/install/install_generator.rb +11 -12
  29. data/lib/pleiades/generators/pleiades/install/templates/config.yml +11 -1
  30. data/lib/pleiades/generators/pleiades/install/templates/pleiades.rb +1 -0
  31. data/lib/pleiades/generators/pleiades/install/templates/routing_proxy.rb +13 -0
  32. data/lib/pleiades/generators/pleiades/setup/setup_generator.rb +100 -98
  33. data/lib/pleiades/generators/pleiades/setup/templates/api_controller.rb +4 -15
  34. data/lib/pleiades/generators/pleiades/setup/templates/base_command.rb +2 -6
  35. data/lib/pleiades/generators/pleiades/setup/templates/command_common.erb +2 -6
  36. data/lib/pleiades/railtie.rb +3 -11
  37. data/lib/pleiades/version.rb +1 -1
  38. data/pleiades.gemspec +1 -0
  39. metadata +32 -8
  40. data/lib/pleiades/core/.keep +0 -0
  41. data/lib/pleiades/core/command/event_proccessor.rb +0 -87
  42. data/lib/pleiades/core/command/nest_blocks.rb +0 -55
  43. data/lib/pleiades/core_ext/.keep +0 -0
@@ -5,25 +5,17 @@ module Pleiades
5
5
  class Config
6
6
  class << self
7
7
  def configration
8
- return nil if loaded?
9
-
10
8
  @config = new(load).freeze
11
- @loaded = true
12
- end
13
-
14
- def loaded?
15
- @loaded
16
9
  end
17
10
 
18
- def method_missing method, *_
19
- configration
20
- return super unless instance_methods.include?(method)
11
+ def method_missing(method, *_)
12
+ return super unless @config.respond_to?(method)
21
13
 
22
14
  @config.__send__ method
23
15
  end
24
16
 
25
- def respond_to_missing? method, _
26
- instance_methods.include?(method)
17
+ def respond_to_missing?(_mes, *_)
18
+ true
27
19
  end
28
20
 
29
21
  private
@@ -33,20 +25,35 @@ module Pleiades
33
25
  end
34
26
  end
35
27
 
36
- def commands_path
37
- @src.command.commands_path
28
+ def router_default_option
29
+ @src.router.default.symbolize_keys
38
30
  end
39
31
 
40
- def disp_console
41
- @src.debug.disp_console
32
+ def client_keys
33
+ @src
34
+ .client
35
+ .key_acquisition_process
36
+ .each_pair.map do |_, str_proc|
37
+ instance_eval(str_proc)
38
+ end
42
39
  end
43
40
 
44
41
  private
45
42
 
46
43
  attr_reader :src
47
44
 
48
- def initialize src
45
+ def initialize(src)
49
46
  @src = Pleiades::Util.define_reader src
50
47
  end
48
+
49
+ def method_missing(method, *_)
50
+ @src.respond_to?(method) || super
51
+
52
+ @src.__send__ method
53
+ end
54
+
55
+ def respond_to_missing?(method, *_)
56
+ @src.respond_to?(method)
57
+ end
51
58
  end
52
59
  end
@@ -1,19 +1,22 @@
1
1
  module Pleiades
2
2
  module Constants
3
3
  module Events
4
- TYPES = %w[
5
- text
6
- sticker
7
- postback
8
- follow
9
- unfollow
10
- ].map(&:freeze).freeze
4
+ TYPES =
5
+ %w[
6
+ text
7
+ sticker
8
+ postback
9
+ follow
10
+ unfollow
11
+ ].map(&:freeze).freeze
11
12
  end
12
13
 
13
14
  module File
14
15
  CONFIG_DIR_PATH = 'config/pleiades'.freeze
15
16
  CONFIG = "#{CONFIG_DIR_PATH}/config.yml".freeze
17
+ INITIALIZER = 'config/initializers/pleiades.rb'.freeze
16
18
  ROUTER = "#{CONFIG_DIR_PATH}/router.rb".freeze
19
+ ROUTING_PROXY = "#{CONFIG_DIR_PATH}/routing_proxy.rb".freeze
17
20
  end
18
21
  end
19
22
  end
@@ -1,17 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pleiades
4
-
5
4
  # Util Modules.
6
5
  module Util
7
6
  class << self
8
7
  def define_reader(hash)
9
- Struct.new(
10
- *hash.keys.map { |key| key.underscore.to_sym }
11
- ).new(
12
- *hash.values.map { |s| Hash === s ? define_reader(s) : s.freeze }
13
- .map(&:freeze)
14
- )
8
+ hash.instance_eval do
9
+ hash.each_pair do |key, val|
10
+ l_val = val.is_a?(Hash) ? Pleiades::Util.define_reader(val) : val
11
+ define_singleton_method(key.to_s.underscore.to_sym) do
12
+ l_val
13
+ end
14
+ end
15
+ end
16
+ hash
15
17
  end
16
18
  end
17
19
  end
@@ -1,12 +1,16 @@
1
1
  require 'pleiades/core/util'
2
+ require 'freeezer'
2
3
 
3
4
  module Line
4
5
  module Bot
5
6
  module Event
6
7
  class Base
7
- def initialize src
8
+ using Freeezer
9
+
10
+ def initialize(src)
8
11
  @src = Pleiades::Util.define_reader src
9
12
 
13
+ # moduleにする
10
14
  # /^[a-z]+_event\?$/
11
15
  # => トークタイプの判定メソッドに反応する。
12
16
  #
@@ -17,16 +21,21 @@ module Line
17
21
  #
18
22
  @src.define_singleton_method(:method_missing) do |method, *_|
19
23
  return super() unless /^[a-z]+_event\?$/ =~ method
24
+
20
25
  source.type == method.to_s.split('_').first
21
26
  end
27
+ @src.deep_freeze
22
28
  end
23
29
 
24
- def method_missing method, *_
25
- begin
26
- @src.__send__ method
27
- rescue ArgumentError => _
28
- raise NoMethodError, "#{self.class} has no `#{method}` method."
29
- end
30
+ private
31
+
32
+ def method_missing(method, *_)
33
+ @src.respond_to?(method) || super
34
+ @src.__send__ method
35
+ end
36
+
37
+ def respond_to_missing?(method, *_)
38
+ @src.respond_to?(method) || super
30
39
  end
31
40
  end
32
41
  end
@@ -4,7 +4,7 @@ module Line
4
4
  class Postback
5
5
  attr_reader :action, :params
6
6
 
7
- def initialize src
7
+ def initialize(src)
8
8
  super
9
9
  set_instance_variables
10
10
  end
@@ -16,9 +16,11 @@ module Line
16
16
  # action 'path/to/command'
17
17
  # params '{product_id: 1, order_num: 3}'
18
18
  def set_instance_variables
19
- data = postback.data.split('&')
20
- .map { |s| s.split('=') }
21
- .each_with_object({}) { |(key, val), hash| hash[key.to_sym] = val }
19
+ data =
20
+ postback.data
21
+ .split('&')
22
+ .map { |s| s.split('=') }
23
+ .each_with_object({}) { |(key, val), hash| hash[key.to_sym] = val }
22
24
 
23
25
  @action = data.delete :action
24
26
  @params = data
@@ -1,68 +1,75 @@
1
1
  require 'pleiades/core/constants'
2
2
  require 'pleiades/core/config'
3
3
 
4
- module Pleiades
5
- module Generators
6
- class CommandGenerator < Rails::Generators::NamedBase
7
- source_root File.expand_path('templates', __dir__)
4
+ class Pleiades::CommandGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path('templates', __dir__)
8
6
 
9
- argument :name, type: :string, desc: 'Specify command class name.'
7
+ argument :names, type: :array, desc: 'Specify command class name.'
10
8
 
11
- dir_options = {
12
- aliases: '-d',
13
- desc: 'Specify the directory to generate commands.',
14
- default: 'common'
15
- }
16
- class_option :dir, dir_options
9
+ dir_options =
10
+ {
11
+ aliases: '-d',
12
+ desc: 'Specify the directory to generate commands.'
13
+ }
14
+ class_option :dir, dir_options
17
15
 
18
- event_type_options = {
19
- aliases: '-t',
20
- desc: 'Specify command type.',
21
- default: :text,
22
- enum: Pleiades::Constants::Events::TYPES
23
- }
24
- class_option :event_type, event_type_options
16
+ event_type_options =
17
+ {
18
+ aliases: '-t',
19
+ desc: 'Specify command type.',
20
+ default: :text,
21
+ enum: Pleiades::Constants::Events::TYPES
22
+ }
23
+ class_option :event_type, event_type_options
25
24
 
26
- def generate_command
27
- commands_path = Pleiades::Config.commands_path
28
- template 'command.erb', "#{commands_path}/#{options['dir']}/#{name}.rb"
29
- end
25
+ def setup
26
+ @names.unshift @name
27
+ end
30
28
 
31
- def drow_route
32
- arg = [
33
- Pleiades::Constants::File::ROUTER,
34
- event_with_option,
35
- { after: /^Pleiades::Command::Router.route do/ }
36
- ]
37
- inject_into_file(*arg)
38
- end
29
+ def generate_command
30
+ commands_path = Pleiades::Config.command.commands_path
31
+ @names.each do |name|
32
+ template 'command.erb', "#{commands_path}/#{options['dir']}/#{name}.rb"
33
+ end
34
+ end
35
+
36
+ def drow_route
37
+ @names.each do |name|
38
+ arg =
39
+ [
40
+ Pleiades::Constants::File::ROUTER,
41
+ event_with_option(name),
42
+ { after: /^Pleiades::Command::Router.route do/ }
43
+ ]
44
+ inject_into_file(*arg)
45
+ end
46
+ end
39
47
 
40
- private
48
+ private
41
49
 
42
- def event_with_option
43
- option = event_specific_options
50
+ def event_with_option(name)
51
+ option = event_specific_options
44
52
 
45
- str = "\n\t#{options['event_type']}"
46
- str += " scope: '#{options['dir']}', action: '#{name}'"
47
- str += ", #{option}" if option
53
+ str = "\n #{options['event_type']}"
54
+ str += " action: '#{name}'"
55
+ str += ", scope: '#{options['dir']}'" if options['dir']
56
+ str += ", #{option}" if option
48
57
 
49
- str
50
- end
58
+ str
59
+ end
51
60
 
52
- def event_specific_options
53
- EventOption.const_get options['event_type'].capitalize
54
- rescue NameError => _
55
- nil
56
- end
61
+ def event_specific_options
62
+ EventOption.const_get options['event_type'].capitalize
63
+ rescue NameError => _e
64
+ nil
65
+ end
57
66
 
58
- def dirs
59
- options['dir'].split('/')
60
- end
67
+ def dirs
68
+ options['dir'].split('/')
69
+ end
61
70
 
62
- module EventOption
63
- Text = 'pattern: //'.freeze
64
- Sticker = 'package_id: 1, sticker_id: 1'.freeze
65
- end
66
- end
71
+ module EventOption
72
+ Text = 'pattern: //'.freeze
73
+ Sticker = 'package_id: 1, sticker_id: 1'.freeze
67
74
  end
68
75
  end
@@ -1,16 +1,15 @@
1
- module Pleiades
2
- module Generators
3
- class InstallGenerator < Rails::Generators::Base
4
- source_root File.expand_path('templates', __dir__)
1
+ class Pleiades::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
5
3
 
6
- def generate_file
7
- file_paths = %W(
8
- #{Pleiades::Constants::File::CONFIG}
9
- #{Pleiades::Constants::File::ROUTER}
10
- )
4
+ def generate_file
5
+ cst = Pleiades::Constants::File
6
+ file_paths =
7
+ %W[
8
+ #{cst::CONFIG}
9
+ #{cst::INITIALIZER}
10
+ #{cst::ROUTER}
11
+ ]
11
12
 
12
- file_paths.each { |f| copy_file File.basename(f), f }
13
- end
14
- end
13
+ file_paths.each { |f| copy_file File.basename(f), f }
15
14
  end
16
15
  end
@@ -1,5 +1,15 @@
1
1
  command:
2
- commands_path: 'app/commands'
2
+ commands_path: app/commands
3
+ default: base_command
4
+ router:
5
+ default:
6
+ method: call
7
+ concern: []
8
+ executor: Pleiades::Command::Executor
9
+ client:
10
+ key_acquisition_process:
11
+ channel_secret: ENV["LINE_CHANNEL_SECRET"]
12
+ channel_token : ENV["LINE_CHANNEL_TOKEN"]
3
13
  debug:
4
14
  # call BaseCommand#show_event
5
15
  disp_console: true
@@ -0,0 +1 @@
1
+ Pleiades::Config.configration
@@ -0,0 +1,13 @@
1
+ Pleiades::Command::RoutingProxy.routing do
2
+ #
3
+ # EXAMPLE
4
+ # if @event.source.user_id == 'Udeadbeefdeadbeefdeadbeefdeadbeef'
5
+ # mount line: 'config/pleiades/line'
6
+ # add 'line_dev', mnt: :line
7
+ # # => load by 'config/pleiades/line/line_dev.rb'
8
+ # end
9
+ #
10
+
11
+ # only load by 'config/pleiades/router.rb'.
12
+ default_routing
13
+ end
@@ -1,125 +1,127 @@
1
- module Pleiades
2
- module Generators
3
- class SetupGenerator < Rails::Generators::Base
4
- source_root File.expand_path('templates', __dir__)
5
-
6
- exe_user_options = {
7
- aliases: '-u',
8
- type: :boolean,
9
- desc: 'Execute generate migration:user & user_related_commands'
10
- }
11
- class_option :user_related_files, exe_user_options
12
-
13
- def gen_user_related_files
14
- return unless options['user_related_files']
15
-
16
- gen_user_table unless user_table_exist?
17
- gen_users_command unless users_command_exist?
18
- end
1
+ class Pleiades::SetupGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ exe_user_options =
5
+ {
6
+ aliases: '-u',
7
+ type: :boolean,
8
+ desc: 'Execute generate migration:user & user_related_commands'
9
+ }
10
+ class_option :user_related_files, exe_user_options
11
+
12
+ def gen_user_related_files
13
+ return unless options['user_related_files']
14
+
15
+ gen_user_table unless user_table_exist?
16
+ gen_users_command unless users_command_exist?
17
+ end
19
18
 
20
- def gen_base_command
21
- return if base_command_exist?
19
+ def gen_base_command
20
+ return if base_command_exist?
22
21
 
23
- copy_file File.basename(command_file_path), command_file_path
24
- end
22
+ copy_file File.basename(command_file_path), command_file_path
23
+ end
25
24
 
26
- def gen_command_concern
27
- return if command_concern_exist?
25
+ def gen_command_concern
26
+ return if command_concern_exist?
28
27
 
29
- template File.basename(command_concern_path('e')), command_concern_path
30
- end
28
+ template File.basename(command_concern_path('e')), command_concern_path
29
+ end
31
30
 
32
- def gen_controller
33
- return if controller_file_exist?
31
+ def gen_controller
32
+ return if controller_file_exist?
34
33
 
35
- generate 'controller', controller_dir
34
+ generate 'controller', controller_dir
36
35
 
37
- route <<~EOF
38
- namespace :line do
39
- namespace :api do
40
- post '/' , action: 'callback'
41
- end
42
- end
43
- EOF
44
- File.delete controller_file_path
45
- copy_file File.basename(controller_file_path), controller_file_path
46
- end
36
+ route routes_drowing_str
47
37
 
48
- private
38
+ File.delete controller_file_path
39
+ copy_file File.basename(controller_file_path), controller_file_path
40
+ end
49
41
 
50
- def method_missing(method, *_)
51
- method_name = /^(([a-z]+_)+)exist\?$/.match(method.to_s)
52
- return super unless method_name
42
+ private
53
43
 
54
- File.exist? method(:"#{method_name[1]}path").call
55
- end
44
+ def method_missing(method, *_)
45
+ method_name = /^(([a-z]+_)+)exist\?$/.match(method.to_s)
46
+ return super unless method_name
56
47
 
57
- # UserModelのマイグレーションファイル生成
58
- def gen_user_table
59
- generate 'model', "user #{migration_arguments}"
48
+ File.exist? method(:"#{method_name[1]}path").call
49
+ end
60
50
 
61
- user_schemas.each_pair do |key, val|
62
- next unless val[:options]
51
+ # UserModelのマイグレーションファイル生成
52
+ def gen_user_table
53
+ generate 'model', "user #{migration_arguments}"
63
54
 
64
- inject_into_file(
65
- migration_file_path,
66
- ", #{val[:options]}",
67
- after: key
68
- )
69
- end
70
- end
55
+ user_schemas.each_pair do |key, val|
56
+ next unless val[:options]
71
57
 
72
- # ユーザに関するコマンドの生成
73
- def gen_users_command
74
- dir = 'users'
75
- command_events = %w[follow unfollow]
58
+ inject_into_file(
59
+ migration_file_path,
60
+ ", #{val[:options]}",
61
+ after: key
62
+ )
63
+ end
64
+ end
76
65
 
77
- command_events.each { |event| generate 'pleiades:command', "#{event} -d #{dir} -t #{event}" }
78
- end
66
+ # ユーザに関するコマンドの生成
67
+ def gen_users_command
68
+ dir = 'users'
69
+ command_events = %w[follow unfollow]
79
70
 
80
- def migration_arguments
81
- user_schemas
82
- .each_pair
83
- .inject('') { |str, (key, val)| "#{str}#{key}:#{val[:type]} " }
84
- end
71
+ command_events.each { |event| generate 'pleiades:command', "#{event} -d #{dir} -t #{event}" }
72
+ end
85
73
 
86
- def user_schemas
87
- {
88
- line_id: { type: :string, options: 'null: false, unique: true' },
89
- unsubscrided: { type: :boolean },
90
- unsubscrided_at: { type: :datetime }
91
- }
92
- end
74
+ def migration_arguments
75
+ user_schemas
76
+ .each_pair
77
+ .inject('') { |str, (key, val)| "#{str}#{key}:#{val[:type]} " }
78
+ end
93
79
 
94
- def user_table_path
95
- path = Dir.glob(migration_file_path).first
96
- path || migration_file_path
97
- end
80
+ def user_schemas
81
+ {
82
+ line_id: { type: :string, options: 'null: false, unique: true' },
83
+ unsubscrided: { type: :boolean },
84
+ unsubscrided_at: { type: :datetime }
85
+ }
86
+ end
98
87
 
99
- def migration_file_path
100
- 'db/migrate/*_users.rb'
101
- end
88
+ def user_table_path
89
+ path = Dir.glob(migration_file_path).first
90
+ path || migration_file_path
91
+ end
102
92
 
103
- def users_command_path
104
- "#{Pleiades::Config.commands_path}/users/follow.rb"
105
- end
93
+ def migration_file_path
94
+ 'db/migrate/*_users.rb'
95
+ end
106
96
 
107
- def base_command_path
108
- "#{Pleiades::Config.commands_path}/base_command.rb"
109
- end
97
+ def users_command_path
98
+ "#{Pleiades::Config.command.commands_path}/users/follow.rb"
99
+ end
110
100
 
111
- def command_concern_path(add_ext = false)
112
- ext = add_ext ? true.to_s[3] : ''
113
- "#{Pleiades::Config.commands_path}/concerns/command_common.#{ext}rb"
114
- end
101
+ def base_command_path
102
+ "#{Pleiades::Config.command.commands_path}/base_command.rb"
103
+ end
115
104
 
116
- def controller_file_path
117
- "app/controllers/#{controller_dir}_controller.rb"
118
- end
105
+ def command_concern_path(add_ext = false)
106
+ ext = add_ext ? true.to_s[3] : ''
107
+ "#{Pleiades::Config.command.commands_path}/concerns/command_common.#{ext}rb"
108
+ end
109
+
110
+ def controller_file_path
111
+ "app/controllers/#{controller_dir}_controller.rb"
112
+ end
119
113
 
120
- def controller_dir
121
- 'line/api'
114
+ def controller_dir
115
+ 'line/api'
116
+ end
117
+
118
+ def routes_drowing_str
119
+ <<~ROUTE
120
+ namespace :line do
121
+ namespace :api do
122
+ post '/' , action: 'callback'
123
+ end
122
124
  end
123
- end
125
+ ROUTE
124
126
  end
125
127
  end