memer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/canhaz +5 -0
- data/lib/memer.rb +115 -0
- data/spec/memer_spec.rb +12 -0
- metadata +71 -0
data/bin/canhaz
ADDED
data/lib/memer.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
class Memer
|
2
|
+
ROOT_URL = "http://version1.api.memegenerator.net"
|
3
|
+
POST_URL = "#{ROOT_URL}/Instance_Create"
|
4
|
+
|
5
|
+
MEME_USER = ENV['MEME_USERNAME']
|
6
|
+
MEME_PASS = ENV['MEME_PASSWORD']
|
7
|
+
|
8
|
+
IDS = {
|
9
|
+
:joseph_ducreux => {:generator => 54, :image => 42},
|
10
|
+
:annoying_facebook_girl => {:generator => 839, :image => 876097},
|
11
|
+
:yo_dawg => {:generator => 79, :image => 108785},
|
12
|
+
:courage_wolf => {:generator => 303, :image => 24},
|
13
|
+
:insanity_wolf => {:generator => 45, :image => 20},
|
14
|
+
:high_expectations_asian_father => {:generator => 111, :image => 1436},
|
15
|
+
:successful_black_man => {:generator => 350, :image => 1570},
|
16
|
+
:all_the_things => {:generator => 318065, :image => 1985197},
|
17
|
+
:business_cat => {:generator => 308, :image => 332591},
|
18
|
+
:advice_dog => {:generator => 225, :image => 32},
|
19
|
+
:hipster_kitty => {:generator => 360, :image => 9351},
|
20
|
+
:conspiracy_keanu => {:generator => 318374, :image => 1986282},
|
21
|
+
:first_world_problems => {:generator => 24870, :image => 1176649},
|
22
|
+
:stoner_dog => {:generator => 475, :image => 1391},
|
23
|
+
:success_kid => {:generator => 121, :image => 1031},
|
24
|
+
:one_does_not_simply => {:generator => 17304, :image => 1148734}
|
25
|
+
}
|
26
|
+
|
27
|
+
# fuzzy matching
|
28
|
+
def IDS.[](meme)
|
29
|
+
return super(meme.to_sym) if super(meme.to_sym) # if you just use it like normal
|
30
|
+
|
31
|
+
regex = Regexp.new(meme.to_s, Regexp::IGNORECASE)
|
32
|
+
super(keys.find {|k, _| k.to_s =~ regex})
|
33
|
+
end
|
34
|
+
|
35
|
+
class << self
|
36
|
+
def generate!(meme, line1, line2="")
|
37
|
+
meme_ids = IDS[meme] || usage("No meme matches '#{meme}'")
|
38
|
+
generator_id = meme_ids[:generator]
|
39
|
+
image_id = meme_ids[:image]
|
40
|
+
response_body = api_response(generator_id, image_id, line1, line2).body
|
41
|
+
|
42
|
+
extract_instance_url(response_body)
|
43
|
+
end
|
44
|
+
|
45
|
+
def api_response(generator_id, image_id, line1, line2)
|
46
|
+
require 'net/http'
|
47
|
+
Net::HTTP.get_response(uri_for(generator_id, image_id, line1, line2))
|
48
|
+
end
|
49
|
+
|
50
|
+
def uri_for(generator_id, image_id, line1, line2)
|
51
|
+
require 'uri'
|
52
|
+
params = {
|
53
|
+
'generatorID' => generator_id,
|
54
|
+
'imageID' => image_id,
|
55
|
+
'text0' => line1,
|
56
|
+
'text1' => line2,
|
57
|
+
'username' => MEME_USER,
|
58
|
+
'password' => MEME_PASS
|
59
|
+
}
|
60
|
+
URI.parse(uri_string(POST_URL, params))
|
61
|
+
end
|
62
|
+
|
63
|
+
def uri_string(url, params_hash)
|
64
|
+
"#{url}?#{param_string(params_hash)}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def param_string(params_hash)
|
68
|
+
params_hash.map { |k, v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join('&')
|
69
|
+
end
|
70
|
+
|
71
|
+
def extract_instance_url(json_string)
|
72
|
+
require 'json'
|
73
|
+
|
74
|
+
hash = JSON.parse(json_string)
|
75
|
+
|
76
|
+
if hash['success']
|
77
|
+
@@instance_url = hash['result']['instanceUrl']
|
78
|
+
else hash['errorMessage']
|
79
|
+
usage("API returned an error: #{hash['errorMessage']}")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def copy_to_clipboard(string)
|
84
|
+
require 'clipboard'
|
85
|
+
Clipboard.copy(string)
|
86
|
+
puts "#{string} copied to clipboard!".color(:yellow)
|
87
|
+
end
|
88
|
+
|
89
|
+
def usage(*messages)
|
90
|
+
if messages.empty?
|
91
|
+
puts "Usage:".color(:green)
|
92
|
+
puts " canhaz <meme> <first-line> [second-line]".color(:yellow)
|
93
|
+
else
|
94
|
+
messages.each { |msg| puts msg.color(:red) }
|
95
|
+
end
|
96
|
+
|
97
|
+
exit(1)
|
98
|
+
end
|
99
|
+
|
100
|
+
def run(args)
|
101
|
+
require 'rainbow'
|
102
|
+
|
103
|
+
if [MEME_USER, MEME_PASS].any?(&:nil?)
|
104
|
+
usage("Sign up on memegenerator.org and export $MEME_USERNAME and $MEME_PASSWORD shell variables")
|
105
|
+
end
|
106
|
+
|
107
|
+
meme, line1, line2 = *args
|
108
|
+
|
109
|
+
generate!(*[meme, line1, line2].compact)
|
110
|
+
copy_to_clipboard(@@instance_url)
|
111
|
+
rescue
|
112
|
+
usage
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/spec/memer_spec.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
describe Memer do
|
2
|
+
it 'fuzzily matches []' do
|
3
|
+
Memer::IDS[:joseph_ducreux].must_equal({:generator => 54, :image => 42})
|
4
|
+
Memer::IDS['courage_wolf'].must_equal({:generator => 303, :image => 24})
|
5
|
+
Memer::IDS['successful_bl'].must_equal({:generator => 350, :image => 1570})
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'generates the right uri string' do
|
9
|
+
uri = Memer.uri_string(Memer::POST_URL, {'generatorID' => 1234, 'line0' => 'first line', 'line1' => 'second line'})
|
10
|
+
uri.must_equal("#{Memer::POST_URL}?generatorID=1234&line0=first%20line&line1=second%20line")
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Genco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rainbow
|
16
|
+
requirement: &2152400240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152400240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: clipboard
|
27
|
+
requirement: &2152399480 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152399480
|
36
|
+
description: A command line tool to easily create and share your own memes!
|
37
|
+
email: alexgenco@gmail.com
|
38
|
+
executables:
|
39
|
+
- canhaz
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- lib/memer.rb
|
44
|
+
- bin/canhaz
|
45
|
+
- spec/memer_spec.rb
|
46
|
+
homepage: http://github.com/alexgenco/memer
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.17
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Meme generator in Ruby
|
70
|
+
test_files:
|
71
|
+
- spec/memer_spec.rb
|