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.



53
54
55
# File 'lib/hg/chunk.rb', line 53

def context
  @context
end

#deliverablesObject

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

#idObject

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

#recipientObject

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_chunksObject



68
69
70
# File 'lib/hg/chunk.rb', line 68

def add_to_chunks
  bot_class.chunks << self
end

#add_to_routerObject



64
65
66
# File 'lib/hg/chunk.rb', line 64

def add_to_router
  bot_class.routes.merge(@id.to_sym => self)
end

#bot_classObject



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 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] = 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])
  end

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

  @card[:buttons] = [] unless @card[:buttons]

  @card[:buttons] << button_content
end

#buttons(&block) ⇒ Object



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 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

#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


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_chunksObject



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 (url)
  button 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, options = {})
  quick_reply_content = {
    content_type: 'text',
    title: title,
    payload: options[: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(message)
  @deliverables <<
    {
      message: {
        text: message
      }
    }
end

#title(text) ⇒ Object



104
105
106
# File 'lib/hg/chunk.rb', line 104

def title(text)
  @card[:title] = text
end