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_button(button_content) ⇒ Object
- #add_to_chunks ⇒ Object
- #bot_class ⇒ Object
-
#button(text, options = {}) ⇒ Object
TODO: High - buttons need their own module.
-
#buttons(&block) ⇒ Object
Build a button template message.
-
#call_button(text, number:)
Add a call button to a card.
- #card(&block) ⇒ Object
- #chunk(chunk_class) ⇒ Object
-
#default_action(url, webview_height_ratio = 'full')
Add a default action (link to navigate to when image is tapped) to the card.
- #gallery(&block) ⇒ Object
- #image(url) ⇒ Object
- #image_url(url, options = {}) ⇒ Object
- #include_chunks ⇒ Object
- #item_url(url) ⇒ Object
-
#log_in(url) ⇒ Object
TODO: should log_in and log_out be log_in_button and log_out_button?.
- #log_out ⇒ Object
- #quick_replies(*classes) ⇒ Object
- #quick_reply(title, options = {}) ⇒ Object
-
#quick_reply_location_request ⇒ Object
Generate a quick reply button that requests the user's location.
- #ref_link(page_id, payload) ⇒ Object
-
#share_button(&block)
Add a share button to a card.
- #show_typing(recipient) ⇒ Object
- #subtitle(text) ⇒ Object
- #t(*args) ⇒ Object
-
#text(message)
Add text message to chunk.
- #title(text) ⇒ Object
- #video(url) ⇒ Object
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
83 84 85 |
# File 'lib/hg/chunk.rb', line 83 def context @context end |
#deliverables ⇒ Object
Returns the value of attribute deliverables.
80 81 82 |
# File 'lib/hg/chunk.rb', line 80 def deliverables @deliverables end |
#dynamic(&block) ⇒ Object
Returns the value of attribute dynamic.
84 85 86 |
# File 'lib/hg/chunk.rb', line 84 def dynamic @dynamic end |
#id ⇒ Object
Returns the value of attribute id.
79 80 81 |
# File 'lib/hg/chunk.rb', line 79 def id @id end |
#label(text) ⇒ Object
Returns the value of attribute label.
81 82 83 |
# File 'lib/hg/chunk.rb', line 81 def label @label end |
#recipient ⇒ Object
Returns the value of attribute recipient.
82 83 84 |
# File 'lib/hg/chunk.rb', line 82 def recipient @recipient end |
Instance Method Details
#add_button(button_content) ⇒ Object
323 324 325 326 327 |
# File 'lib/hg/chunk.rb', line 323 def () @card[:buttons] = [] unless @card[:buttons] @card[:buttons] << end |
#add_to_chunks ⇒ Object
94 95 96 |
# File 'lib/hg/chunk.rb', line 94 def add_to_chunks bot_class.chunks << self end |
#bot_class ⇒ Object
86 87 88 |
# File 'lib/hg/chunk.rb', line 86 def bot_class Kernel.const_get(self.to_s.split('::').first) end |
#button(text, options = {}) ⇒ Object
TODO: High - buttons need their own module
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/hg/chunk.rb', line 273 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] = JSON.generate({ action: Hg::InternalActions::DISPLAY_CHUNK, parameters: { chunk: [: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]) elsif [:payload] [:type] = 'postback' # Encode the payload hash as JSON. [:payload] = JSON.generate([:payload]) end # Pass through the `webview_height_ratio` option. [:webview_height_ratio] = [:webview_height_ratio] () end |
#buttons(&block) ⇒ Object
Build a button template message. See https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/hg/chunk.rb', line 183 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 |
#call_button(text, number:)
This method returns an undefined value.
Add a call button to a card.
225 226 227 228 229 230 231 |
# File 'lib/hg/chunk.rb', line 225 def (text, number:) ({ type: 'phone_number', title: text, payload: number }) end |
#card(&block) ⇒ Object
380 381 382 383 384 |
# File 'lib/hg/chunk.rb', line 380 def card(&block) @card = {} yield @gallery[:cards] << @card end |
#chunk(chunk_class) ⇒ Object
415 416 417 |
# File 'lib/hg/chunk.rb', line 415 def chunk(chunk_class) @deliverables << chunk_class end |
#default_action(url, webview_height_ratio = 'full')
This method returns an undefined value.
Add a default action (link to navigate to when image is tapped) to the card.
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/hg/chunk.rb', line 149 def default_action(url, webview_height_ratio = 'full') # TODO: This should be a private method check that runs anywhere we # use this option. unless %w[compact tall full].any? { |r| r == webview_height_ratio } raise ArgumentError, 'webview_height_ratio must be one of "compact", "tall", or "full"' end @card[:default_action] = { type: 'web_url', url: url, webview_height_ratio: webview_height_ratio.to_s } end |
#gallery(&block) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/hg/chunk.rb', line 386 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(url) ⇒ Object
407 408 409 |
# File 'lib/hg/chunk.rb', line 407 def image(url) ('image', url) end |
#image_url(url, options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/hg/chunk.rb', line 163 def image_url(url, = {}) if .has_key?(:host) @card[:image_url] = ApplicationController.helpers.image_url(url, ) else @card[:image_url] = url end # If aspect ratio changed to square from the horizontal default if [:square_aspect_ratio] set_square_image_ratio end end |
#include_chunks ⇒ Object
98 99 100 |
# File 'lib/hg/chunk.rb', line 98 def include_chunks bot_class.class_eval "include #{bot_class.to_s}::Chunks" end |
#item_url(url) ⇒ Object
177 178 179 |
# File 'lib/hg/chunk.rb', line 177 def item_url(url) @card[:item_url] = url end |
#log_in(url) ⇒ Object
TODO: should log_in and log_out be log_in_button and log_out_button?
207 208 209 |
# File 'lib/hg/chunk.rb', line 207 def log_in(url) nil, url: url, type: 'account_link' end |
#log_out ⇒ Object
211 212 213 |
# File 'lib/hg/chunk.rb', line 211 def log_out nil, type: 'account_unlink' end |
#quick_replies(*classes) ⇒ Object
329 330 331 332 333 |
# File 'lib/hg/chunk.rb', line 329 def quick_replies(*classes) classes.each do |klass| quick_reply klass.instance_variable_get(:@label), to: klass end end |
#quick_reply(title, options = {}) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/hg/chunk.rb', line 335 def quick_reply(title, = {}) quick_reply_content = { content_type: 'text', title: title } # If image_url is specified include the url or asset path if [:image_path] quick_reply_content[:image_url] = ApplicationController .helpers .image_url([:image_path], host: [:host]) else quick_reply_content[:image_url] = [:image_url] end # If a `to` option is present, assume this is a postback link to another chunk. if [:to] quick_reply_content[:payload] = JSON.generate({ action: Hg::InternalActions::DISPLAY_CHUNK, parameters: { chunk: [:to].to_s } }) # If this is a location request, send the appropriate options. elsif [:location_request] quick_reply_content[:content_type] = 'location' quick_reply_content[:title] = nil # Otherwise, just take the payload as passed. else quick_reply_content[:payload] = JSON.generate([:payload]) end unless @deliverables.last[:message][:quick_replies] @deliverables.last[:message][:quick_replies] = [] end @deliverables.last[:message][:quick_replies] << quick_reply_content end |
#quick_reply_location_request ⇒ Object
Generate a quick reply button that requests the user's location.
376 377 378 |
# File 'lib/hg/chunk.rb', line 376 def quick_reply_location_request quick_reply nil, location_request: true end |
#ref_link(page_id, payload) ⇒ Object
423 424 425 |
# File 'lib/hg/chunk.rb', line 423 def ref_link(page_id, payload) "http://m.me/#{page_id}?ref=#{URI.encode(payload.to_json)}" end |
#share_button(&block)
This method returns an undefined value.
Add a share button to a card.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/hg/chunk.rb', line 238 def (&block) = { type: 'element_share' } # If a chunk is passed, add as share_contents option. if block_given? original_gallery = @gallery.clone original_card = @card.clone @gallery = { cards: [], attachment: { type: 'template', payload: { template_type: 'generic', elements: [] } } } card(&block) @gallery[:attachment][:payload][:elements] = @gallery.delete(:cards) [:share_contents] = @gallery.clone @gallery = original_gallery @card = original_card end () end |
#show_typing(recipient) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/hg/chunk.rb', line 108 def show_typing(recipient) Facebook::Messenger::Bot.deliver({ recipient: recipient, sender_action: 'typing_on' }, access_token: ENV['FB_ACCESS_TOKEN']) end |
#subtitle(text) ⇒ Object
137 138 139 |
# File 'lib/hg/chunk.rb', line 137 def subtitle(text) @card[:subtitle] = text end |
#t(*args) ⇒ Object
419 420 421 |
# File 'lib/hg/chunk.rb', line 419 def t(*args) I18n.t(*args) end |
#text(message)
This method returns an undefined value.
Add text message to chunk
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/hg/chunk.rb', line 122 def text() # Sample if message is an array = .sample if .respond_to?(:sample) @deliverables << { message: { text: } } end |
#title(text) ⇒ Object
133 134 135 |
# File 'lib/hg/chunk.rb', line 133 def title(text) @card[:title] = text end |
#video(url) ⇒ Object
411 412 413 |
# File 'lib/hg/chunk.rb', line 411 def video(url) ('video', url) end |