In Files

Parent

Namespace

Class/Module Index [+]

Quicksearch

Meme

Generate memes using memegenerator.net

Constants

GENERATORS

We have some generators up-in-here

USER_AGENT

For statistics!

VERSION

Every meme generator needs a version

Public Class Methods

advice_dog(name, id, template_name, first_line = nil) click to toggle source

For creating advice-dog type meme images.

These can accept up to two lines of text

# File lib/meme.rb, line 38
def self.advice_dog name, id, template_name, first_line = nil
  template = [id, 'AdviceDogSpinoff', template_name, first_line]
  template.compact

  GENERATORS[name] = template
end
new(generator) click to toggle source

Generates links for generator

# File lib/meme.rb, line 152
def initialize generator
  @template_id, @template_type, @generator_name, @default_line = GENERATORS.match generator
end
run(argv = ARGV) click to toggle source

Interface for the executable

# File lib/meme.rb, line 115
def self.run argv = ARGV
  generator = ARGV.shift

  if generator == '--list' then
    width = GENERATORS.keys.map { |command| command.length }.max

    GENERATORS.sort.each do |command, (id, name, _)|
      puts "%-*s  %s" % [width, command, name]
    end

    exit
  end

  abort "#{$0} [GENERATOR|--list] LINE [ADDITONAL_LINES]" if ARGV.empty?

  meme = new generator
  link = meme.generate *ARGV

  meme.paste link

  if $stdout.tty?
    puts link
  else
    puts meme.fetch link
  end
  link
rescue Interrupt
  exit
rescue SystemExit
  raise
rescue Exception => e
  abort "ERROR: #{e.message} (#{e.class})"
end
vertical(name, id, template_name) click to toggle source

For creating vertical type meme images

These can accept multiple lines of text

# File lib/meme.rb, line 50
def self.vertical name, id, template_name
  GENERATORS[name] = [id, 'Vertical', template_name]
end

Public Instance Methods

fetch(link) click to toggle source
# File lib/meme.rb, line 199
def fetch link
  url = URI.parse link
  res = nil

  Net::HTTP.start url.host do |http|
    get = Net::HTTP::Get.new url.request_uri
    get['User-Agent'] = USER_AGENT

    res = http.request get
  end
  res.body
end
generate(*args) click to toggle source

Generates a meme with line1 and line2. For some generators you only have to supply one line because the first line is defaulted for you. Isn’t that great?

# File lib/meme.rb, line 161
def generate *args
  url = URI.parse 'http://memegenerator.net/Instance/CreateOrEdit'
  res = nil
  location = nil

  # Prepend the default line if this meme has one and we only had 1 text input
  args.unshift @default_line if @default_line and args.size <= 1

  raise Error, "two lines are required for #{@generator_name}" unless
    args.size > 1

  post_data = { 'templateType'  => @template_type,
                'templateID'    => @template_id,
                'generatorName' => @generator_name }

  # go through each argument and add it back into the post data as textN
  (0..args.size).map {|num| post_data.merge! "text#{num}" => args[num] }

  Net::HTTP.start url.host do |http|
    post = Net::HTTP::Post.new url.path
    post['User-Agent'] = USER_AGENT
    post.set_form_data post_data

    res = http.request post

    location = res['Location']
    redirect = url + location

    get = Net::HTTP::Get.new redirect.request_uri
    get['User-Agent'] = USER_AGENT

    res = http.request get
  end

  doc = Nokogiri.HTML res.body
  doc.css("a[href=\"#{location}\"] img").first['src']
end
paste(link) click to toggle source

Tries to find clipboard copy executable and if found puts link in your clipboard.

# File lib/meme.rb, line 216
def paste link
  require 'pasteboard'

  clipboard = Pasteboard.new

  jpeg = fetch link

  clipboard.put_jpeg_url jpeg, link
rescue LoadError
  clipboard = %{
    /usr/bin/pbcopy
    /usr/bin/xclip
  }.find { |path| File.exist? path }

  if clipboard
    IO.popen clipboard, 'w' do |io| io.write link end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.