Module: Hg::Chunk::ClassMethods

Defined in:
lib/hg/chunk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context.



83
84
85
# File 'lib/hg/chunk.rb', line 83

def context
  @context
end

#deliverablesObject

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

#idObject

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

#recipientObject

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 add_button(button_content)
  @card[:buttons] = [] unless @card[:buttons]

  @card[:buttons] << button_content
end

#add_to_chunksObject



94
95
96
# File 'lib/hg/chunk.rb', line 94

def add_to_chunks
  bot_class.chunks << self
end

#bot_classObject



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 button(text, options = {})
  # 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)

    button_content = {
      title: text,
      type: 'postback',
      payload: klass.to_s
    }
  else
    button_content = {
      title: text
    }
  end

  # If a `to` option is present, assume this is a postback link to another chunk.
  if options[:to]
    button_content[:type] = 'postback'
    button_content[:payload] = JSON.generate({
      action: Hg::InternalActions::DISPLAY_CHUNK,
      parameters: {
        chunk: options[:to].to_s
      }
    })
  # If a different type of button is specified (e.g. "Log in"), then pass
  # through the `type` and `url`.
  elsif options[:type]
    button_content[:type] = options[:type]

    button_content[:url] = evaluate_option(options[:url])
  # If a `url` option is present, assume this is a webview link button.
  elsif options[:url]
    button_content[:type] = 'web_url'

    button_content[:url] = evaluate_option(options[:url])
  elsif options[:payload]
    button_content[:type] = 'postback'
    # Encode the payload hash as JSON.
    button_content[:payload] = JSON.generate(options[:payload])
  end

  # Pass through the `webview_height_ratio` option.
  button_content[:webview_height_ratio] = options[:webview_height_ratio]

  add_button(button_content)
end

#buttons(&block) ⇒ Object



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 buttons(&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.

Parameters:

  • text (String)

    The text to appear in the button.

  • number (String)

    The number to call when the button is pressed.

See Also:



225
226
227
228
229
230
231
# File 'lib/hg/chunk.rb', line 225

def call_button(text, number:)
  add_button({
    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.

Parameters:

  • url (String)

    The URL to navigate to.

  • webview_height_ratio (String|Symbol) (defaults to: 'full')

    The height of the webview to open (compact, tall, or full).



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


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)
  attachment('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, options = {})
  if options.has_key?(:host)
    @card[:image_url] =
      ApplicationController.helpers.image_url(url, options)
  else
    @card[:image_url] = url
  end

  # If aspect ratio changed to square from the horizontal default
  if options[:square_aspect_ratio]
    set_square_image_ratio
  end
end

#include_chunksObject



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 (url)
  button nil, url: url, type: 'account_link'
end

#log_outObject



211
212
213
# File 'lib/hg/chunk.rb', line 211

def log_out
  button 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, options = {})
  quick_reply_content = {
    content_type: 'text',
    title: title
  }

  # If image_url is specified include the url or asset path
  if options[:image_path]
    quick_reply_content[:image_url] =
      ApplicationController
        .helpers
        .image_url(options[:image_path], host: options[:host])
  else
    quick_reply_content[:image_url] = options[:image_url]
  end

  # If a `to` option is present, assume this is a postback link to another chunk.
  if options[:to]
    quick_reply_content[:payload] = JSON.generate({
      action: Hg::InternalActions::DISPLAY_CHUNK,
      parameters: {
        chunk: options[:to].to_s
      }
    })
  # If this is a location request, send the appropriate options.
  elsif options[: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(options[: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_requestObject

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


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 share_button(&block)
  button_options = {
    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)

    button_options[:share_contents] = @gallery.clone

    @gallery = original_gallery
    @card = original_card
  end

  add_button(button_options)
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

Parameters:

  • message (String, Array)

    Message to be delivered. If message is an array, will deliver one at random



122
123
124
125
126
127
128
129
130
131
# File 'lib/hg/chunk.rb', line 122

def text(message)
  # Sample if message is an array
  message = message.sample if message.respond_to?(:sample)
  @deliverables <<
    {
      message: {
        text: message
      }
    }
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)
  attachment('video', url)
end