kamigo 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d529ed01718d1dc57a19189fdcb99530b2fd02358ea2605cb6c6224d024623b9
4
+ data.tar.gz: 417f956b2ce77c78da399142d3a4c88f830b75084dfc4c4a63b12e61377bde74
5
+ SHA512:
6
+ metadata.gz: 1f08b10da6a6ad03a7e711ba1545b8d30e701f926a504849d2b7c85b8d0b2cca86e1bbc6cdd4e090935054f539867dbf2e892027d8c8895aa87c0295fcf52bb8
7
+ data.tar.gz: 82ef36f907229039d06b399453f7ccb031888c10e1df70affdc1bf1c717e0e6e61115ac5780d48392d0da270e484a1b0f2c5ecc5c2b4a3de34b1f41e0842c970
@@ -0,0 +1,20 @@
1
+ Copyright 2019 etrex
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # Kamigo
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'kamigo'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install kamigo
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Kamigo'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
File without changes
@@ -0,0 +1,14 @@
1
+ module ReverseRoute
2
+ def reserve_route(path, http_method: "GET", request_params: nil, format: nil)
3
+ request.request_method = http_method
4
+ request.path_info = path
5
+ request.format = format if format.present?
6
+ request.request_parameters = request_params if request_params.present?
7
+
8
+ # req = Rack::Request.new
9
+ # env = {Rack::RACK_INPUT => StringIO.new}
10
+
11
+ res = Rails.application.routes.router.serve(request)
12
+ res[2].body
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ class LineController < ApplicationController
2
+ include ReverseRoute
3
+ protect_from_forgery with: :null_session
4
+
5
+ def entry
6
+ body = request.body.read
7
+ events = client.parse_events_from(body)
8
+ events.each do |event|
9
+ process_event(event)
10
+ end
11
+ head :ok
12
+ end
13
+
14
+ private
15
+
16
+ def process_event(event)
17
+ reply_token = event['replyToken']
18
+ event
19
+ http_method, path, request_params = language_understanding(event.message['text'])
20
+ output = reserve_route(path, http_method: http_method, request_params: request_params, format: :line)
21
+ response = client.reply_message(reply_token, JSON.parse(output))
22
+ puts response.body
23
+
24
+ rescue NoMethodError => e
25
+ puts e.full_message
26
+ response = client.reply_message(reply_token, {
27
+ type: "text",
28
+ text: "404 not found"
29
+ })
30
+ end
31
+
32
+ def language_understanding(text)
33
+ http_method = %w[GET POST PUT PATCH DELETE].find do |http_method|
34
+ text.start_with? http_method
35
+ end
36
+ text = text[http_method.size..-1] if http_method.present?
37
+ text = text.strip
38
+ lines = text.split("\n").compact
39
+ path = lines.shift
40
+ request_params = parse_json(lines.join(""))
41
+ request_params[:authenticity_token] = form_authenticity_token
42
+ http_method = request_params["_method"]&.upcase || http_method || "GET"
43
+ [http_method, path, request_params]
44
+ end
45
+
46
+ def parse_json(string)
47
+ return {} if string.strip.empty?
48
+ JSON.parse(string)
49
+ end
50
+
51
+ def client
52
+ @client ||= Line::Bot::Client.new do |config|
53
+ config.channel_secret = ENV['LINE_CHANNEL_SECRET']
54
+ config.channel_token = ENV['LINE_CHANNEL_TOKEN']
55
+ end
56
+ end
57
+
58
+ def validate_signature(request, body)
59
+ signature = request.env['HTTP_X_LINE_SIGNATURE']
60
+ client.validate_signature(body, signature)
61
+ end
62
+ end
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ # line webhook entry
3
+ post 'line', to: 'line#entry'
4
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate kamigo Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,57 @@
1
+ require 'rails/generators/named_base'
2
+ require 'rails/generators/resource_helpers'
3
+
4
+ module Rails
5
+ module Generators
6
+ class KamigoGenerator < NamedBase
7
+ include Rails::Generators::ResourceHelpers
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ argument :attributes, type: :array, default: [], banner: 'field:type field:type'
11
+
12
+ class_option :timestamps, type: :boolean, default: true
13
+
14
+ def create_root_folder
15
+ path = File.join('app/views', controller_file_path)
16
+ empty_directory path unless File.directory?(path)
17
+ end
18
+
19
+ def copy_view_files
20
+ filenames.each do |filename|
21
+ template filename, File.join('app/views', controller_file_path, filename)
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def attributes_names
28
+ [:id] + super
29
+ end
30
+
31
+ def filenames
32
+ [
33
+ "index.line.erb",
34
+ "show.line.erb",
35
+ "edit.liff.erb",
36
+ "new.liff.erb",
37
+ ]
38
+ end
39
+
40
+ def full_attributes_list
41
+ if options[:timestamps]
42
+ attributes_list(attributes_names + %w(created_at updated_at))
43
+ else
44
+ attributes_list(attributes_names)
45
+ end
46
+ end
47
+
48
+ def attributes_list(attributes = attributes_names)
49
+ if self.attributes.any? {|attr| attr.name == 'password' && attr.type == :digest}
50
+ attributes = attributes.reject {|name| %w(password password_confirmation).include? name}
51
+ end
52
+
53
+ attributes.map { |a| ":#{a}"} * ', '
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,2 @@
1
+ <%% content_for :title, "Edit <%= singular_table_name.titleize %>" %>
2
+ <%%= render "<%= route_url %>/form.html", <%= singular_table_name %>: @<%= singular_table_name %> %>
@@ -0,0 +1,26 @@
1
+ <%%= raw(Kamiflex.build(self) do
2
+ bubble do
3
+ body do
4
+ horizontal_box do
5
+ text "🍔", flex: 0, action: message_action("/")
6
+ text "<%= class_name.pluralize %>", weight: "bold", margin: "sm"
7
+ text "new", align: "end", action: uri_action(liff_path(path: <%= "new_#{singular_route_name}_path" %>)), color: "#0000ee"
8
+ end
9
+ separator
10
+ if @<%= plural_table_name %>.present?
11
+ vertical_box margin: "lg" do
12
+ horizontal_box margin: "lg" do
13
+ text "<%= attributes&.first&.human_name || "Id" %>", weight: "bold"
14
+ text "action", align: "end"
15
+ end
16
+ horizontal_box @<%= plural_table_name %>, margin: "lg" do |<%= singular_table_name %>|
17
+ text <%= singular_table_name %>.<%= attributes&.first&.column_name || "id" %>, action: message_action("GET <%= route_url %>/#{<%= singular_table_name %>.id}")
18
+ text "❌", align: "end", action: message_action("DELETE <%= route_url %>/#{<%= singular_table_name %>.id}")
19
+ end
20
+ end
21
+ else
22
+ text "沒有目前內容", margin: "lg"
23
+ end
24
+ end
25
+ end
26
+ end )%>
@@ -0,0 +1,2 @@
1
+ <%% content_for :title, "New <%= singular_table_name.titleize %>" %>
2
+ <%%= render "<%= route_url %>/form.html", <%= singular_table_name %>: @<%= singular_table_name %> %>
@@ -0,0 +1,25 @@
1
+ <%%= raw(Kamiflex.build(self) do
2
+ bubble do
3
+ body do
4
+ horizontal_box do
5
+ text "🍔", flex: 0, action: message_action("/")
6
+ text "Show <%= class_name %>", weight: "bold", margin: "sm"
7
+ text "edit", align: "end", action: uri_action(liff_path(path: <%= "edit_#{singular_route_name}_path(@#{singular_table_name})" %> )), color: "#0000ee"
8
+ end
9
+ separator
10
+
11
+ horizontal_box margin: "md" do
12
+ vertical_box do
13
+ <% attributes_names.each do |attribute_name| -%>
14
+ text "<%= attribute_name %>", weight: "bold", margin: "xs"
15
+ <% end -%>
16
+ end
17
+ vertical_box do
18
+ <% attributes_names.each do |attribute_name| -%>
19
+ text @<%= singular_table_name %>.<%= attribute_name %>.blank? ? "-" : @<%= singular_table_name %>.<%= attribute_name %>.to_s, align: "end", margin: "xs"
20
+ <% end -%>
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end )%>
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
3
+
4
+ module Rails
5
+ module Generators
6
+ class ScaffoldControllerGenerator
7
+ source_paths << File.expand_path('../templates', __FILE__)
8
+
9
+ hook_for :jbuilder, type: :boolean, default: true
10
+ hook_for :kamigo, type: :boolean, default: true
11
+
12
+ private
13
+
14
+ def permitted_params
15
+ attributes_names.map { |name| ":#{name}" }.join(', ')
16
+ end unless private_method_defined? :permitted_params
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,92 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= namespaced_file_path %>/application_controller"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class <%= controller_class_name %>Controller < ApplicationController
7
+ before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
8
+
9
+ # GET <%= route_url %>
10
+ # GET <%= route_url %>.json
11
+ def index
12
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
13
+ end
14
+
15
+ # GET <%= route_url %>/1
16
+ # GET <%= route_url %>/1.json
17
+ def show
18
+ end
19
+
20
+ # GET <%= route_url %>/new
21
+ def new
22
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
23
+ end
24
+
25
+ # GET <%= route_url %>/1/edit
26
+ def edit
27
+ end
28
+
29
+ # POST <%= route_url %>
30
+ # POST <%= route_url %>.json
31
+ def create
32
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
33
+
34
+ respond_to do |format|
35
+ if @<%= orm_instance.save %>
36
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
37
+ format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> }
38
+ format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
39
+ format.line { render :index }
40
+ else
41
+ format.html { render :new }
42
+ format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
43
+ format.line { render json: flex_text(<%= "@#{orm_instance.errors}" %>.to_s) }
44
+ end
45
+ end
46
+ end
47
+
48
+ # PATCH/PUT <%= route_url %>/1
49
+ # PATCH/PUT <%= route_url %>/1.json
50
+ def update
51
+ respond_to do |format|
52
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
53
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
54
+ format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
55
+ format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
56
+ format.line { render :index }
57
+ else
58
+ format.html { render :edit }
59
+ format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
60
+ format.line { render json: flex_text(@todo.errors.to_s) }
61
+ end
62
+ end
63
+ end
64
+
65
+ # DELETE <%= route_url %>/1
66
+ # DELETE <%= route_url %>/1.json
67
+ def destroy
68
+ @<%= orm_instance.destroy %>
69
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
70
+ respond_to do |format|
71
+ format.html { redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> }
72
+ format.json { head :no_content }
73
+ format.line { render :index }
74
+ end
75
+ end
76
+
77
+ private
78
+ # Use callbacks to share common setup or constraints between actions.
79
+ def set_<%= singular_table_name %>
80
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
81
+ end
82
+
83
+ # Never trust parameters from the scary internet, only allow the white list through.
84
+ def <%= "#{singular_table_name}_params" %>
85
+ <%- if attributes_names.empty? -%>
86
+ params.fetch(<%= ":#{singular_table_name}" %>, {})
87
+ <%- else -%>
88
+ params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
89
+ <%- end -%>
90
+ end
91
+ end
92
+ <% end -%>
@@ -0,0 +1,8 @@
1
+ require "kamigo/engine"
2
+ require "line-bot-api"
3
+ require "kamiflex"
4
+ require "kamiliff"
5
+
6
+ module Kamigo
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,4 @@
1
+ module Kamigo
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,36 @@
1
+ require 'rails/railtie'
2
+ # require 'jbuilder/jbuilder_template'
3
+
4
+ class Jbuilder
5
+ class Railtie < ::Rails::Railtie
6
+ # initializer :jbuilder do
7
+ # ActiveSupport.on_load :action_view do
8
+ # ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
9
+ # require 'jbuilder/dependency_tracker'
10
+ # end
11
+
12
+ # if Rails::VERSION::MAJOR >= 5
13
+ # module ::ActionController
14
+ # module ApiRendering
15
+ # include ActionView::Rendering
16
+ # end
17
+ # end
18
+
19
+ # ActiveSupport.on_load :action_controller do
20
+ # if self == ActionController::API
21
+ # include ActionController::Helpers
22
+ # include ActionController::ImplicitRender
23
+ # end
24
+ # end
25
+ # end
26
+ # end
27
+
28
+ # if Rails::VERSION::MAJOR >= 4
29
+ generators do |app|
30
+ Rails::Generators.configure! app.config.generators
31
+ Rails::Generators.hidden_namespaces.uniq!
32
+ require 'generators/rails/scaffold_controller_generator'
33
+ end
34
+ # end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Kamigo
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,6 @@
1
+ # desc "Explaining what the task does"
2
+ namespace :kamigo do
3
+ task :scaffold do
4
+ puts "kamigo scaffold"
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kamigo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - etrex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: kamiliff
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: kamiflex
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: line-bot-api
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: a chatbot framework based on rails
84
+ email:
85
+ - et284vu065k3@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - app/assets/config/kamigo_manifest.js
94
+ - app/controllers/concerns/reverse_route.rb
95
+ - app/controllers/line_controller.rb
96
+ - config/routes.rb
97
+ - lib/generators/rails/kamigo/USAGE
98
+ - lib/generators/rails/kamigo/kamigo_generator.rb
99
+ - lib/generators/rails/kamigo/templates/edit.liff.erb
100
+ - lib/generators/rails/kamigo/templates/index.line.erb
101
+ - lib/generators/rails/kamigo/templates/new.liff.erb
102
+ - lib/generators/rails/kamigo/templates/show.line.erb
103
+ - lib/generators/rails/scaffold_controller_generator.rb
104
+ - lib/generators/rails/templates/controller.rb
105
+ - lib/kamigo.rb
106
+ - lib/kamigo/engine.rb
107
+ - lib/kamigo/railtie.rb
108
+ - lib/kamigo/version.rb
109
+ - lib/tasks/kamigo.rake
110
+ homepage: https://github.com/etrex/kamigo
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubygems_version: 3.0.3
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: a chatbot framework based on rails
133
+ test_files: []