Module: Hg::Chunk::ClassMethods
- Defined in:
- lib/hg/chunk.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#deliverables ⇒ Object
Returns the value of attribute deliverables.
-
#dynamic(&block) ⇒ Object
Returns the value of attribute dynamic.
-
#id ⇒ Object
Returns the value of attribute id.
-
#label(text) ⇒ Object
Returns the value of attribute label.
-
#recipient ⇒ Object
Returns the value of attribute recipient.
Instance Method Summary collapse
- #add_to_chunks ⇒ Object
- #add_to_router ⇒ Object
- #bot_class ⇒ Object
- #button(text, options = {}) ⇒ Object
-
#buttons(&block) ⇒ Object
Build a button template message.
- #card(&block) ⇒ Object
- #chunk(chunk_class) ⇒ Object
- #gallery(&block) ⇒ Object
- #image(path) ⇒ Object
- #image_url(path) ⇒ Object
- #include_chunks ⇒ Object
- #item_url(url) ⇒ Object
- #keywords(*chunk_keywords) ⇒ Object
- #log_in(url) ⇒ Object
- #quick_replies(*classes) ⇒ Object
- #quick_reply(title, options = {}) ⇒ Object
- #show_typing(recipient) ⇒ Object
- #subtitle(text) ⇒ Object
- #text(message) ⇒ Object
- #title(text) ⇒ Object
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
53 54 55 |
# File 'lib/hg/chunk.rb', line 53 def context @context end |
#deliverables ⇒ Object
Returns the value of attribute deliverables.
50 51 52 |
# File 'lib/hg/chunk.rb', line 50 def deliverables @deliverables end |
#dynamic(&block) ⇒ Object
Returns the value of attribute dynamic.
54 55 56 |
# File 'lib/hg/chunk.rb', line 54 def dynamic @dynamic end |
#id ⇒ Object
Returns the value of attribute id.
49 50 51 |
# File 'lib/hg/chunk.rb', line 49 def id @id end |
#label(text) ⇒ Object
Returns the value of attribute label.
51 52 53 |
# File 'lib/hg/chunk.rb', line 51 def label @label end |
#recipient ⇒ Object
Returns the value of attribute recipient.
52 53 54 |
# File 'lib/hg/chunk.rb', line 52 def recipient @recipient end |
Instance Method Details
#add_to_chunks ⇒ Object
68 69 70 |
# File 'lib/hg/chunk.rb', line 68 def add_to_chunks bot_class.chunks << self end |
#add_to_router ⇒ Object
64 65 66 |
# File 'lib/hg/chunk.rb', line 64 def add_to_router bot_class.routes.merge(@id.to_sym => self) end |
#bot_class ⇒ Object
56 57 58 |
# File 'lib/hg/chunk.rb', line 56 def bot_class self.to_s.split('::').first.constantize end |
#button(text, options = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/hg/chunk.rb', line 149 def (text, = {}) # TODO: text needs a better name # If the first argument is a chunk, then make this button a link to that chunk if text.is_a? Class klass = text text = text.instance_variable_get(:@label) = { title: text, type: 'postback', payload: klass.to_s } else = { title: text } end # If a `to` option is present, assume this is a postback link to another chunk. if [:to] [:type] = 'postback' [:payload] = [:to].to_s # If a different type of button is specified (e.g. "Log in"), then pass # through the `type` and `url`. elsif [:type] [:type] = [:type] [:url] = evaluate_option([:url]) # If a `url` option is present, assume this is a webview link button. elsif [:url] [:type] = 'web_url' [:url] = evaluate_option([:url]) end # Pass through the `webview_height_ratio` option. [:webview_height_ratio] = [:webview_height_ratio] @card[:buttons] = [] unless @card[:buttons] @card[:buttons] << end |
#buttons(&block) ⇒ Object
Build a button template message. See developers.facebook.com/docs/messenger-platform/send-api-reference/button-template
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hg/chunk.rb', line 122 def (&block) @card = {} yield deliverable = { message: { attachment: { type: 'template', payload: { template_type: 'button' } } } } # Move buttons to proper location deliverable[:message][:attachment][:payload][:buttons] = @card.delete(:buttons) deliverable[:message][:attachment][:payload][:text] = @deliverables.pop[:message][:text] @deliverables << deliverable end |
#card(&block) ⇒ Object
212 213 214 215 216 |
# File 'lib/hg/chunk.rb', line 212 def card(&block) @card = {} yield @gallery[:cards] << @card end |
#chunk(chunk_class) ⇒ Object
252 253 254 |
# File 'lib/hg/chunk.rb', line 252 def chunk(chunk_class) @deliverables << chunk_class end |
#gallery(&block) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/hg/chunk.rb', line 218 def gallery(&block) @gallery = { cards: [], message: { attachment: { type: 'template', payload: { template_type: 'generic', elements: [] } } } } yield @gallery[:message][:attachment][:payload][:elements] = @gallery.delete(:cards) @deliverables << @gallery end |
#image(path) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/hg/chunk.rb', line 239 def image(path) @deliverables << { message: { attachment: { type: 'image', payload: { url: bot_class.instance_variable_get(:@image_url_base_portion) + path } } } } end |
#image_url(path) ⇒ Object
112 113 114 |
# File 'lib/hg/chunk.rb', line 112 def image_url(path) @card[:image_url] = bot_class.instance_variable_get(:@image_url_base_portion) + path end |
#include_chunks ⇒ Object
72 73 74 |
# File 'lib/hg/chunk.rb', line 72 def include_chunks bot_class.class_eval "include #{bot_class.to_s}::Chunks" end |
#item_url(url) ⇒ Object
116 117 118 |
# File 'lib/hg/chunk.rb', line 116 def item_url(url) @card[:item_url] = url end |
#keywords(*chunk_keywords) ⇒ Object
89 90 91 92 93 |
# File 'lib/hg/chunk.rb', line 89 def keywords(*chunk_keywords) chunk_keywords.each do |keyword| bot_class.routes[keyword] = self end end |
#log_in(url) ⇒ Object
145 146 147 |
# File 'lib/hg/chunk.rb', line 145 def log_in(url) nil, url: url, type: 'account_link' end |
#quick_replies(*classes) ⇒ Object
192 193 194 195 196 |
# File 'lib/hg/chunk.rb', line 192 def quick_replies(*classes) classes.each do |klass| quick_reply klass.instance_variable_get(:@label), to: klass end end |
#quick_reply(title, options = {}) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/hg/chunk.rb', line 198 def quick_reply(title, = {}) quick_reply_content = { content_type: 'text', title: title, payload: [:to].to_s } unless @deliverables.last[:message][:quick_replies] @deliverables.last[:message][:quick_replies] = [] end @deliverables.last[:message][:quick_replies] << quick_reply_content end |
#show_typing(recipient) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/hg/chunk.rb', line 82 def show_typing(recipient) Facebook::Messenger::Bot.deliver({ recipient: recipient, sender_action: 'typing_on' }, access_token: ENV['ACCESS_TOKEN']) end |
#subtitle(text) ⇒ Object
108 109 110 |
# File 'lib/hg/chunk.rb', line 108 def subtitle(text) @card[:subtitle] = text end |
#text(message) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/hg/chunk.rb', line 95 def text() @deliverables << { message: { text: } } end |
#title(text) ⇒ Object
104 105 106 |
# File 'lib/hg/chunk.rb', line 104 def title(text) @card[:title] = text end |