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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61770f9cbd46a0c7a47fadf3f42f69ec91efa608e7b7adf1c02bad853236c126
4
- data.tar.gz: 729ad8ca0d354114894429c5b78eefbb627f2fd1cac55d688b3da4fcbf665002
3
+ metadata.gz: c303e9dfbed28743e63d0c5d542eb7c7ab357b9e2b7fbf8d0ca2a8ab40a7a551
4
+ data.tar.gz: 0dd07f1e245f0c56fd8ab7857c92ed2a4c94dbc8213102c643f606ee513132ae
5
5
  SHA512:
6
- metadata.gz: 968cf11cf8dd04d42a30852b5d05134bc4cc7c48e822fc5b63f58134233255681f48bf64ebfd2acf018c651285f2b59d19b5c64efe137f2d8c11e61938338a58
7
- data.tar.gz: eb3d084d1fa7cbbf971956205c26a1d7096a3de12ae3dd9171fa54a14e6c465ccdf56cfac165483bfbe8ffa5755e2633f9064509ff8653c9714610993440c30c
6
+ metadata.gz: e26d3c12ade048b4ba9e45a170c88d5a837b78af8a2c09893b8da2643ac1c1f10e76841a2942874df72f47e76713450bed3b24d3c4c4c94f802cf1745de021fc
7
+ data.tar.gz: 84d6fcc5db36955ce9b20532451ef09bd92620e5eafcca39523885392481a996ad43517ce866a1395d2f2f6e020ed4bca4113285e9344d7e1e20b55799093fb3
data/.gitignore CHANGED
@@ -8,8 +8,8 @@
8
8
  !Gemfile
9
9
  !LICENSE.txt
10
10
  !pleiades.gemspec
11
- Rakefile
12
- README.md
11
+ !Rakefile
12
+ !README.md
13
13
 
14
14
  !/bin/
15
15
  !/bin/**/*
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Pleiades
2
+
3
+ RailsでLINEBotを開発するためのgem。
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pleiades'
11
+ ```
12
+ $ bundle install
13
+
14
+ ## Usage
15
+ Comming soon.....
16
+
17
+ ## Contributing
18
+
19
+ https://github.com/harukikubota/pleiades
20
+
21
+ ## License
22
+
23
+ [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/lib/pleiades/core.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'pleiades/core/client'
1
2
  require 'pleiades/core/command'
2
3
  require 'pleiades/core/config'
3
4
  require 'pleiades/core/constants'
@@ -0,0 +1,52 @@
1
+ require 'pleiades/core/client/wrapper'
2
+
3
+ module Pleiades
4
+ module Client
5
+ def self.included(base)
6
+ include_modules = [Common]
7
+
8
+ on_controller_class = ApplicationController.subclasses.include?(base)
9
+
10
+ include_modules << Controller if on_controller_class
11
+
12
+ include_modules.each do |mod|
13
+ base.include mod
14
+ end
15
+ end
16
+
17
+ module Common
18
+ def self.included(base)
19
+ base.class_eval <<~RUBY, __FILE__, __LINE__ + 1
20
+ def client
21
+ return @client if @client
22
+
23
+ channel_secret, channel_token = Pleiades::Config.client_keys
24
+
25
+ @client = Line::Bot::Client.new do |config|
26
+ config.channel_secret = channel_secret
27
+ config.channel_token = channel_token
28
+ end
29
+ end
30
+ RUBY
31
+ end
32
+ end
33
+
34
+ module Controller
35
+ def validate_signature
36
+ client.validate_signature(body, signature)
37
+ end
38
+
39
+ def signature
40
+ request.env['HTTP_X_LINE_SIGNATURE']
41
+ end
42
+
43
+ def body
44
+ @body ||= request.body.read
45
+ end
46
+
47
+ def events
48
+ client.parse_events_from(body)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,55 @@
1
+ module Pleiades
2
+ module Client
3
+ class Wrapper
4
+ attr_reader :histories
5
+
6
+ def initialize(client)
7
+ @obj = client
8
+ @histories = []
9
+ end
10
+
11
+ private
12
+
13
+ def method_missing(method, *args, &block)
14
+ return super unless @obj.respond_to?(method)
15
+
16
+ execute(method, *args)
17
+ end
18
+
19
+ def respond_to_missing?(method, *_, &_)
20
+ @obj.respond_to?(method) || super
21
+ end
22
+
23
+ def respond_to?(method)
24
+ @obj.respond_to?(method) || super
25
+ end
26
+
27
+ def execute(method, *args)
28
+ res = @obj.__send__ method, *args
29
+ code = res.code
30
+ body = JSON.parse(res.body)
31
+
32
+ @histories << response.new(method, code, body)
33
+
34
+ [code, body]
35
+ end
36
+
37
+ def method_assigns(method, *args)
38
+ return {} if args.empty?
39
+
40
+ @obj.method(method)
41
+ .parameters
42
+ .map(&:last)
43
+ .each_with_index
44
+ .each_with_object({}) do |(arg_name, at), hash|
45
+ hash.store(arg_name, args[at])
46
+ end
47
+ end
48
+
49
+ def response
50
+ attributes = %i[method_name code body]
51
+ Struct.new(*attributes)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,18 +1,22 @@
1
- require 'pleiades/core/command/router'
2
1
  require 'pleiades/core/command/base_command'
2
+ require 'pleiades/core/command/executor'
3
+ require 'pleiades/core/command/factory'
4
+ require 'pleiades/core/command/router'
5
+ require 'pleiades/core/command/routing_proxy'
3
6
  require 'pleiades/core/config'
4
7
 
5
8
  module Pleiades
6
9
  module Command
7
10
  class << self
8
- def get event
9
- path = Pleiades::Command::Router.find_route event
11
+ def get(event)
12
+ event = Pleiades::Util.define_reader(event).freeze
10
13
 
11
- command_cst = path.split('/')
12
- .map(&:camelize)
13
- .join('::')
14
+ RoutingProxy.collect_router_file(event).each do |path|
15
+ Router.find_route(event, path)
16
+ break if Router.path_found?
17
+ end
14
18
 
15
- command_cst.constantize.new(event)
19
+ Factory.production(event, Router.path_info)
16
20
  end
17
21
  end
18
22
  end
@@ -1,26 +1,11 @@
1
1
  module Pleiades
2
2
  module Command
3
3
  class BaseCommand
4
- def initialize event
4
+ def initialize(event)
5
5
  @event = event
6
- @success = nil
7
6
  end
8
7
 
9
8
  def call; end
10
-
11
- def success?
12
- @success
13
- end
14
-
15
- protected
16
-
17
- def success!
18
- @success = true
19
- end
20
-
21
- def fail!
22
- @success = false
23
- end
24
9
  end
25
10
  end
26
11
  end
@@ -0,0 +1,16 @@
1
+ module Pleiades
2
+ module Command
3
+ class Executor
4
+ def initialize(command, method)
5
+ @to = command
6
+ @method = method
7
+ end
8
+
9
+ def execute
10
+ @to.__send__ @method
11
+ @to
12
+ end
13
+ alias :call execute
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,50 @@
1
+ require 'pleiades/core/command/executor'
2
+
3
+ module Pleiades
4
+ module Command
5
+ class Factory
6
+ def self.production(event, path_info)
7
+ @factory = new(event, path_info)
8
+ @factory.operate
9
+ end
10
+
11
+ def initialize(event, path_info)
12
+ @event = event
13
+ @path_info = path_info
14
+ end
15
+
16
+ def operate
17
+ executor_class(decorate_command(command_class))
18
+ end
19
+
20
+ private
21
+
22
+ def command_class
23
+ klass_path = @path_info[:command_path] || Pleiades::Config.command.default
24
+
25
+ command_constantize(klass_path).new(@event)
26
+ end
27
+
28
+ def command_constantize(path)
29
+ path.split('/').map(&:camelize).join('::').constantize
30
+ end
31
+
32
+ def executor_class(command)
33
+ @path_info[:executor].constantize.new(
34
+ command,
35
+ @path_info[:call_method]
36
+ )
37
+ end
38
+
39
+ def decorate_command(command)
40
+ concerns = @path_info[:concern].map(&:constantize)
41
+ command.class_eval do
42
+ concerns.each do |concern|
43
+ include concern
44
+ end
45
+ end
46
+ command
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,36 +1,51 @@
1
1
  require 'pleiades/core/constants'
2
2
  require 'pleiades/core/config'
3
- require 'pleiades/core/command/nest_blocks'
4
- require 'pleiades/core/command/event_proccessor'
3
+ require 'pleiades/core/command/routing/event_proccessor'
4
+ require 'pleiades/core/command/routing/nest_blocks'
5
+ require 'pleiades/core/command/routing/path_builder'
6
+ require 'pleiades/core/command/routing/reflection'
7
+ require 'pleiades/core/command/routing/result'
8
+ require 'pleiades/core/command/routing/validator'
5
9
 
6
10
  module Pleiades
7
11
  module Command
8
12
  class Router
9
- prepend Pleiades::Command::NestBlocks
10
- prepend Pleiades::Command::EventProccessor
13
+ prepend Pleiades::Command::Routing::EventProccessor
14
+ prepend Pleiades::Command::Routing::NestBlocks
15
+ prepend Pleiades::Command::Routing::PathBuilder
16
+ prepend Pleiades::Command::Routing::Reflection
17
+ prepend Pleiades::Command::Routing::Validator
11
18
 
12
19
  class << self
13
- attr_accessor :path
14
20
  attr_reader :event
21
+ attr_writer :path_info
15
22
 
16
- def find_route(event)
23
+ def find_route(event, router_path)
17
24
  @event = event
18
- @path = nil
25
+ @path_info = nil
19
26
 
20
- load Pleiades::Constants::File::ROUTER
21
-
22
- Router.path_found? ? Router.path : 'base_command'
27
+ load router_path
23
28
  end
24
29
 
25
30
  def route(&block)
26
31
  new.instance_eval(&block) if block_given?
27
32
  end
28
33
 
34
+ def path_info
35
+ @path_info || default_path_info
36
+ end
37
+
38
+ def default_path_info
39
+ new.instance_eval { Pleiades::Command::Routing::Result.create(@options) }
40
+ end
41
+
29
42
  def path_found?
30
- @path ? true : false
43
+ !!@path_info
31
44
  end
32
45
  end
33
46
 
47
+ attr_reader :options
48
+
34
49
  def initialize(options = nil)
35
50
  @event = Router.event
36
51
  @options = options || default_options
@@ -38,22 +53,8 @@ module Pleiades
38
53
 
39
54
  private
40
55
 
41
- def normalize_path(scope = nil, action = nil)
42
- dirs = []
43
-
44
- dirs << @options[:scope] if @options[:scope].any?
45
- dirs << scope if scope
46
- dirs << (action || @options[:action])
47
-
48
- dirs.join('/')
49
- end
50
-
51
- def talk_type(*callable_types, &block)
52
- instance_eval(&block) if callable_type?(callable_types)
53
- end
54
-
55
- def callable_type?(types)
56
- types.map(&:to_s).include?(@event.source.type)
56
+ def nest(new_option, &block)
57
+ self.class.new(new_option).instance_eval(&block)
57
58
  end
58
59
  end
59
60
  end
@@ -0,0 +1,51 @@
1
+ module Pleiades
2
+ module Command
3
+ module Routing
4
+ module EventJudgeMethods
5
+ private
6
+
7
+ def judge_method_defined?(method_name)
8
+ Pleiades::Command::Routing::EventJudgeMethods
9
+ .private_instance_methods(false)
10
+ .include?(judge_method(method_name))
11
+ end
12
+
13
+ def judge_method(event)
14
+ :"judge_#{event}"
15
+ end
16
+
17
+ def judge_text(args)
18
+ pattern =
19
+ case args[:pattern]
20
+ when Regexp
21
+ args[:pattern]
22
+ when String
23
+ /^#{args[:pattern]}$/
24
+ end
25
+
26
+ pattern =~ @event.text
27
+ end
28
+
29
+ def judge_sticker(args)
30
+ p_id, s_id = @event.sticker_ids
31
+ convert_to_reg(args[:package_id]) =~ p_id &&
32
+ convert_to_reg(args[:sticker_id]) =~ s_id
33
+ end
34
+
35
+ def convert_to_reg(id)
36
+ case id
37
+ when String, Integer
38
+ return '*'.eql?(id) ? /^\d+$/ : /^#{id}$/
39
+ when Array
40
+ return /^#{id.join('|')}$/
41
+ end
42
+ id
43
+ end
44
+
45
+ def judge_postback(args)
46
+ normalize_path(args[:scope], args[:action]) == @event.action
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,122 @@
1
+ require 'pleiades/core/command/routing/event_judge_methods'
2
+
3
+ module Pleiades
4
+ module Command
5
+ module Routing
6
+ module EventProccessor
7
+ extend ActiveSupport::Concern
8
+ prepend Pleiades::Command::Routing::EventJudgeMethods
9
+
10
+ # match 複数のイベントメソッドを実行する。
11
+ #
12
+ # EXAMPLE(以下のmatchなし、matchありは同義)
13
+ #
14
+ # ## matchなし
15
+ # scope 'line' do
16
+ # action :greet do
17
+ # text pattern: 'Hello, world'
18
+ # sticker package_id: '1', sticker_id: '1'
19
+ # end
20
+ # end
21
+ #
22
+ # ## matchあり
23
+ # match(
24
+ # via: {
25
+ # text: {
26
+ # pattern: 'Hello, world'
27
+ # },
28
+ # sticker: {
29
+ # package_id: '1',
30
+ # sticker_id: '1'
31
+ # }
32
+ # },
33
+ # scope: 'line',
34
+ # action: :greet
35
+ # )
36
+ #
37
+ def match(**args)
38
+ validate_match_keywords(args)
39
+
40
+ args.delete(:via).each_pair do |event, val|
41
+ __send__ event, merge_from_match(val, args)
42
+ end
43
+ end
44
+
45
+ # イベントクラス名をリフレクションして、イベントメソッドを実行する。
46
+ #
47
+ # EXAMPLE
48
+ # ## 使用しない
49
+ # text pattern: 'Hello'
50
+ #
51
+ # ## 使用する
52
+ # p __event_name__ # => "text"
53
+ # event pattern: 'Hello'
54
+ #
55
+ def event(**keywords)
56
+ __send__ __event_name__.to_sym, keywords
57
+ end
58
+
59
+ private
60
+
61
+ def method_missing(method, *args, &block)
62
+ return super unless event_types.include?(method.to_s)
63
+
64
+ exe_event_method(args.inject(&:merge).merge(type: method))
65
+ end
66
+
67
+ def respond_to_missing?(method, *args, &block)
68
+ event_types.include?(method.to_s) || super
69
+ end
70
+
71
+ def exe_event_method(args)
72
+ on_execute = validate_event_keywords(args) && event_executable?(args)
73
+ return unless on_execute
74
+
75
+ method_name = args[:type]
76
+
77
+ if judge_method_defined?(method_name)
78
+ return unless method(judge_method(method_name)).call(args)
79
+ end
80
+
81
+ route_fix!(args)
82
+ end
83
+
84
+ def event_executable?(args)
85
+ exe_conditions = [route_unfixid?, matching_events?(args[:type])]
86
+
87
+ exe_conditions << callable_talk_type?([args[:talk_type]]) if args.key?(:talk_type)
88
+
89
+ exe_conditions.all?
90
+ end
91
+
92
+ def merge_from_match(via, arg)
93
+ arg.merge(via) do |key, a_val, v_val|
94
+ case key
95
+ when :scope, :concern
96
+ [a_val, v_val].flatten
97
+ else
98
+ v_val
99
+ end
100
+ end
101
+ end
102
+
103
+ def event_types
104
+ Pleiades::Constants::Events::TYPES
105
+ end
106
+
107
+ def route_unfixid?
108
+ !Router.path_found?
109
+ end
110
+
111
+ def matching_events?(method_name)
112
+ __event_name__ == method_name.to_s
113
+ end
114
+
115
+ def route_fix!(args)
116
+ Pleiades::Command::Router.path_info =
117
+ Pleiades::Command::Routing::Result.create(@options, args)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end