kamiliff 0.24.0 → 0.28.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: 3189c0303b55fd1e7d8ed2cc55f613544f8bdd9db48f34cf526baec151044afe
4
- data.tar.gz: 5204d91c73e59aec7386a07f1f25d7ef4f1a5b6d9f5eb81d032faf91ac0e94cb
3
+ metadata.gz: ab005f01ac6af835748c47ef0c94a5036f7da0d0ed2e250750adaee470f7cf4d
4
+ data.tar.gz: deba6685fa6388fc17b0b97ee26a0980ddd686b55b180c56b9a93527b27cde36
5
5
  SHA512:
6
- metadata.gz: ba41019d9be741d925eacf2451a16e3d4486a7404f2a2ed696eaed173b4f99f32bf235d4360331213113ba4a84a8de4ebccd7e98847d7f0673b302313c67bd2a
7
- data.tar.gz: 226c181fd2fb67213dd105c984a1d251ccf58a3eb33645f2b0bdda5092c280810d91c9e09cd8e51dcb310f547d9cd48c2dc957a5471e152673334857abe81a6d
6
+ metadata.gz: c84c268fcad2335741d7fb4e0c80bcc7997d895ceda2b81263f72932900ac679211e5ab1407746b63aca9545947657c0f249dbe2f4426ea9e0839f9a70860e52
7
+ data.tar.gz: 9d2be0ac73c44d9ae24608d12eab10351d8ce7c4dd413e46f45962637b832691513154cf842a32bf2f2f6ead904e8df9567da64e65927c2badf8a01dfe102bd4
@@ -1,35 +1,62 @@
1
1
 
2
2
  class LiffController < ActionController::Base
3
3
  layout false, only: :route
4
+ before_action :set_liff_param, only: [:entry]
4
5
 
5
6
  def entry
7
+ @liff = LiffService.new(@liff_param)
8
+ end
9
+
10
+ def route
11
+ path, query = params["path"].split("?")
12
+ query = Rack::Utils.parse_nested_query(query)
13
+ http_method = query["_method"]&.upcase || "GET"
14
+ @body = reserve_route(path, http_method: http_method, request_params: source_info.merge(query), format: :liff)
15
+ end
16
+
17
+ private
18
+
19
+ # {"path"=>"/orders", "liff_size"=>"TALL"}
20
+ def set_liff_param
6
21
  query = Rack::Utils.parse_nested_query(request.query_string)
7
22
 
8
23
  # fix liff 2.0 redirect issue
9
24
  @need_reload = query["liff.state"].present?
25
+
26
+ # 需要從 session 讀取 @liff_param
27
+ if query["liffRedirectUri"].present?
28
+ @liff_param = Base64DecodeService.new(session[:liff_param]).run
29
+ session[:liff_param] = nil
30
+ return
31
+ end
32
+
33
+ # 第一次 redirect
10
34
  if(@need_reload)
11
- if query["liff.state"].index('?').present?
35
+ # base64 的情況
36
+ if(query["liff.state"][0] == '/')
37
+ base64_string = query["liff.state"][1..-1]
38
+ @liff_param = Base64DecodeService.new(base64_string).run
39
+ # query 的情況
40
+ else
12
41
  querystring = query["liff.state"][(query["liff.state"].index('?')+1)..-1]
13
- query = Rack::Utils.parse_nested_query(querystring)
42
+ @liff_param = Rack::Utils.parse_nested_query(querystring)
14
43
  end
15
- end
16
-
17
- # try base64 decode
18
- if params[:base64].present?
19
- @liff = LiffBase64Service.from_base64(params[:base64])
44
+ # 第二次 redirect
20
45
  else
21
- @liff = LiffService.new(query)
46
+ # base64 的情況
47
+ if params[:base64].present?
48
+ base64_string = params[:base64]
49
+ @liff_param = Base64DecodeService.new(base64_string).run
50
+ # query 的情況
51
+ else
52
+ @liff_param = query
53
+ end
22
54
  end
23
- end
24
55
 
25
- def route
26
- path, query = params["path"].split("?")
27
- query = Rack::Utils.parse_nested_query(query)
28
- @body = reserve_route(path, request_params: source_info.merge(query), format: :liff)
56
+ # 保存最後一次 liff_param 到 session
57
+ session[:liff_param] = Base64EncodeService.new(@liff_param).run
29
58
  end
30
59
 
31
- private
32
-
33
60
  def reserve_route(path, http_method: "GET", request_params: nil, format: nil)
34
61
  request.request_method = http_method
35
62
  request.path_info = path
@@ -9,7 +9,7 @@ module LiffHelper
9
9
  end
10
10
 
11
11
  def liff_path(params)
12
- liff = LiffBase64Service.new(params)
12
+ liff = LiffService.new(params)
13
13
  liff.full_url
14
14
  end
15
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/lib/kamiliff.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "kamiliff/engine"
2
+ require "kamiliff/services/base64_encode_service"
3
+ require "kamiliff/services/base64_decode_service"
2
4
  require "kamiliff/services/liff_service"
3
- require "kamiliff/services/liff_base64_service"
4
5
 
5
6
  module Kamiliff
6
7
  # Your code goes here...
@@ -0,0 +1,15 @@
1
+ require "base64"
2
+
3
+ class Base64DecodeService
4
+
5
+ def initialize(base64_string)
6
+ @base64_string = base64_string
7
+ end
8
+
9
+ def run
10
+ json = Base64.decode64(@base64_string)
11
+ options = JSON.parse(json)
12
+ options.with_indifferent_access
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ require "base64"
2
+
3
+ class Base64EncodeService
4
+
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ def run
10
+ json = @options.to_json
11
+ base64_string = Base64.encode64(json)
12
+ base64_string.tr('+','-').tr('/','_').tr("\n", '').tr('=', '')
13
+ end
14
+
15
+ end
@@ -15,12 +15,22 @@ class LiffService
15
15
  attr_accessor :id
16
16
 
17
17
  def initialize(options)
18
- self.path = options[:path] || options['path'] || "/"
19
- self.size = options[:liff_size] || options['liff_size'] || :compact
18
+ self.path = options[:path] || "/"
19
+ self.size = options[:liff_size] || :compact
20
20
  self.size = size.to_s.upcase
21
21
  raise "liff_size should be compact, tall or full." unless size.in? %w[COMPACT TALL FULL]
22
22
  self.url = ENV["LIFF_#{size}"]
23
23
  raise "LIFF_#{size} should be in the env variables" if url.blank?
24
24
  self.id = url[(url.rindex('/')+1)..-1]
25
25
  end
26
+
27
+ def full_url
28
+ # liff mode is Concatenate
29
+ base64_string = Base64EncodeService.new({
30
+ path: path,
31
+ liff_size: size
32
+ }).run
33
+ "#{url}/#{base64_string}"
34
+ end
35
+
26
36
  end
@@ -1,3 +1,3 @@
1
1
  module Kamiliff
2
- VERSION = '0.24.0'
2
+ VERSION = '0.28.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.24.0
4
+ version: 0.28.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: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -46,7 +46,8 @@ 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
+ - lib/kamiliff/services/base64_decode_service.rb
50
+ - lib/kamiliff/services/base64_encode_service.rb
50
51
  - lib/kamiliff/services/liff_service.rb
51
52
  - lib/kamiliff/version.rb
52
53
  - lib/tasks/kamiliff_tasks.rake
@@ -1,52 +0,0 @@
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
- size: size
30
- }.to_json
31
- base64_string = Base64.encode64(json)
32
- base64_string.tr('+','-').tr('/','_')[0..-4]
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