Class: LiffBase64Service

Inherits:
Object
  • Object
show all
Defined in:
lib/kamiliff/services/liff_base64_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ LiffBase64Service

Returns a new instance of LiffBase64Service.



35
36
37
38
39
40
41
42
43
44
# File 'lib/kamiliff/services/liff_base64_service.rb', line 35

def initialize(options)
  options = options.with_indifferent_access
  self.path = options[:path] || "/"
  self.size = options[:size] || :compact
  self.size = size.to_s.upcase
  raise "liff_size should be compact, tall or full." unless size.in? %w[COMPACT TALL FULL]
  self.url = ENV["LIFF_#{size}"]
  raise "LIFF_#{size} should be in the env variables" if url.blank?
  self.id = url[(url.rindex('/')+1)..-1]
end

Instance Attribute Details

#idObject

liff id



17
18
19
# File 'lib/kamiliff/services/liff_base64_service.rb', line 17

def id
  @id
end

#pathObject

rails routes path



6
7
8
# File 'lib/kamiliff/services/liff_base64_service.rb', line 6

def path
  @path
end

#sizeObject

size COMPACT TALL FULL



10
11
12
# File 'lib/kamiliff/services/liff_base64_service.rb', line 10

def size
  @size
end

#urlObject

liff app url liff.line.me/app/#liff_id



14
15
16
# File 'lib/kamiliff/services/liff_base64_service.rb', line 14

def url
  @url
end

Class Method Details

.from_base64(base64_string) ⇒ Object



19
20
21
22
23
24
# File 'lib/kamiliff/services/liff_base64_service.rb', line 19

def self.from_base64(base64_string)
  base64_string = base64_string.tr('-','+').tr('_','/')
  json = Base64.decode64(base64_string)
  options = JSON.parse(json)
  LiffBase64Service.new(options)
end

Instance Method Details

#full_urlObject



46
47
48
49
50
51
# File 'lib/kamiliff/services/liff_base64_service.rb', line 46

def full_url
  if ENV["LIFF_MODE"]&.downcase == "replace"
  end
  # liff mode is Concatenate
  "#{url}/#{to_base64}"
end

#to_base64Object



26
27
28
29
30
31
32
33
# File 'lib/kamiliff/services/liff_base64_service.rb', line 26

def to_base64
  json = {
    path: path,
    size: size
  }.to_json
  base64_string = Base64.encode64(json)
  base64_string.tr('+','-').tr('/','_').tr("\n", '').tr('=', '')
end