lib/lita/handlers/imgflip_memes.rb in lita-imgflip-memes-1.0.1 vs lib/lita/handlers/imgflip_memes.rb in lita-imgflip-memes-1.1.1

- old
+ new

@@ -9,19 +9,12 @@ config :api_user, default: ENV['IMGFLIP_API_USER'] config :api_password, default: ENV['IMGFLIP_API_PASSWORD'] API_URL = 'https://api.imgflip.com/caption_image' - TEMPLATES = [ - { template_id: 101470, pattern: /^aliens()\s+(.+)/i, help: 'Ancient aliens guy' }, - { template_id: 61579, pattern: /(one does not simply) (.*)/i, help: 'one does not simply walk into mordor' }, - ] + @@_templates = [] - TEMPLATES.each do |t| - route t.fetch(:pattern), :make_meme, command: true, help: t.fetch(:help) - end - def make_meme(message) line1, line2 = extract_meme_text(message.match_data) template = find_template(message.pattern) @@ -39,11 +32,11 @@ _, line1, line2 = match_data.to_a return line1, line2 end def find_template(pattern) - template = TEMPLATES.select { |t| t.fetch(:pattern) == pattern }.first + template = registered_templates.select { |t| t.fetch(:pattern) == pattern }.first raise ArgumentError if template.nil? return template end def pull_image(template_id, line1, line2) @@ -65,9 +58,26 @@ # clean me up parsed = JSON.parse(result.body) raise(ImgflipApiError, parsed['error_message']) if parsed.keys.include?('error_message') image = parsed.fetch('data', {}).fetch('url') end + + def self.add_meme(template_id:, pattern:, help:) + @@_templates << { template_id: template_id, pattern: pattern, help: help } + + route pattern, :make_meme, help: help + end + + def registered_templates + self.class.registered_templates + end + + def self.registered_templates + @@_templates + end + + add_meme(template_id: 101470, pattern: /^aliens()\s+(.+)/i, help: 'Ancient aliens guy') + add_meme(template_id: 61579, pattern: /(one does not simply) (.*)/i, help: 'one does not simply walk into mordor') Lita.register_handler(self) end end end