rails_liff 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bf93438d5059b859e44a4969c6c310f36f60b7cef8b452b4f04520c68a52d11
4
- data.tar.gz: 6172757153c6c617eb60fedbbf2966cce60cfdd3614c1d0b64f3481bf7f49e29
3
+ metadata.gz: 15597087e32963e4ab7b501b976e98da8200efd55a75de25f6ee4344ba8a939b
4
+ data.tar.gz: c9bcafcf900c22d32b129bf1cb00a59bfb75d1ee95b394ebfe39b22077bc1e1d
5
5
  SHA512:
6
- metadata.gz: 0dba30d7f7866b67cf6c2bd3fa65e2d03f9e38c38db4eca4d6536c4f87f10924c36af31cbca582b3ca3b4f9a29e54223dace3ba92f93c2341d4aeb8e5278e79f
7
- data.tar.gz: 90a8bbc5d12133153f6f0fad9783dbb3d0a7b39830f044e3842530d8744a7f515cd7f19478c23206d3f615a0249b8a004fd0db3f7017e19d289cf82d4581b148
6
+ metadata.gz: b2707626a0608504b9c8323c9f9110ad4b823246d37973c40f0e0d60269ad66edba31faa53d1339e2bd12616e660bfb933e0f2240318c58f86ab9cafe7c415f1
7
+ data.tar.gz: 90fbb45bd348dfed1912ce87e56bab753eb55177623472c5277b4b6eb4cb652ed65bbf3ded0b757bf03a528e61e7bcbd67f6b6b88de2a91258ca4bc6dfbfc582
data/README.md CHANGED
@@ -1,28 +1,83 @@
1
1
  # RailsLiff
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ RailsLiff make LIFF easy to use.
4
+
5
+ # Feature
6
+ - register LIFF once and reuse to your all path.
7
+ - liff_submit event: a hook when LIFF form submit, with form data in js object format.
8
+ - liff_send_text_message: quick send message and close LIFF.
6
9
 
7
10
  ## Installation
8
- Add this line to your application's Gemfile:
9
11
 
10
12
  ```ruby
11
13
  gem 'rails_liff'
12
14
  ```
13
15
 
14
- And then execute:
15
- ```bash
16
- $ bundle
16
+ login to line developers, and create 3 LIFF for 3 different size.
17
+
18
+ - for compact
19
+ - name: Compact
20
+ - size: Compact
21
+ - Endpoint URL: https://yourwebsite/liff
22
+
23
+ - for tall
24
+ - name: Tall
25
+ - size: Tall
26
+ - Endpoint URL: https://yourwebsite/liff
27
+
28
+ - for full
29
+ - name: Full
30
+ - size: Full
31
+ - Endpoint URL: https://yourwebsite/liff
32
+
33
+ and then copy the result to enviroment parameters:
34
+
35
+ ```
36
+ LIFF_COMPACT=line://app/for_compact_liff_id
37
+ LIFF_TALL=line://app/for_tall_liff_id
38
+ LIFF_FULL=line://app/for_full_liff_id
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ### Implement LIFF View
44
+
45
+ RailsLiff using view format `.liff`, so you can reuse exists controller and model, you can create a LIFF form by only adding a new view file.
46
+
47
+ Suppose you have a resource `todos`, you want to create a liff form for `todos/new`, so you create a file `app/views/todos/new.liff.erb`, the content is as follows:
48
+
49
+ ```
50
+ <% content_for :title, "new todo" %>
51
+
52
+ <%= render "todos/form.html", todo: @todo %>
53
+
54
+ <%= link_to "test", liff_path(path: '/todos/new') %>
55
+ ```
56
+
57
+ You can test the form at [localhost:3000/todos/new.liff](localhost:3000/todos/new.liff)
58
+
59
+ ### Generate LIFF Link
60
+
61
+ You can change any path to liff by `liff_path` method.
62
+
63
+ for example:
64
+
65
+ ```
66
+ liff_path(path: '/todos/new')
17
67
  ```
18
68
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install rails_liff
69
+ You can choice the liff size by `liff_size` parameter. the default value of liff_size is compact.
70
+
71
+ ```
72
+ liff_path(path: '/todos/new', liff_size: :compact)
73
+ liff_path(path: '/todos/new', liff_size: :tall)
74
+ liff_path(path: '/todos/new', liff_size: :full)
22
75
  ```
23
76
 
24
- ## Contributing
25
- Contribution directions go here.
77
+ liff_path method add format :liff automatically.
78
+
79
+ ## Author
80
+ create by [etrex](https://etrex.tw)
26
81
 
27
82
  ## License
28
83
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,57 @@
1
+ function liff_send_text_message(text){
2
+ liff.sendMessages(
3
+ [
4
+ {
5
+ type: 'text',
6
+ text: text
7
+ }
8
+ ]
9
+ ).then(() => {
10
+ liff.closeWindow();
11
+ })
12
+ .catch((err) => {
13
+ alert('error', err);
14
+ });
15
+ }
16
+
17
+ $(function(){
18
+ function dispatch_liff_event(data){
19
+ var event = new CustomEvent('liff_submit', { 'detail': data });
20
+ window.dispatchEvent(event);
21
+ }
22
+ $('input[type="submit"]').click(function(e){
23
+ e.preventDefault();
24
+ dispatch_liff_event(get_request_text($("form")));
25
+ })
26
+
27
+ function get_request_text(form_element){
28
+ var data = get_form_data(form_element);
29
+ var method = form_element.attr("method").toUpperCase();
30
+ var url = form_element.attr("action");
31
+ return {
32
+ data: data,
33
+ method: method,
34
+ url: url
35
+ }
36
+ }
37
+
38
+ function get_form_data(form_element){
39
+ var excpet = ["utf8","authenticity_token"];
40
+ var form = form_element.serializeArray();
41
+ form = form.filter((a)=>{ return !excpet.includes(a.name) })
42
+ var data = {}
43
+ form.forEach((a)=>{ set_object_value(data, a.name, a.value) })
44
+ return data;
45
+ }
46
+
47
+ function set_object_value(object, path, value){
48
+ var o = object;
49
+ var p = path.replace(/(\]\[)/g, "[").replace(/]$/g, "").split("[")
50
+ var last_key = p.pop();
51
+ p.forEach((key)=>{
52
+ o[key] = o[key] || {}
53
+ o = o[key]
54
+ })
55
+ o[last_key] = value;
56
+ }
57
+ });
@@ -1,6 +1,8 @@
1
1
  module LiffHelper
2
2
  def liff_path(params)
3
- liff_url = ENV['LIFF_FORM']
3
+ liff_size = params[:liff_size] || "COMPACT"
4
+ raise "liff_size should be COMPACT, TALL or FULL." unless liff_size.in? %w[COMPACT TALL FULL]
5
+ liff_url = ENV["LIFF_#{liff_size}"]
4
6
  "#{liff_url}?#{params.to_query}"
5
7
  end
6
8
  end
@@ -13,64 +13,12 @@
13
13
 
14
14
  <script src="https://d.line-scdn.net/liff/1.0/sdk.js"></script>
15
15
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
16
+ <%= javascript_include_tag 'rails_liff', 'data-turbolinks-track': 'reload' %>
16
17
  </head>
17
18
 
18
19
  <body>
19
20
  <div class="container-fluid">
20
21
  <%= yield %>
21
22
  </div>
22
-
23
- <script>
24
- $(function(){
25
- $('input[type="submit"]').click(function(e){
26
- e.preventDefault();
27
- send_message(get_request_text($("form")));
28
- })
29
-
30
- function send_message(text){
31
- liff.sendMessages(
32
- [
33
- {
34
- type: 'text',
35
- text: text
36
- }
37
- ]
38
- ).then(() => {
39
- liff.closeWindow();
40
- })
41
- .catch((err) => {
42
- alert('error', err);
43
- });
44
- }
45
-
46
- function get_request_text(form_element){
47
- var data = get_form_data(form_element);
48
- var json = JSON.stringify(data);
49
- var method = form_element.attr("method").toUpperCase();
50
- var url = form_element.attr("action");
51
- return `${method} ${url}\n${json}`;
52
- }
53
-
54
- function get_form_data(form_element){
55
- var excpet = ["utf8","authenticity_token"];
56
- var form = form_element.serializeArray();
57
- form = form.filter((a)=>{ return !excpet.includes(a.name) })
58
- var data = {}
59
- form.forEach((a)=>{ set_object_value(data, a.name, a.value) })
60
- return data;
61
- }
62
-
63
- function set_object_value(object, path, value){
64
- var o = object;
65
- var p = path.replace(/(\]\[)/g, "[").replace(/]$/g, "").split("[")
66
- var last_key = p.pop();
67
- p.forEach((key)=>{
68
- o[key] = o[key] || {}
69
- o = o[key]
70
- })
71
- o[last_key] = value;
72
- }
73
- });
74
- </script>
75
23
  </body>
76
24
  </html>
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.precompile << "rails_liff.js"
@@ -1,3 +1,3 @@
1
1
  module RailsLiff
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_liff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - etrex kuo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-11 00:00:00.000000000 Z
11
+ date: 2019-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -34,10 +34,12 @@ files:
34
34
  - MIT-LICENSE
35
35
  - README.md
36
36
  - Rakefile
37
+ - app/assets/javascripts/rails_liff.js
37
38
  - app/controllers/liff_controller.rb
38
39
  - app/helpers/liff_helper.rb
39
40
  - app/views/layouts/application.liff.erb
40
41
  - app/views/liff/entry.html.erb
42
+ - config/initializers/assets.rb
41
43
  - config/initializers/mime_types.rb
42
44
  - config/routes.rb
43
45
  - lib/rails_liff.rb