Class: Memer

Inherits:
Object
  • Object
show all
Defined in:
lib/memer.rb

Constant Summary collapse

VERSION =
'0.1.0'
ROOT_URL =
"http://version1.api.memegenerator.net"
POST_URL =
"#{ROOT_URL}/Instance_Create"
MEME_USER =
ENV['MEME_USERNAME']
MEME_PASS =
ENV['MEME_PASSWORD']
IDS =
{
  :advice_dog               => {:generator => 225,    :image => 32},
  :all_the_things           => {:generator => 318065, :image => 1985197},
  :annoying_facebook_girl   => {:generator => 839,    :image => 876097},
  :business_cat             => {:generator => 308,    :image => 332591},
  :conspiracy_keanu         => {:generator => 318374, :image => 1986282},
  :courage_wolf             => {:generator => 303,    :image => 24},
  :first_world_problems     => {:generator => 340895, :image => 2055789},
  :hipster_kitty            => {:generator => 360,    :image => 9351},
  :insanity_wolf            => {:generator => 45,     :image => 20},
  :joseph_ducreux           => {:generator => 54,     :image => 42},
  :one_does_not_simply      => {:generator => 689854, :image => 3291562},
  :socially_awkward_penguin => {:generator => 29,     :image => 983},
  :successful_black_man     => {:generator => 350,    :image => 1570},
  :scumbag_steve            => {:generator => 142,    :image => 366130},
  :stoner_dog               => {:generator => 475,    :image => 1391},
  :success_kid              => {:generator => 121,    :image => 1031},
  :trollface                => {:generator => 68,     :image => 269},
  :vengeance_dad            => {:generator => 283,    :image => 73438},
  :y_u_no                   => {:generator => 2,      :image => 166088},
  :yo_dawg                  => {:generator => 79,     :image => 108785}
}

Class Method Summary collapse

Class Method Details

.api_response(generator_id, image_id, line1, line2) ⇒ Object



100
101
102
103
104
# File 'lib/memer.rb', line 100

def api_response(generator_id, image_id, line1, line2)
  Net::HTTP.get_response(uri_for(generator_id, image_id, line1, line2))
rescue => e
  whine({:message => "Could not access API... No connection?\n  Error: #{e.message}"})
end

.assert_username_passwordObject



67
68
69
70
71
# File 'lib/memer.rb', line 67

def assert_username_password
  if [MEME_USER, MEME_PASS].any?(&:nil?)
    whine(:message => "Sign up on memegenerator.org and export $MEME_USERNAME and $MEME_PASSWORD shell variables")
  end
end

.copy_to_clipboard(string) ⇒ Object



138
139
140
141
# File 'lib/memer.rb', line 138

def copy_to_clipboard(string)
  Clipboard.copy(string)
  puts "#{string} copied to clipboard!".color(:yellow)
end

.extract_instance_url(json_string) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/memer.rb', line 126

def extract_instance_url(json_string)
  hash = JSON.parse(json_string)

  if hash['success']
    @@instance_url = hash['result']['instanceUrl']
  elsif hash['errorMessage']
    whine({:message => "API returned an error: #{hash['errorMessage']}"})
  else
    raise("Unexpected API response: #{json_string}")
  end
end

.generate!(meme, line1 = "", line2 = "") ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/memer.rb', line 91

def generate!(meme, line1="", line2="")
  meme_ids      = IDS[meme]
  generator_id  = meme_ids[:generator]
  image_id      = meme_ids[:image]
  response_body = api_response(generator_id, image_id, line1, line2).body

  extract_instance_url(response_body)
end

.param_string(params_hash) ⇒ Object



122
123
124
# File 'lib/memer.rb', line 122

def param_string(params_hash)
  params_hash.map { |k, v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join('&')
end

.pretty_memes_listObject



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

def pretty_memes_list
  "Available memes:\n#{IDS.keys.map { |meme| "  #{meme.to_s}" }.join("\n")}"
end

.pretty_usageObject



87
88
89
# File 'lib/memer.rb', line 87

def pretty_usage
  "Usage:\n  canhaz <meme> [first-line] [second-line]\n  canhaz [list | memes]"
end

.run(args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/memer.rb', line 53

def run(args)
  assert_username_password

  whine(:list_memes => true) if args[0] == 'memes' || args[0] == 'list'
  whine(:usage => true) if args.size == 0

  meme, line1, line2 = *args

  generate!(*[meme, line1, line2].compact)
  copy_to_clipboard(@@instance_url)
rescue => e
  whine({:message => "Uncaught exception: #{e.message}"})
end

.suggest_meme(original, possibles) ⇒ Object



143
144
145
# File 'lib/memer.rb', line 143

def suggest_meme(original, possibles)
  whine({:message => "Did you mean:\n#{possibles.map {|p| "  #{p}"}.join("\n")}"})
end

.uri_for(generator_id, image_id, line1, line2) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/memer.rb', line 106

def uri_for(generator_id, image_id, line1, line2)
  params = {
    'generatorID' => generator_id,
    'imageID'     => image_id,
    'text0'       => line1,
    'text1'       => line2,
    'username'    => MEME_USER,
    'password'    => MEME_PASS
  }
  URI.parse(uri_string(POST_URL, params))
end

.uri_string(url, params_hash) ⇒ Object



118
119
120
# File 'lib/memer.rb', line 118

def uri_string(url, params_hash)
  "#{url}?#{param_string(params_hash)}"
end

.whine(options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/memer.rb', line 73

def whine(options={})
  msg = "#{options[:message]}".color(:red) if options[:message]
  lst = pretty_memes_list.color(:cyan) if options[:list_memes]
  usg = pretty_usage.color(:green) if options[:usage]

  puts [msg, usg, lst].compact.join("\n\n")

  exit(1)
end