kamiflex 0.12.2 → 0.17.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: abe7d79a2da6e406553d0ac0affcf3d68e8bfc11eeb708b0491b76da8181a29a
4
- data.tar.gz: 4f17979f04de0cbf1ac0239800c16a12b6ec40280c4d923a78e4fe1de4ecd99b
3
+ metadata.gz: 3aebab2a944c48f712b4a67eb0e647f681752c5e6a12d0ad0643dcf94aef3100
4
+ data.tar.gz: 1cfcd6b3b0608f67dd4194544b77040d9637a5dcb3a94408163c0a9f536c9326
5
5
  SHA512:
6
- metadata.gz: a7d6689a1430d5f072cd4cf98ce57b94a6ce08f0f67e95e053328ca8c4f1a885ee17393a20d457f80a803d002cdde8aacc39419198a0928a83f6767f5849c656
7
- data.tar.gz: b6ba2c19320da472a198382773329254d10a1900b7e281d8f848776dc818a04e4cab47eb4213afbc0f41031d2884c250db194ddf56e11be5130f639399e5d5e0
6
+ metadata.gz: 25a72620cf4bc5a5f06ab54254eb4697c0fc212b7946d924a61a09232123a75dae3d6a07db165f46f2631dcb7b90f34ba4902d819f385add4e80fedb48aa1968
7
+ data.tar.gz: fd76b82aa9368e3a00cc3c38aae6bf73b07460d067e9258ad17a36eac286b21f7995794ebbb3ce2bfb1d97c1f63ec6d038b8a015c2c14bfc5d365de3a8942bc7
data/README.md CHANGED
@@ -1,44 +1,81 @@
1
1
  # Kamiflex
2
2
  Kamiflex provide a pretty DSL to build your [flex message of line messaging api](https://developers.line.biz/en/docs/messaging-api/using-flex-messages/) like this:
3
3
 
4
+
5
+ # Live Demo
6
+
7
+ [Kamiflex Simulator](https://kamiflex.etrex.tw/)
8
+
4
9
  #### in pure ruby
10
+
11
+ # [example/show_cases/cards.rb](example/show_cases/cards.rb)
12
+
5
13
  ``` ruby
6
- # example/todos_index.rb
7
14
  require 'kamiflex'
8
15
 
9
- @todos = [
16
+ @products = [
10
17
  {
11
- id: 1,
12
- name: "ruby"
18
+ name: "kamigo",
19
+ star: 4,
20
+ place: "https://github.com/etrex/kamigo",
21
+ time: "10:00 - 23:00",
22
+ contact_url: "https://github.com/etrex/kamigo",
23
+ product_url: "https://github.com/etrex/kamigo"
13
24
  },
14
25
  {
15
- id: 2,
16
- name: "rails"
26
+ name: "kamiliff",
27
+ star: 3,
28
+ place: "https://github.com/etrex/kamiliff",
29
+ time: "11:00 - 23:00",
30
+ contact_url: "https://github.com/etrex/kamiliff",
31
+ product_url: "https://github.com/etrex/kamiliff"
17
32
  },
18
33
  {
19
- id: 3,
20
- name: "kamiflex"
21
- }
34
+ name: "kamiflex",
35
+ star: 5,
36
+ place: "https://github.com/etrex/kamiflex",
37
+ time: "09:00 - 23:00",
38
+ contact_url: "https://github.com/etrex/kamiflex",
39
+ product_url: "https://github.com/etrex/kamiflex"
40
+ },
22
41
  ]
23
42
 
43
+ def star(num)
44
+ baseline_box margin: :md do
45
+ (0...num).each do
46
+ icon "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png", size: :sm
47
+ end
48
+ (num...5).each do
49
+ icon "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png", size: :sm
50
+ end
51
+ text "#{num}.0", size: :sm, color: "#999999", margin: :md, flex: 0
52
+ end
53
+ end
54
+
55
+ def field(key, value)
56
+ baseline_box spacing: :sm do
57
+ text key, "color":"#aaaaaa","size":"sm","flex":1
58
+ text value, "wrap":true,"color":"#666666","size":"sm","flex":5
59
+ end
60
+ end
61
+
24
62
  json = Kamiflex.build(self) do
25
- bubble do
26
- body do
27
- horizontal_box do
28
- text "🍔", flex: 0, action: message_action("/")
29
- text "Todos"
30
- text "🆕", align: "end", action: uri_action(new_todo_path))
31
- end
32
- separator
33
- if @todos.present?
34
- vertical_box margin: "lg" do
35
- horizontal_box @todos, margin: "lg" do |todo|
36
- text todo[:name], action: message_action("/todos/#{todo[:id]}")
37
- text "❌", align: "end", action: message_action("DELETE /todos/#{todo[:id]}")
38
- end
63
+ carousel do
64
+ bubbles @products do |product|
65
+ hero "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_1_cafe.png",
66
+ size: :full, aspectRatio: "20:13", aspectMode: :cover, action: uri_action("http://linecorp.com/")
67
+ body do
68
+ text product[:name], weight: :bold, size: :xl
69
+ star(product[:star])
70
+ vertical_box margin: :lg, spacing: :sm do
71
+ field "Place", product[:place]
72
+ field "Time", product[:time]
39
73
  end
40
- else
41
- text "no contents yet", margin: "lg"
74
+ end
75
+ footer spacing: :sm, flex: 0 do
76
+ url_button "CALL", product[:contact_url], style: :link, height: :sm
77
+ url_button "WEBSITE", product[:product_url], style: :link, height: :sm
78
+ spacer size: :sm
42
79
  end
43
80
  end
44
81
  end
@@ -47,20 +84,18 @@ end
47
84
  puts json
48
85
  ```
49
86
 
50
- The render result looks like this:
51
-
52
- ![](image/todos_index.png)
87
+ ![](image/show_cases/cards.png)
53
88
 
54
89
  #### in rails
55
90
  ``` ruby
56
91
  # todos/index.line.erb
57
- <%= raw(Kamiflex.build(self) do
92
+ <%= raw Kamiflex.build(self) do
58
93
  bubble do
59
94
  body do
60
95
  horizontal_box do
61
96
  text "🍔", flex: 0, action: message_action("/")
62
97
  text "Todos"
63
- text "🆕", align: "end", action: uri_action(new_todo_path))
98
+ text "🆕", align: "end", action: uri_action(new_todo_path)
64
99
  end
65
100
  separator
66
101
  if @todos.present?
@@ -75,7 +110,7 @@ The render result looks like this:
75
110
  end
76
111
  end
77
112
  end
78
- end )%>
113
+ end %>
79
114
  ```
80
115
 
81
116
  I will make a template name `flex` for rails in the future.
@@ -89,7 +124,7 @@ bubble do
89
124
  horizontal_box do
90
125
  text "🍔", flex: 0, action: message_action("/")
91
126
  text "Todos"
92
- text "🆕", align: "end", action: uri_action(new_todo_path))
127
+ text "🆕", align: "end", action: uri_action(new_todo_path)
93
128
  end
94
129
  separator
95
130
  if @todos.present?
data/lib/kamiflex.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "json"
2
+ require_relative "./kamiflex/version"
2
3
  require_relative "./kamiflex/core"
3
4
  require_relative "./kamiflex/basic_elements"
4
5
  require_relative "./kamiflex/actions"
@@ -6,7 +7,7 @@ require_relative "./kamiflex/quick_reply"
6
7
  require_relative "./kamiflex/custom"
7
8
 
8
9
  module Kamiflex
9
- def self.to_hash(parent)
10
+ def self.hash(parent, &block)
10
11
  parent.class.include Kamiflex::Core
11
12
  parent.class.include Kamiflex::BasicElements
12
13
  parent.class.include Kamiflex::Actions
@@ -15,24 +16,25 @@ module Kamiflex
15
16
 
16
17
  parent.instance_exec do
17
18
  flex do
18
- yield
19
+ parent.instance_exec(&block)
19
20
  end
20
21
  end
21
22
  end
22
23
 
23
- def self.build(parent)
24
- parent.class.include Kamiflex::Core
25
- parent.class.include Kamiflex::BasicElements
26
- parent.class.include Kamiflex::Actions
27
- parent.class.include Kamiflex::QuickReply
28
- parent.class.include Kamiflex::Custom
24
+ def self.to_hash(parent, &block)
25
+ self.hash(parent, &block)
26
+ end
29
27
 
30
- hash = parent.instance_exec do
31
- flex do
32
- yield
33
- end
34
- end
35
- JSON.pretty_generate hash
28
+ def self.build(parent, &block)
29
+ JSON.pretty_generate self.hash(parent, &block)
30
+ end
31
+
32
+ def self.json(parent, &block)
33
+ self.build(parent, &block)
34
+ end
35
+
36
+ def self.compact_json(parent, &block)
37
+ self.to_hash(parent, &block).to_json
36
38
  end
37
39
  end
38
40
 
@@ -4,19 +4,25 @@ module Kamiflex
4
4
  {
5
5
  type: "message",
6
6
  label: label,
7
- text: label
7
+ text: params[:text] || label
8
8
  }.merge(params)
9
9
  end
10
10
 
11
11
  def uri_action(uri, **params)
12
- {
12
+ action = {
13
13
  type: "uri",
14
14
  label: uri[0...40],
15
- uri: uri,
16
- # altUri: {
17
- # desktop: uri
18
- # }
15
+ uri: uri
19
16
  }
17
+
18
+ return action if params.nil?
19
+ return action if params[:desktop].nil?
20
+
21
+ action[:altUri] = {
22
+ desktop: params[:desktop]
23
+ }
24
+
25
+ action
20
26
  end
21
27
 
22
28
  def postback_action(data, **params)
@@ -1,8 +1,23 @@
1
1
  module Kamiflex
2
2
  module BasicElements
3
- def text(message, **params)
3
+ def text(message = nil, **params)
4
+ if block_given?
5
+ _attributes, contents = flex_scope{ yield }
6
+ @flex_contents << {
7
+ type: "text",
8
+ contents: contents,
9
+ }.merge(params)
10
+ else
11
+ @flex_contents << {
12
+ type: "text",
13
+ text: message
14
+ }.merge(params)
15
+ end
16
+ end
17
+
18
+ def span(message, **params)
4
19
  @flex_contents << {
5
- "type": "text",
20
+ "type": "span",
6
21
  "text": message
7
22
  }.merge(params)
8
23
  end
@@ -43,13 +58,22 @@ module Kamiflex
43
58
  end
44
59
 
45
60
  def url_button(label, url, **params)
46
- @flex_contents << {
47
- "type": "button",
48
- "action": {
49
- "type": "uri",
50
- "label": label,
51
- "uri": url
61
+ action = {
62
+ type: "uri",
63
+ label: label,
64
+ uri: url,
65
+ }
66
+
67
+ if params&.dig(:desktop)
68
+ action[:altUri] = {
69
+ desktop: params&.dig(:desktop)
52
70
  }
71
+ params = params.reject {|key, value| key == :desktop }
72
+ end
73
+
74
+ @flex_contents << {
75
+ type: "button",
76
+ action: action
53
77
  }.merge(params)
54
78
  end
55
79
 
data/lib/kamiflex/core.rb CHANGED
@@ -82,11 +82,11 @@ module Kamiflex
82
82
  # container
83
83
  def horizontal_box(resources = [nil], **params)
84
84
  resources.each_with_index do |resource, index|
85
- _attributes, contents = flex_scope{ yield(resource, index) }
85
+ _attributes, contents = flex_scope{ yield(resource, index) } if block_given?
86
86
  @flex_contents << {
87
87
  type: "box",
88
88
  layout: "horizontal",
89
- contents: contents
89
+ contents: contents || []
90
90
  }.merge(params)
91
91
  end
92
92
  end
@@ -1,3 +1,3 @@
1
1
  module Kamiflex
2
- VERSION = '0.12.2'
2
+ VERSION = '0.17.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kamiflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.17.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-10-27 00:00:00.000000000 Z
11
+ date: 2021-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json