kamiliff 0.27.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: 38aca00ff3bcedc33c8e81f3c2067db8aeba06020701d93b03e8c6927b12267e
4
- data.tar.gz: 7a5eeece3bc9a2530d568b9b50c99e8fd39e77b4f48ee79a59e85b1cf07811d6
3
+ metadata.gz: ab005f01ac6af835748c47ef0c94a5036f7da0d0ed2e250750adaee470f7cf4d
4
+ data.tar.gz: deba6685fa6388fc17b0b97ee26a0980ddd686b55b180c56b9a93527b27cde36
5
5
  SHA512:
6
- metadata.gz: fcded347d34ad824c20dc0e481d46e5ee376140f049a7d3fe603d6ea71dd9662bcfe9d09bd4d9cb2d3a2ad384f8503f4bfaeb22529a5a040a09e570bc99410a0
7
- data.tar.gz: 71bbd81ebc6963ddd9dca6244803e62c4bbbce31ec4479b7f7fa3d948c8fa2ce7e397ebc225d10f832d6ee3ba6f744c2c7e45d900143f6885db17ab89bb32b4e
6
+ metadata.gz: c84c268fcad2335741d7fb4e0c80bcc7997d895ceda2b81263f72932900ac679211e5ab1407746b63aca9545947657c0f249dbe2f4426ea9e0839f9a70860e52
7
+ data.tar.gz: 9d2be0ac73c44d9ae24608d12eab10351d8ce7c4dd413e46f45962637b832691513154cf842a32bf2f2f6ead904e8df9567da64e65927c2badf8a01dfe102bd4
@@ -1,41 +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?
10
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
+
11
33
  # 第一次 redirect
12
34
  if(@need_reload)
35
+ # base64 的情況
13
36
  if(query["liff.state"][0] == '/')
14
- @liff = LiffBase64Service.from_base64(query["liff.state"][1..-1])
37
+ base64_string = query["liff.state"][1..-1]
38
+ @liff_param = Base64DecodeService.new(base64_string).run
39
+ # query 的情況
15
40
  else
16
41
  querystring = query["liff.state"][(query["liff.state"].index('?')+1)..-1]
17
- query = Rack::Utils.parse_nested_query(querystring)
18
- @liff = LiffService.new(query)
42
+ @liff_param = Rack::Utils.parse_nested_query(querystring)
19
43
  end
20
44
  # 第二次 redirect
21
45
  else
46
+ # base64 的情況
22
47
  if params[:base64].present?
23
- @liff = LiffBase64Service.from_base64(params[:base64])
48
+ base64_string = params[:base64]
49
+ @liff_param = Base64DecodeService.new(base64_string).run
50
+ # query 的情況
24
51
  else
25
- @liff = LiffService.new(query)
52
+ @liff_param = query
26
53
  end
27
54
  end
28
- end
29
55
 
30
- def route
31
- path, query = params["path"].split("?")
32
- query = Rack::Utils.parse_nested_query(query)
33
- http_method = query["_method"]&.upcase || "GET"
34
- @body = reserve_route(path, http_method: http_method, request_params: source_info.merge(query), format: :liff)
56
+ # 保存最後一次 liff_param 到 session
57
+ session[:liff_param] = Base64EncodeService.new(@liff_param).run
35
58
  end
36
59
 
37
- private
38
-
39
60
  def reserve_route(path, http_method: "GET", request_params: nil, format: nil)
40
61
  request.request_method = http_method
41
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
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.27.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.27.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-06-10 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
- 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