kamiliff 0.24.1 → 0.29.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41f6d9fc6bfe8a13690ec161fbed9dac12cdbe36967a5a3c80dca4d7b16d4000
4
- data.tar.gz: 532ad15c5e760ee8dc13409d9a5ee7907be83c25efcd948c64079468e34abff0
3
+ metadata.gz: a255c2f6b73407c86e8f993716595072c3dc1c8c8a655df0b626c13719eae8d0
4
+ data.tar.gz: 49b62ebd406218e682cb1fe2b68c7300695c53392cdce0938bc56fceb7c90e9a
5
5
  SHA512:
6
- metadata.gz: 1e78a0aedee965fbea3dcb845b7ddaaf78cf01fa96c1cbd84c3fb2de8c07b5f1fe5f8e6b1062a971789b4489175634977f3b0cdfa1e2f20c4d167ac58190b23a
7
- data.tar.gz: 3b14acd351d07c7ea43431c77c66925cfc26a1945c644ebed7f2bb4a401f05236df54d18b1ffdfc8cb541f8718e033576351204f9925536428987da7fb6794bd
6
+ metadata.gz: e9a989ee028a5e130b7ba041e79a57279510e39672411263c5087bb162603d8ccc04bf46f9ef03c7f0e11007f1b6707f2e9b208d6d031d4b53a50aa58e1d327b
7
+ data.tar.gz: '09fd775c392002cf34d7998ec020f779faa7a00fd977270056ac77ab786507cedc5187a6c2be1ebfe693389635a18fa5b815ef206af9791a72e5ff712533903f'
@@ -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
@@ -0,0 +1,31 @@
1
+ <script>
2
+ (function(){
3
+ async function share_target_picker(messages){
4
+ /* JS 不支援的情況 */
5
+ if (!liff.isApiAvailable('shareTargetPicker')){
6
+ return;
7
+ }
8
+
9
+ /* 分享訊息給好友 */
10
+ const response = await liff.shareTargetPicker(messages);
11
+
12
+ /* 分享訊息失敗 */
13
+ if(!response){
14
+ const [majorVer, minorVer] = (liff.getLineVersion() || "").split('.');
15
+ if (parseInt(majorVer) == 10 && parseInt(minorVer) < 11) {
16
+ alert("你的 LINE 版本太舊,無法使用分享訊息功能。");
17
+ } else {
18
+ alert("你已取消分享訊息。");
19
+ }
20
+ }
21
+ }
22
+
23
+ async function send_messages(){
24
+ const messages = <%= raw messages %>;
25
+ await share_target_picker(messages);
26
+ liff.closeWindow();
27
+ }
28
+
29
+ send_messages();
30
+ })();
31
+ </script>
@@ -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.1'
2
+ VERSION = '0.29.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.1
4
+ version: 0.29.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-31 00:00:00.000000000 Z
11
+ date: 2021-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -39,6 +39,7 @@ files:
39
39
  - app/helpers/liff_helper.rb
40
40
  - app/views/layouts/application.liff.erb
41
41
  - app/views/layouts/liff.html.erb
42
+ - app/views/liff/_share_target_picker.liff.erb
42
43
  - app/views/liff/entry.html.erb
43
44
  - app/views/liff/route.html.erb
44
45
  - config/initializers/assets.rb
@@ -46,7 +47,8 @@ files:
46
47
  - config/routes.rb
47
48
  - lib/kamiliff.rb
48
49
  - lib/kamiliff/engine.rb
49
- - lib/kamiliff/services/liff_base64_service.rb
50
+ - lib/kamiliff/services/base64_decode_service.rb
51
+ - lib/kamiliff/services/base64_encode_service.rb
50
52
  - lib/kamiliff/services/liff_service.rb
51
53
  - lib/kamiliff/version.rb
52
54
  - 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('/','_').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