kamiliff 0.23.0 → 0.27.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: d8d9f1e36c1dc706651145146649792d8f128784824c476d688e1b7c42b0e6ba
4
- data.tar.gz: 2bc4437b67d94a0bb39456d470a615d63363aadf43bb1fbf6301659fc80e982e
3
+ metadata.gz: 38aca00ff3bcedc33c8e81f3c2067db8aeba06020701d93b03e8c6927b12267e
4
+ data.tar.gz: 7a5eeece3bc9a2530d568b9b50c99e8fd39e77b4f48ee79a59e85b1cf07811d6
5
5
  SHA512:
6
- metadata.gz: 7debaefba5c60a6327e74db069680dbcd4462a42c1ad31a10ea28456651302032e7c6c4c8a9a625509ee095731a8292f6e6424a63b629065483e769215607d71
7
- data.tar.gz: ce34f047195fe836f37f3da058902aef6218448efacc0c0bf5e1d11235e9b0e6d57a3d16d02ba95f462e51af1c83d782a9240411cae49eea95863a881ada2172
6
+ metadata.gz: fcded347d34ad824c20dc0e481d46e5ee376140f049a7d3fe603d6ea71dd9662bcfe9d09bd4d9cb2d3a2ad384f8503f4bfaeb22529a5a040a09e570bc99410a0
7
+ data.tar.gz: 71bbd81ebc6963ddd9dca6244803e62c4bbbce31ec4479b7f7fa3d948c8fa2ce7e397ebc225d10f832d6ee3ba6f744c2c7e45d900143f6885db17ab89bb32b4e
@@ -7,18 +7,31 @@ class LiffController < ActionController::Base
7
7
 
8
8
  # fix liff 2.0 redirect issue
9
9
  @need_reload = query["liff.state"].present?
10
+
11
+ # 第一次 redirect
10
12
  if(@need_reload)
11
- querystring = query["liff.state"][(query["liff.state"].index('?')+1)..-1]
12
- query = Rack::Utils.parse_nested_query(querystring)
13
+ if(query["liff.state"][0] == '/')
14
+ @liff = LiffBase64Service.from_base64(query["liff.state"][1..-1])
15
+ else
16
+ querystring = query["liff.state"][(query["liff.state"].index('?')+1)..-1]
17
+ query = Rack::Utils.parse_nested_query(querystring)
18
+ @liff = LiffService.new(query)
19
+ end
20
+ # 第二次 redirect
21
+ else
22
+ if params[:base64].present?
23
+ @liff = LiffBase64Service.from_base64(params[:base64])
24
+ else
25
+ @liff = LiffService.new(query)
26
+ end
13
27
  end
14
-
15
- @liff = LiffService.new(query)
16
28
  end
17
29
 
18
30
  def route
19
31
  path, query = params["path"].split("?")
20
32
  query = Rack::Utils.parse_nested_query(query)
21
- @body = reserve_route(path, request_params: source_info.merge(query), format: :liff)
33
+ http_method = query["_method"]&.upcase || "GET"
34
+ @body = reserve_route(path, http_method: http_method, request_params: source_info.merge(query), format: :liff)
22
35
  end
23
36
 
24
37
  private
@@ -1,5 +1,5 @@
1
1
  module LiffHelper
2
- def liff_path(params)
2
+ def old_liff_path(params)
3
3
  liff = LiffService.new(params)
4
4
  if ENV["LIFF_MODE"]&.downcase == "replace"
5
5
  return "#{liff.url}/liff_entry?#{params.to_query}"
@@ -7,4 +7,9 @@ module LiffHelper
7
7
  # liff mode is Concatenate
8
8
  "#{liff.url}?#{params.to_query}"
9
9
  end
10
+
11
+ def liff_path(params)
12
+ liff = LiffBase64Service.new(params)
13
+ liff.full_url
14
+ end
10
15
  end
@@ -1,6 +1,6 @@
1
1
  <% content_for :js do %>
2
2
  <script>
3
- (function(){
3
+ let open_liff_page = (function(){
4
4
  function append_csrf(data){
5
5
  var csrf_param = undefined;
6
6
  var csrf_token = undefined;
@@ -24,47 +24,52 @@
24
24
  $("#liff_body").html(body);
25
25
  }
26
26
 
27
- function dispatch_ready_event(data){
27
+ function dispatch_liff_ready_event(){
28
28
  var event = new CustomEvent('liff_ready');
29
29
  window.dispatchEvent(event);
30
30
  }
31
31
 
32
- function route(data){
32
+ async function liff_init(){
33
+ await liff.init({
34
+ liffId: "<%= @liff.id %>"
35
+ });
36
+ if (!liff.isLoggedIn()) {
37
+ liff.login();
38
+ }
39
+ }
40
+
41
+ async function get_liff_data(){
42
+ const profile = await liff.getProfile();
43
+ const context = liff.getContext();
44
+ return {
45
+ profile,
46
+ context
47
+ }
48
+ }
49
+
50
+ async function open_liff_page(path){
51
+ const data = await get_liff_data();
52
+ data.path = path;
33
53
  $.ajax({
34
54
  type: "POST",
35
55
  url: '/liff_route',
36
56
  data: append_csrf(data)
37
57
  }).done(function(html){
38
58
  render_body(html);
39
- dispatch_ready_event();
59
+ dispatch_liff_ready_event();
40
60
  }).fail(function(e){
41
61
  alert("on error: " + JSON.stringify(e));
42
62
  });
43
63
  }
44
64
 
45
- window.onload = function() {
46
- liff.init({
47
- liffId: "<%= @liff.id %>"
48
- })
49
- .then(() => {
50
- if (!liff.isLoggedIn()) {
51
- liff.login();
52
- }else{
53
- liff.getProfile().then(profile => {
54
- const data = {};
55
- data.context = liff.getContext();
56
- data.profile = profile;
57
- data.path = "<%= @liff.path %>";
58
- <% if !@need_reload %>
59
- route(data);
60
- <% end %>
61
- })
62
- }
63
- })
64
- .catch((err) => {
65
- console.log(err.code, err.message);
66
- });
65
+ window.onload = async function() {
66
+ await liff_init();
67
+ <% if !@need_reload %>
68
+ await open_liff_page("<%= @liff.path %>");
69
+ <% end %>
67
70
  };
71
+
72
+ return open_liff_page;
68
73
  })();
69
74
  </script>
70
75
  <% end %>
data/config/routes.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  Rails.application.routes.draw do
2
2
  # liff
3
+ get 'liff_entry/:base64', to: 'liff#entry'
3
4
  get 'liff_entry', to: 'liff#entry'
4
5
  post 'liff_route', to: 'liff#route'
5
6
  end
data/lib/kamiliff.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "kamiliff/engine"
2
2
  require "kamiliff/services/liff_service"
3
+ require "kamiliff/services/liff_base64_service"
3
4
 
4
5
  module Kamiliff
5
6
  # Your code goes here...
@@ -0,0 +1,52 @@
1
+ require "base64"
2
+
3
+ class LiffBase64Service
4
+
5
+ # rails routes path
6
+ attr_accessor :path
7
+
8
+ # size
9
+ # COMPACT TALL FULL
10
+ attr_accessor :size
11
+
12
+ # liff app url
13
+ # https://liff.line.me/app/#{liff_id}
14
+ attr_accessor :url
15
+
16
+ # liff id
17
+ attr_accessor :id
18
+
19
+ def self.from_base64(base64_string)
20
+ base64_string = base64_string.tr('-','+').tr('_','/')
21
+ json = Base64.decode64(base64_string)
22
+ options = JSON.parse(json)
23
+ LiffBase64Service.new(options)
24
+ end
25
+
26
+ def to_base64
27
+ json = {
28
+ path: path,
29
+ liff_size: size
30
+ }.to_json
31
+ base64_string = Base64.encode64(json)
32
+ base64_string.tr('+','-').tr('/','_').tr("\n", '').tr('=', '')
33
+ end
34
+
35
+ def initialize(options)
36
+ options = options.with_indifferent_access
37
+ self.path = options[:path] || "/"
38
+ self.size = options[:liff_size] || :compact
39
+ self.size = size.to_s.upcase
40
+ raise "liff_size should be compact, tall or full." unless size.in? %w[COMPACT TALL FULL]
41
+ self.url = ENV["LIFF_#{size}"]
42
+ raise "LIFF_#{size} should be in the env variables" if url.blank?
43
+ self.id = url[(url.rindex('/')+1)..-1]
44
+ end
45
+
46
+ def full_url
47
+ if ENV["LIFF_MODE"]&.downcase == "replace"
48
+ end
49
+ # liff mode is Concatenate
50
+ "#{url}/#{to_base64}"
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module Kamiliff
2
- VERSION = '0.23.0'
2
+ VERSION = '0.27.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kamiliff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.27.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: 2020-12-28 00:00:00.000000000 Z
11
+ date: 2021-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -46,6 +46,7 @@ files:
46
46
  - config/routes.rb
47
47
  - lib/kamiliff.rb
48
48
  - lib/kamiliff/engine.rb
49
+ - lib/kamiliff/services/liff_base64_service.rb
49
50
  - lib/kamiliff/services/liff_service.rb
50
51
  - lib/kamiliff/version.rb
51
52
  - lib/tasks/kamiliff_tasks.rake