pleiades 0.1.0 → 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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +18 -10
  3. data/Gemfile.lock +60 -59
  4. data/README.md +5 -21
  5. data/lib/pleiades/core/.keep +0 -0
  6. data/lib/pleiades/core/command/base_command.rb +26 -0
  7. data/lib/pleiades/core/command/event_proccessor.rb +87 -0
  8. data/lib/pleiades/core/command/nest_blocks.rb +55 -0
  9. data/lib/pleiades/core/command/router.rb +60 -0
  10. data/lib/pleiades/core/command.rb +19 -0
  11. data/lib/pleiades/core/config.rb +52 -0
  12. data/lib/pleiades/core/constants.rb +19 -0
  13. data/lib/pleiades/core/util.rb +18 -0
  14. data/lib/pleiades/core.rb +3 -0
  15. data/lib/pleiades/core_ext/.keep +0 -0
  16. data/lib/pleiades/core_ext/line/bot/event/base.rb +34 -0
  17. data/lib/pleiades/core_ext/line/bot/event/message.rb +24 -0
  18. data/lib/pleiades/core_ext/line/bot/event/postback.rb +29 -0
  19. data/lib/pleiades/core_ext/line/bot/event.rb +3 -0
  20. data/lib/pleiades/core_ext/line/bot.rb +2 -0
  21. data/lib/pleiades/core_ext.rb +1 -0
  22. data/lib/pleiades/generators/pleiades/command/command_generator.rb +68 -0
  23. data/lib/pleiades/generators/pleiades/command/templates/command.erb +6 -0
  24. data/lib/pleiades/generators/pleiades/install/USAGE +5 -0
  25. data/lib/pleiades/generators/pleiades/install/install_generator.rb +16 -0
  26. data/lib/pleiades/generators/pleiades/install/templates/config.yml +5 -0
  27. data/lib/pleiades/generators/pleiades/install/templates/router.rb +2 -0
  28. data/lib/pleiades/generators/pleiades/setup/setup_generator.rb +125 -0
  29. data/lib/pleiades/generators/pleiades/setup/templates/api_controller.rb +25 -0
  30. data/lib/pleiades/generators/pleiades/setup/templates/base_command.rb +28 -0
  31. data/lib/pleiades/generators/pleiades/setup/templates/command_common.erb +18 -0
  32. data/lib/pleiades/railtie.rb +18 -0
  33. data/lib/pleiades/version.rb +1 -1
  34. data/lib/pleiades.rb +4 -3
  35. data/pleiades.gemspec +2 -2
  36. metadata +32 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e09501bb9c5a50805b5eac30f3fe45e5b698d8d08d153a024306cbe3ce49bbe
4
- data.tar.gz: 53271ea51f0ac3feab1ae3ee38c77beba89c342c5fe602b083563c0235defa31
3
+ metadata.gz: 61770f9cbd46a0c7a47fadf3f42f69ec91efa608e7b7adf1c02bad853236c126
4
+ data.tar.gz: 729ad8ca0d354114894429c5b78eefbb627f2fd1cac55d688b3da4fcbf665002
5
5
  SHA512:
6
- metadata.gz: 324a5dc70bf027a3e99a26b597ec011d16e9dbb9881fc69a2d872343d5e2b54449a65d7c14cf9a9cf3e1f80314fe0eb1f7a7fdc40e96ae5edde835ddc9db82fd
7
- data.tar.gz: adcfec8628100a7823b1db1f048be2ff05431c812d2f1d1a86eeecbed1b99214eb05956f92e6ec98adee486085e7f5ca74f640dc00d375c8d2a5ca636e50bb8d
6
+ metadata.gz: 968cf11cf8dd04d42a30852b5d05134bc4cc7c48e822fc5b63f58134233255681f48bf64ebfd2acf018c651285f2b59d19b5c64efe137f2d8c11e61938338a58
7
+ data.tar.gz: eb3d084d1fa7cbbf971956205c26a1d7096a3de12ae3dd9171fa54a14e6c465ccdf56cfac165483bfbe8ffa5755e2633f9064509ff8653c9714610993440c30c
data/.gitignore CHANGED
@@ -1,11 +1,19 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
1
+ *
2
+ */
9
3
 
10
- # rspec failure tracking
11
- .rspec_status
4
+ !.gitignore
5
+ !.rspec
6
+ !.ruby-version
7
+ !.travis.yml
8
+ !Gemfile
9
+ !LICENSE.txt
10
+ !pleiades.gemspec
11
+ Rakefile
12
+ README.md
13
+
14
+ !/bin/
15
+ !/bin/**/*
16
+ !/lib/
17
+ !/lib/**/*
18
+ !/spec/
19
+ !/spec/**/*
data/Gemfile.lock CHANGED
@@ -1,64 +1,64 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pleiades (0.1.0)
4
+ pleiades (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- actioncable (5.2.3)
10
- actionpack (= 5.2.3)
9
+ actioncable (5.2.4)
10
+ actionpack (= 5.2.4)
11
11
  nio4r (~> 2.0)
12
12
  websocket-driver (>= 0.6.1)
13
- actionmailer (5.2.3)
14
- actionpack (= 5.2.3)
15
- actionview (= 5.2.3)
16
- activejob (= 5.2.3)
13
+ actionmailer (5.2.4)
14
+ actionpack (= 5.2.4)
15
+ actionview (= 5.2.4)
16
+ activejob (= 5.2.4)
17
17
  mail (~> 2.5, >= 2.5.4)
18
18
  rails-dom-testing (~> 2.0)
19
- actionpack (5.2.3)
20
- actionview (= 5.2.3)
21
- activesupport (= 5.2.3)
19
+ actionpack (5.2.4)
20
+ actionview (= 5.2.4)
21
+ activesupport (= 5.2.4)
22
22
  rack (~> 2.0)
23
23
  rack-test (>= 0.6.3)
24
24
  rails-dom-testing (~> 2.0)
25
25
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
26
- actionview (5.2.3)
27
- activesupport (= 5.2.3)
26
+ actionview (5.2.4)
27
+ activesupport (= 5.2.4)
28
28
  builder (~> 3.1)
29
29
  erubi (~> 1.4)
30
30
  rails-dom-testing (~> 2.0)
31
31
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
32
- activejob (5.2.3)
33
- activesupport (= 5.2.3)
32
+ activejob (5.2.4)
33
+ activesupport (= 5.2.4)
34
34
  globalid (>= 0.3.6)
35
- activemodel (5.2.3)
36
- activesupport (= 5.2.3)
37
- activerecord (5.2.3)
38
- activemodel (= 5.2.3)
39
- activesupport (= 5.2.3)
35
+ activemodel (5.2.4)
36
+ activesupport (= 5.2.4)
37
+ activerecord (5.2.4)
38
+ activemodel (= 5.2.4)
39
+ activesupport (= 5.2.4)
40
40
  arel (>= 9.0)
41
- activestorage (5.2.3)
42
- actionpack (= 5.2.3)
43
- activerecord (= 5.2.3)
41
+ activestorage (5.2.4)
42
+ actionpack (= 5.2.4)
43
+ activerecord (= 5.2.4)
44
44
  marcel (~> 0.3.1)
45
- activesupport (5.2.3)
45
+ activesupport (5.2.4)
46
46
  concurrent-ruby (~> 1.0, >= 1.0.2)
47
47
  i18n (>= 0.7, < 2)
48
48
  minitest (~> 5.1)
49
49
  tzinfo (~> 1.1)
50
50
  arel (9.0.0)
51
- builder (3.2.3)
51
+ builder (3.2.4)
52
52
  concurrent-ruby (1.1.5)
53
- crass (1.0.4)
53
+ crass (1.0.5)
54
54
  diff-lcs (1.3)
55
- erubi (1.8.0)
55
+ erubi (1.9.0)
56
56
  globalid (0.4.2)
57
57
  activesupport (>= 4.2.0)
58
- i18n (1.6.0)
58
+ i18n (1.7.0)
59
59
  concurrent-ruby (~> 1.0)
60
60
  line-bot-api (1.12.0)
61
- loofah (2.2.3)
61
+ loofah (2.4.0)
62
62
  crass (~> 1.0.2)
63
63
  nokogiri (>= 1.5.9)
64
64
  mail (2.7.1)
@@ -69,52 +69,52 @@ GEM
69
69
  mimemagic (0.3.3)
70
70
  mini_mime (1.0.2)
71
71
  mini_portile2 (2.4.0)
72
- minitest (5.11.3)
73
- nio4r (2.4.0)
74
- nokogiri (1.10.4)
72
+ minitest (5.13.0)
73
+ nio4r (2.5.2)
74
+ nokogiri (1.10.7)
75
75
  mini_portile2 (~> 2.4.0)
76
76
  rack (2.0.7)
77
77
  rack-test (1.1.0)
78
78
  rack (>= 1.0, < 3)
79
- rails (5.2.3)
80
- actioncable (= 5.2.3)
81
- actionmailer (= 5.2.3)
82
- actionpack (= 5.2.3)
83
- actionview (= 5.2.3)
84
- activejob (= 5.2.3)
85
- activemodel (= 5.2.3)
86
- activerecord (= 5.2.3)
87
- activestorage (= 5.2.3)
88
- activesupport (= 5.2.3)
79
+ rails (5.2.4)
80
+ actioncable (= 5.2.4)
81
+ actionmailer (= 5.2.4)
82
+ actionpack (= 5.2.4)
83
+ actionview (= 5.2.4)
84
+ activejob (= 5.2.4)
85
+ activemodel (= 5.2.4)
86
+ activerecord (= 5.2.4)
87
+ activestorage (= 5.2.4)
88
+ activesupport (= 5.2.4)
89
89
  bundler (>= 1.3.0)
90
- railties (= 5.2.3)
90
+ railties (= 5.2.4)
91
91
  sprockets-rails (>= 2.0.0)
92
92
  rails-dom-testing (2.0.3)
93
93
  activesupport (>= 4.2.0)
94
94
  nokogiri (>= 1.6)
95
- rails-html-sanitizer (1.2.0)
96
- loofah (~> 2.2, >= 2.2.2)
97
- railties (5.2.3)
98
- actionpack (= 5.2.3)
99
- activesupport (= 5.2.3)
95
+ rails-html-sanitizer (1.3.0)
96
+ loofah (~> 2.3)
97
+ railties (5.2.4)
98
+ actionpack (= 5.2.4)
99
+ activesupport (= 5.2.4)
100
100
  method_source
101
101
  rake (>= 0.8.7)
102
102
  thor (>= 0.19.0, < 2.0)
103
103
  rake (10.5.0)
104
- rspec (3.8.0)
105
- rspec-core (~> 3.8.0)
106
- rspec-expectations (~> 3.8.0)
107
- rspec-mocks (~> 3.8.0)
108
- rspec-core (3.8.2)
109
- rspec-support (~> 3.8.0)
110
- rspec-expectations (3.8.4)
104
+ rspec (3.9.0)
105
+ rspec-core (~> 3.9.0)
106
+ rspec-expectations (~> 3.9.0)
107
+ rspec-mocks (~> 3.9.0)
108
+ rspec-core (3.9.0)
109
+ rspec-support (~> 3.9.0)
110
+ rspec-expectations (3.9.0)
111
111
  diff-lcs (>= 1.2.0, < 2.0)
112
- rspec-support (~> 3.8.0)
113
- rspec-mocks (3.8.1)
112
+ rspec-support (~> 3.9.0)
113
+ rspec-mocks (3.9.0)
114
114
  diff-lcs (>= 1.2.0, < 2.0)
115
- rspec-support (~> 3.8.0)
116
- rspec-support (3.8.2)
117
- sprockets (3.7.2)
115
+ rspec-support (~> 3.9.0)
116
+ rspec-support (3.9.0)
117
+ sprockets (4.0.0)
118
118
  concurrent-ruby (~> 1.0)
119
119
  rack (> 1, < 3)
120
120
  sprockets-rails (3.2.1)
@@ -135,6 +135,7 @@ PLATFORMS
135
135
  DEPENDENCIES
136
136
  bundler (~> 2.0)
137
137
  line-bot-api (~> 1.12.0)
138
+ loofah (>= 2.3.1)
138
139
  pleiades!
139
140
  rails (~> 5.2.3)
140
141
  rake (~> 10.0)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Pleiades
2
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/pleiades`. 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
3
+ RailsでLINEBotを開発するためのgem
6
4
 
7
5
  ## Installation
8
6
 
@@ -11,29 +9,15 @@ Add this line to your application's Gemfile:
11
9
  ```ruby
12
10
  gem 'pleiades'
13
11
  ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install pleiades
12
+ $ bundle install
22
13
 
23
14
  ## 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).
15
+ Comming soon.....
32
16
 
33
17
  ## Contributing
34
18
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pleiades.
19
+ https://github.com/harukikubota/pleiades
36
20
 
37
21
  ## License
38
22
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
23
+ [MIT License](https://opensource.org/licenses/MIT)
File without changes
@@ -0,0 +1,26 @@
1
+ module Pleiades
2
+ module Command
3
+ class BaseCommand
4
+ def initialize event
5
+ @event = event
6
+ @success = nil
7
+ end
8
+
9
+ 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
+ end
25
+ end
26
+ end
@@ -0,0 +1,87 @@
1
+ module Pleiades
2
+ module Command
3
+ module EventProccessor
4
+ extend ActiveSupport::Concern
5
+
6
+ def method_missing(method, **args)
7
+ return super unless event_types.include?(method.to_s)
8
+
9
+ args.merge! type: method.to_s
10
+
11
+ exe_event_method args
12
+ end
13
+
14
+ private
15
+
16
+ def event_types
17
+ Pleiades::Constants::Events::TYPES
18
+ end
19
+
20
+ def exe_event_method(args)
21
+ procs(args[:type]).each_with_object([true]) do |proc, arr|
22
+ next unless arr.last
23
+
24
+ arr << proc.call(args)
25
+ end
26
+ end
27
+
28
+ def procs(method_name)
29
+ proc_method_names = %i[
30
+ route_unfixid?
31
+ matching_events?
32
+ route_fix!
33
+ ]
34
+
35
+ proc_method_names.insert(2, :"#{method_name}_judge_method") if judge_method_defined?(method_name)
36
+ proc_method_names.map { |n| method(n) }
37
+ end
38
+
39
+ def route_unfixid?(_args)
40
+ !Router.path_found?
41
+ end
42
+
43
+ def matching_events?(args)
44
+ @event.type == args[:type]
45
+ end
46
+
47
+ def route_fix!(args)
48
+ Router.path = normalize_path args[:scope], args[:action]
49
+ end
50
+
51
+ def judge_method_defined?(method)
52
+ private_methods.include?(:"#{method}_judge_method")
53
+ end
54
+
55
+ def text_judge_method(args)
56
+ decision_operator =
57
+ case args[:pattern]
58
+ when Regexp
59
+ '=~'
60
+ when String
61
+ '=='
62
+ end
63
+ eval("args[:pattern] #{decision_operator} @event.text")
64
+ end
65
+
66
+ def sticker_judge_method(args)
67
+ convert_to_reg = -> (id) do
68
+ case id
69
+ when String, Integer
70
+ return '*'.eql?(id) ? /^\d+$/ : /^#{id}$/
71
+ when Array
72
+ return /^#{id.join('|')}$/
73
+ end
74
+ id
75
+ end
76
+
77
+ p, s = @event.sticker_ids
78
+ convert_to_reg.(args[:package_id]) =~ p &&
79
+ convert_to_reg.(args[:sticker_id]) =~ s
80
+ end
81
+
82
+ def postback_judge_method(args)
83
+ normalize_path(args[:scope], args[:action]) == @event.action
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,55 @@
1
+ require 'active_support/core_ext'
2
+
3
+ module Pleiades
4
+ module Command
5
+ module NestBlocks
6
+ extend ActiveSupport::Concern
7
+
8
+ def scope(*scopes, &block)
9
+ scopes.each { |s| nest_block __callee__, s, &block }
10
+ end
11
+ alias :scope_append scope
12
+ alias :scope_unshift scope
13
+
14
+ def action(*actions, &block)
15
+ actions.each { |a| nest_block __callee__, a, &block }
16
+ end
17
+
18
+ private
19
+
20
+ def default_options
21
+ {
22
+ scope: [],
23
+ action: ''
24
+ }
25
+ end
26
+
27
+ def nest_block(method, context, &block)
28
+ @_options = __send__ "merge_#{method}", context
29
+ Pleiades::Command::Router.new(@_options).instance_eval(&block)
30
+ end
31
+
32
+ def merge_scope(context)
33
+ operator =
34
+ if /^merge_scope(_append)?$/ =~ __callee__
35
+ :append
36
+ elsif /^merge_scope_unshift$/ =~ __callee__
37
+ :unshift
38
+ end
39
+ __merge__ ->(option) { option[:scope].__send__ operator, context }
40
+ end
41
+ alias :merge_scope_append merge_scope
42
+ alias :merge_scope_unshift merge_scope
43
+
44
+ def merge_action(context)
45
+ __merge__ ->(option) { option[:action] = context }
46
+ end
47
+
48
+ def __merge__(proc)
49
+ dup_options = @options.deep_dup
50
+ proc.call(dup_options)
51
+ dup_options
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,60 @@
1
+ require 'pleiades/core/constants'
2
+ require 'pleiades/core/config'
3
+ require 'pleiades/core/command/nest_blocks'
4
+ require 'pleiades/core/command/event_proccessor'
5
+
6
+ module Pleiades
7
+ module Command
8
+ class Router
9
+ prepend Pleiades::Command::NestBlocks
10
+ prepend Pleiades::Command::EventProccessor
11
+
12
+ class << self
13
+ attr_accessor :path
14
+ attr_reader :event
15
+
16
+ def find_route(event)
17
+ @event = event
18
+ @path = nil
19
+
20
+ load Pleiades::Constants::File::ROUTER
21
+
22
+ Router.path_found? ? Router.path : 'base_command'
23
+ end
24
+
25
+ def route(&block)
26
+ new.instance_eval(&block) if block_given?
27
+ end
28
+
29
+ def path_found?
30
+ @path ? true : false
31
+ end
32
+ end
33
+
34
+ def initialize(options = nil)
35
+ @event = Router.event
36
+ @options = options || default_options
37
+ end
38
+
39
+ private
40
+
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)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ require 'pleiades/core/command/router'
2
+ require 'pleiades/core/command/base_command'
3
+ require 'pleiades/core/config'
4
+
5
+ module Pleiades
6
+ module Command
7
+ class << self
8
+ def get event
9
+ path = Pleiades::Command::Router.find_route event
10
+
11
+ command_cst = path.split('/')
12
+ .map(&:camelize)
13
+ .join('::')
14
+
15
+ command_cst.constantize.new(event)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ require 'yaml'
2
+ require 'pleiades/core/util'
3
+
4
+ module Pleiades
5
+ class Config
6
+ class << self
7
+ def configration
8
+ return nil if loaded?
9
+
10
+ @config = new(load).freeze
11
+ @loaded = true
12
+ end
13
+
14
+ def loaded?
15
+ @loaded
16
+ end
17
+
18
+ def method_missing method, *_
19
+ configration
20
+ return super unless instance_methods.include?(method)
21
+
22
+ @config.__send__ method
23
+ end
24
+
25
+ def respond_to_missing? method, _
26
+ instance_methods.include?(method)
27
+ end
28
+
29
+ private
30
+
31
+ def load
32
+ YAML.load_file Pleiades::Constants::File::CONFIG
33
+ end
34
+ end
35
+
36
+ def commands_path
37
+ @src.command.commands_path
38
+ end
39
+
40
+ def disp_console
41
+ @src.debug.disp_console
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :src
47
+
48
+ def initialize src
49
+ @src = Pleiades::Util.define_reader src
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,19 @@
1
+ module Pleiades
2
+ module Constants
3
+ module Events
4
+ TYPES = %w[
5
+ text
6
+ sticker
7
+ postback
8
+ follow
9
+ unfollow
10
+ ].map(&:freeze).freeze
11
+ end
12
+
13
+ module File
14
+ CONFIG_DIR_PATH = 'config/pleiades'.freeze
15
+ CONFIG = "#{CONFIG_DIR_PATH}/config.yml".freeze
16
+ ROUTER = "#{CONFIG_DIR_PATH}/router.rb".freeze
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pleiades
4
+
5
+ # Util Modules.
6
+ module Util
7
+ class << self
8
+ 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
+ )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ require 'pleiades/core/command'
2
+ require 'pleiades/core/config'
3
+ require 'pleiades/core/constants'
File without changes
@@ -0,0 +1,34 @@
1
+ require 'pleiades/core/util'
2
+
3
+ module Line
4
+ module Bot
5
+ module Event
6
+ class Base
7
+ def initialize src
8
+ @src = Pleiades::Util.define_reader src
9
+
10
+ # /^[a-z]+_event\?$/
11
+ # => トークタイプの判定メソッドに反応する。
12
+ #
13
+ # @src.source.type => 'user'
14
+ # user_event? => true
15
+ # room_event? => false
16
+ # hoge_event? => false
17
+ #
18
+ @src.define_singleton_method(:method_missing) do |method, *_|
19
+ return super() unless /^[a-z]+_event\?$/ =~ method
20
+ source.type == method.to_s.split('_').first
21
+ end
22
+ end
23
+
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
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ module Line
2
+ module Bot
3
+ module Event
4
+ class Message
5
+
6
+ # ユーザの送信メッセージ
7
+ #
8
+ # @return EXAMPLE 'Hello world.'
9
+ #
10
+ def text
11
+ message.text
12
+ end
13
+
14
+ # ユーザの送信スタンプのids(パッケージID、スタンプID)
15
+ #
16
+ # @return EXAMPLE ["1", "1"]
17
+ #
18
+ def sticker_ids
19
+ [message.package_id, message.sticker_id]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ module Line
2
+ module Bot
3
+ module Event
4
+ class Postback
5
+ attr_reader :action, :params
6
+
7
+ def initialize src
8
+ super
9
+ set_instance_variables
10
+ end
11
+
12
+ private
13
+
14
+ # dataプロパティからインスタンス変数に設定する。
15
+ #
16
+ # action 'path/to/command'
17
+ # params '{product_id: 1, order_num: 3}'
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 }
22
+
23
+ @action = data.delete :action
24
+ @params = data
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ require 'pleiades/core_ext/line/bot/event/base'
2
+ require 'pleiades/core_ext/line/bot/event/message'
3
+ require 'pleiades/core_ext/line/bot/event/postback'
@@ -0,0 +1,2 @@
1
+ require 'line/bot'
2
+ require 'pleiades/core_ext/line/bot/event'
@@ -0,0 +1 @@
1
+ require 'pleiades/core_ext/line/bot'
@@ -0,0 +1,68 @@
1
+ require 'pleiades/core/constants'
2
+ require 'pleiades/core/config'
3
+
4
+ module Pleiades
5
+ module Generators
6
+ class CommandGenerator < Rails::Generators::NamedBase
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ argument :name, type: :string, desc: 'Specify command class name.'
10
+
11
+ dir_options = {
12
+ aliases: '-d',
13
+ desc: 'Specify the directory to generate commands.',
14
+ default: 'common'
15
+ }
16
+ class_option :dir, dir_options
17
+
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
25
+
26
+ def generate_command
27
+ commands_path = Pleiades::Config.commands_path
28
+ template 'command.erb', "#{commands_path}/#{options['dir']}/#{name}.rb"
29
+ end
30
+
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
39
+
40
+ private
41
+
42
+ def event_with_option
43
+ option = event_specific_options
44
+
45
+ str = "\n\t#{options['event_type']}"
46
+ str += " scope: '#{options['dir']}', action: '#{name}'"
47
+ str += ", #{option}" if option
48
+
49
+ str
50
+ end
51
+
52
+ def event_specific_options
53
+ EventOption.const_get options['event_type'].capitalize
54
+ rescue NameError => _
55
+ nil
56
+ end
57
+
58
+ def dirs
59
+ options['dir'].split('/')
60
+ end
61
+
62
+ module EventOption
63
+ Text = 'pattern: //'.freeze
64
+ Sticker = 'package_id: 1, sticker_id: 1'.freeze
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,6 @@
1
+ <% dirs.each.with_index(0) do |dir, tab| %><%= ' ' * tab %>module <%= dir.camelize %>
2
+ <% end %><%= ' ' * dirs.size %>class <%= name.camelize%> < BaseCommand
3
+ <%= ' ' * (dirs.size + 1) %>def call
4
+ <%= ' ' * (dirs.size + 2) %># TODO: write something
5
+ <% [*0..(dirs.size + 1)].reverse_each do |tab| %><%= ' ' * tab %>end
6
+ <% end %>
@@ -0,0 +1,5 @@
1
+ Usage:
2
+ rails generate pleiades:install
3
+
4
+ Description:
5
+ Create pleiades files for install generator.
@@ -0,0 +1,16 @@
1
+ module Pleiades
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ def generate_file
7
+ file_paths = %W(
8
+ #{Pleiades::Constants::File::CONFIG}
9
+ #{Pleiades::Constants::File::ROUTER}
10
+ )
11
+
12
+ file_paths.each { |f| copy_file File.basename(f), f }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ command:
2
+ commands_path: 'app/commands'
3
+ debug:
4
+ # call BaseCommand#show_event
5
+ disp_console: true
@@ -0,0 +1,2 @@
1
+ Pleiades::Command::Router.route do
2
+ end
@@ -0,0 +1,125 @@
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
19
+
20
+ def gen_base_command
21
+ return if base_command_exist?
22
+
23
+ copy_file File.basename(command_file_path), command_file_path
24
+ end
25
+
26
+ def gen_command_concern
27
+ return if command_concern_exist?
28
+
29
+ template File.basename(command_concern_path('e')), command_concern_path
30
+ end
31
+
32
+ def gen_controller
33
+ return if controller_file_exist?
34
+
35
+ generate 'controller', controller_dir
36
+
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
47
+
48
+ private
49
+
50
+ def method_missing(method, *_)
51
+ method_name = /^(([a-z]+_)+)exist\?$/.match(method.to_s)
52
+ return super unless method_name
53
+
54
+ File.exist? method(:"#{method_name[1]}path").call
55
+ end
56
+
57
+ # UserModelのマイグレーションファイル生成
58
+ def gen_user_table
59
+ generate 'model', "user #{migration_arguments}"
60
+
61
+ user_schemas.each_pair do |key, val|
62
+ next unless val[:options]
63
+
64
+ inject_into_file(
65
+ migration_file_path,
66
+ ", #{val[:options]}",
67
+ after: key
68
+ )
69
+ end
70
+ end
71
+
72
+ # ユーザに関するコマンドの生成
73
+ def gen_users_command
74
+ dir = 'users'
75
+ command_events = %w[follow unfollow]
76
+
77
+ command_events.each { |event| generate 'pleiades:command', "#{event} -d #{dir} -t #{event}" }
78
+ end
79
+
80
+ def migration_arguments
81
+ user_schemas
82
+ .each_pair
83
+ .inject('') { |str, (key, val)| "#{str}#{key}:#{val[:type]} " }
84
+ end
85
+
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
93
+
94
+ def user_table_path
95
+ path = Dir.glob(migration_file_path).first
96
+ path || migration_file_path
97
+ end
98
+
99
+ def migration_file_path
100
+ 'db/migrate/*_users.rb'
101
+ end
102
+
103
+ def users_command_path
104
+ "#{Pleiades::Config.commands_path}/users/follow.rb"
105
+ end
106
+
107
+ def base_command_path
108
+ "#{Pleiades::Config.commands_path}/base_command.rb"
109
+ end
110
+
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
115
+
116
+ def controller_file_path
117
+ "app/controllers/#{controller_dir}_controller.rb"
118
+ end
119
+
120
+ def controller_dir
121
+ 'line/api'
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,25 @@
1
+ require 'pleiades'
2
+
3
+ class Line::ApiController < ApplicationController
4
+ def client
5
+ @client ||= Line::Bot::Client.new { |config|
6
+ config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
7
+ config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
8
+ }
9
+ end
10
+
11
+ def callback
12
+ body = request.body.read
13
+
14
+ signature = request.env['HTTP_X_LINE_SIGNATURE']
15
+ client.validate_signature(body, signature) || head(:bad_request)
16
+
17
+ events = client.parse_events_from(body)
18
+
19
+ events.each do |event|
20
+ command = Pleiades::Command.get(event)
21
+ command.call
22
+ head :ok if command.success?
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ class BaseCommand < Pleiades::Command::BaseCommand
2
+ include CommandCommon
3
+
4
+ def call
5
+ success!
6
+ show_event if disp?
7
+ end
8
+
9
+ private
10
+
11
+ def disp?
12
+ Rails.env.development? && Pleiades::Config.disp_console
13
+ end
14
+
15
+ def show_event
16
+ mes = <<~MES
17
+ \n
18
+ \n
19
+ |------------------------------------|
20
+ | There is no corresponding command. |
21
+ |------------------------------------|
22
+ \n
23
+ event:#{@event.type}
24
+ MES
25
+
26
+ p mes
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ module CommandCommon
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ def client
6
+ @client ||= Line::Bot::Client.new { |config|
7
+ config.channel_secret = ENV['LINE_CHANNEL_SECRET']
8
+ config.channel_token = ENV['LINE_CHANNEL_TOKEN']
9
+ }
10
+ end<% if options['user_related_files'] %>
11
+
12
+ def current_user
13
+ @user ||= User.find_by_line_id(@event.source.user_id)
14
+ end
15
+ alias_method :user, :current_user
16
+ <% end %>
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Pleiades
2
+ class Railtie < ::Rails::Railtie
3
+ generators do
4
+
5
+ generator_names = %w(
6
+ install
7
+ setup
8
+ command
9
+ )
10
+
11
+ generator_path = './generators/pleiades'.freeze
12
+ generator_names
13
+ .map { |f| "#{generator_path}/#{f}/#{f}_generator.rb" }
14
+ .each { |f| require_relative f }
15
+
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Pleiades
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/pleiades.rb CHANGED
@@ -1,6 +1,7 @@
1
- require "pleiades/version"
1
+ require 'pleiades/version'
2
+ require 'pleiades/railtie'
3
+ require 'pleiades/core'
4
+ require 'pleiades/core_ext'
2
5
 
3
6
  module Pleiades
4
- class Error < StandardError; end
5
- # Your code goes here...
6
7
  end
data/pleiades.gemspec CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ['lib']
27
27
 
28
- spec.add_development_dependency 'bundler', '~> 2.0'
29
- spec.add_development_dependency 'line-bot-api', '~> 1.12.0'
28
+ spec.add_dependency 'bundler', '~> 2.0'
29
+ spec.add_dependency 'line-bot-api', '~> 1.12.0'
30
30
  spec.add_development_dependency 'rails', '~> 5.2.3'
31
31
  spec.add_development_dependency 'rake', '~> 10.0'
32
32
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pleiades
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - HarukiKubota
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-07 00:00:00.000000000 Z
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -17,7 +17,7 @@ dependencies:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.0'
20
- type: :development
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -31,7 +31,7 @@ dependencies:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.12.0
34
- type: :development
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
@@ -98,6 +98,34 @@ files:
98
98
  - bin/console
99
99
  - bin/setup
100
100
  - lib/pleiades.rb
101
+ - lib/pleiades/core.rb
102
+ - lib/pleiades/core/.keep
103
+ - lib/pleiades/core/command.rb
104
+ - lib/pleiades/core/command/base_command.rb
105
+ - lib/pleiades/core/command/event_proccessor.rb
106
+ - lib/pleiades/core/command/nest_blocks.rb
107
+ - lib/pleiades/core/command/router.rb
108
+ - lib/pleiades/core/config.rb
109
+ - lib/pleiades/core/constants.rb
110
+ - lib/pleiades/core/util.rb
111
+ - lib/pleiades/core_ext.rb
112
+ - lib/pleiades/core_ext/.keep
113
+ - lib/pleiades/core_ext/line/bot.rb
114
+ - lib/pleiades/core_ext/line/bot/event.rb
115
+ - lib/pleiades/core_ext/line/bot/event/base.rb
116
+ - lib/pleiades/core_ext/line/bot/event/message.rb
117
+ - lib/pleiades/core_ext/line/bot/event/postback.rb
118
+ - lib/pleiades/generators/pleiades/command/command_generator.rb
119
+ - lib/pleiades/generators/pleiades/command/templates/command.erb
120
+ - lib/pleiades/generators/pleiades/install/USAGE
121
+ - lib/pleiades/generators/pleiades/install/install_generator.rb
122
+ - lib/pleiades/generators/pleiades/install/templates/config.yml
123
+ - lib/pleiades/generators/pleiades/install/templates/router.rb
124
+ - lib/pleiades/generators/pleiades/setup/setup_generator.rb
125
+ - lib/pleiades/generators/pleiades/setup/templates/api_controller.rb
126
+ - lib/pleiades/generators/pleiades/setup/templates/base_command.rb
127
+ - lib/pleiades/generators/pleiades/setup/templates/command_common.erb
128
+ - lib/pleiades/railtie.rb
101
129
  - lib/pleiades/version.rb
102
130
  - pleiades.gemspec
103
131
  homepage: https://github.com/harukikubota/pleiades