meminator 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ group :test do
5
+ gem "rake"
6
+ gem "rspec"
7
+ gem "mocha"
8
+ end
data/README.md CHANGED
@@ -1,56 +1,25 @@
1
- NOTE:
1
+ # Meminator
2
2
 
3
- To ease the addition of new memes, use this handy bookmarklet:
3
+ A quick hack based on https://github.com/drbrain/meme
4
4
 
5
- http://bookmarklets.heroku.com/bookmarklets/1814
5
+ # API
6
6
 
7
- = meme_generator
7
+ Is crude but usable. It was written almost entirely for jamming into
8
+ [Agent99](https://github.com/99designs/agent99) and so it does some really
9
+ stupid things that it shouldn't. I intend to fix them at some point.
8
10
 
9
- * http://github.com/drbrain/meme
10
- * http://docs.seattlerb.org/meme_generator
11
+ # Examples
11
12
 
12
- == DESCRIPTION:
13
+ ```ruby
13
14
 
14
- Generate meme images using http://memegenerator.net! Save yourself some time!
15
+ meme_generator = Meminator::Meminator.new
16
+ meme_generator.get_url("I_WOLF", "Patient stubbed toe", "Put him down") # => returns a url for the new meme
15
17
 
16
- == FEATURES/PROBLEMS:
18
+ ```
17
19
 
18
- * Features many popular meme pictures
19
- * No tests
20
+ It's worth noting that at time of writing, memegenerator.net was offline so I
21
+ couldn't even integration test
20
22
 
21
- == SYNOPSIS:
22
23
 
23
- Generate a Y U NO meme:
24
24
 
25
- $ meme Y_U_NO 'write tests?'
26
-
27
- Generate a Y U NO meme url only, no clipboard or pulling of the image data:
28
-
29
- $ meme --text Y_U_NO 'write tests?'
30
-
31
- See a list of available generators
32
-
33
- $ meme --list
34
-
35
- You can also drive it like an API.
36
-
37
- == REQUIREMENTS:
38
-
39
- * nokogiri
40
- * internet connection
41
-
42
- == INSTALL:
43
-
44
- gem install meme_generator
45
-
46
- == DEVELOPERS:
47
-
48
- After checking out the source, run:
49
-
50
- $ rake newb
51
-
52
- This task will install any missing dependencies, run the tests/specs,
53
- and generate the RDoc.
54
-
55
- == LICENSE:
56
25
 
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ end
7
+
8
+ desc 'Default: run specs'
9
+ task :default => :spec
data/lib/meminator.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'net/http'
2
+ require 'nokogiri'
3
+ require 'cgi'
4
+
5
+ ##
6
+ # Generate memes using http://memegenerator.net
7
+
8
+ module Meminator
9
+ VERSION = '0.0.1'
10
+ GENERATOR_URL = 'http://memegenerator.net/Instance/CreateOrEdit'
11
+ class Error < Exception; end
12
+ class Meminator
13
+
14
+ def get_url(meme, *text)
15
+ template_id, template_type, generator_name, default_line = List.get(meme)
16
+
17
+ unless template_id
18
+ return "Couldn't find template #{meme}"
19
+ end
20
+
21
+ url = URI.parse(GENERATOR_URL)
22
+
23
+ post_data = { 'templateType' => template_type,
24
+ 'templateID' => template_id,
25
+ 'generatorName' => generator_name }
26
+
27
+ [default_line, *text].compact.each_with_index do |item, idx|
28
+ post_data.merge! "text#{idx}" => item
29
+ end
30
+
31
+ begin
32
+ return fetch(url, post_data)
33
+ rescue Error => e
34
+ return e.message
35
+ end
36
+ end
37
+
38
+ def fetch(url, post_data)
39
+ Net::HTTP.start url.host do |http|
40
+ post = Net::HTTP::Post.new url.path
41
+ post['User-Agent'] = ::Meminator.user_agent
42
+ post.set_form_data post_data
43
+
44
+ res = http.request post
45
+
46
+ unless Net::HTTPSuccess === res
47
+ raise Error, "memegenerator.net appears to be down, got #{res.code}"
48
+ end
49
+
50
+ location = res['Location']
51
+ redirect = url + location
52
+
53
+ get = Net::HTTP::Get.new redirect.request_uri
54
+ get['User-Agent'] = ::Meminator.user_agent
55
+
56
+ res = http.request get
57
+ end
58
+
59
+ if Net::HTTPSuccess === res
60
+ doc = Nokogiri.HTML res.body
61
+ doc.css("a[href=\"#{location}\"] img").first['src']
62
+ else
63
+ raise Error, "memegenerator.net appears to be down, got #{res.code}"
64
+ end
65
+ end
66
+
67
+ end
68
+ end
69
+
70
+ %w[list user_agent].each do |filename|
71
+ require "meminator/#{filename}"
72
+ end
@@ -0,0 +1,81 @@
1
+ module Meminator
2
+ class List; class << self
3
+
4
+ def memes
5
+ @memes ||= generate_meme_list
6
+ end
7
+
8
+ def get(name)
9
+ return memes[name] if memes.has_key?(name)
10
+ matcher = Regexp.new(name, Regexp::IGNORECASE)
11
+ _, generator = @memes.find { |k,v| matcher =~ k || v.grep(matcher).any? }
12
+ generator
13
+ end
14
+
15
+ def generate_meme_list
16
+ @tmp_meme_list = Hash.new
17
+ advice_dog 'ANTEATER', 41191, 'anteater'
18
+ advice_dog 'A_DODSON', 106375, 'Antoine-Dodson'
19
+ advice_dog 'A_DOG', 940, 'Advice-Dog'
20
+ advice_dog 'A_FATHER', 1436, 'High-Expectations-Asian-Father'
21
+ advice_dog 'ARNOLD', 1236, 'Angry-Arnold'
22
+ advice_dog 'BEAR-GRYLLS', 89714, 'Bear-Grylls'
23
+ advice_dog 'BUTTHURT_DWELLER', 1438, 'Butthurt-Dweller'
24
+ advice_dog 'B_FROG', 1211, 'Foul-Bachelorette-Frog'
25
+ advice_dog 'B_FROG2', 1045, 'Foul-Bachelor-Frog'
26
+ advice_dog 'BEIBER', 11809, 'Justin-Beiber'
27
+ advice_dog 'CATHY', 622381, 'AckCathy'
28
+ advice_dog 'CHALLENGE_ACCEPTED', 275025, 'Challenge-Accepted'
29
+ advice_dog 'COOL_STORY_HOUSE', 16948, 'cool-story-bro-house'
30
+ advice_dog 'CREEPER', 173501, 'Minecraft-Creeper'
31
+ advice_dog 'C_WOLF', 931, 'Courage-Wolf'
32
+ advice_dog 'C_LEMUR', 1227, 'Chill-Out-Lemur'
33
+ advice_dog 'F_FRY', 84688, 'Futurama-Fry'
34
+ advice_dog 'G_GRANDPA', 185650, 'Grumpy-Grandpa'
35
+ advice_dog 'H_MERMAID', 405224, 'Hipster-Mermaid'
36
+ advice_dog 'I_DONT_ALWAYS', 38926, 'The-Most-Interesting-Man-in-the-World'
37
+ advice_dog 'I_WOLF', 926, 'Insanity-Wolf'
38
+ advice_dog 'JESUS', 1281, 'jesus-says'
39
+ advice_dog 'J_DUCREUX', 1356, 'Joseph-Ducreux'
40
+ advice_dog 'KANYE', 622033, 'kanyee'
41
+ advice_dog 'KEANU', 47718, 'Keanu-reeves'
42
+ advice_dog 'KJI', 5840, 'Kim-Jong-IL'
43
+ advice_dog 'MILHOUSE', 228797, 'Millhouse'
44
+ advice_dog 'MINECRAFT', 122309, 'Minecraft'
45
+ advice_dog 'MORE_BEAR', 33675, 'More-Bear'
46
+ advice_dog 'O-RLY-OWL', 117041, 'O-RLY-OWL', 'ORLY???'
47
+ advice_dog 'OBAMA', 1332, 'Obama-'
48
+ advice_dog 'PHILOSORAPTOR', 984, 'Philosoraptor'
49
+ advice_dog 'P_OAK', 24321, 'Professor-Oak'
50
+ advice_dog 'P_OBAMA', 45557, 'Pissed-off-Obama'
51
+ advice_dog 'R_BLACK', 547955, 'Rebecca-Black-Meme'
52
+ advice_dog 'REDSHIRT', 239371, 'red-shirt-guy'
53
+ advice_dog 'SCUMBAG', 364688, 'Scumbag-Steve'
54
+ advice_dog 'SERIOUS_FISH', 7054219, 'Serious-fish-spongebob'
55
+ advice_dog 'SHEEN', 488762, 'Charlie-Sheen'
56
+ advice_dog 'SNOB', 2994, 'Snob'
57
+ advice_dog 'SPARTA', 1013, 'sparta'
58
+ advice_dog 'SPIDERMAN', 1037, 'Question-Spiderman'
59
+ advice_dog 'SPILLMAN', 622133, 'Sassy-Spillman'
60
+ advice_dog 'SWEDISH_CHEF', 186651, 'Swedish-Chef'
61
+ advice_dog 'S_AWKWARD_PENGUIN', 983, 'Socially-Awkward-Penguin'
62
+ advice_dog 'TOWNCRIER', 434537, 'Towncrier'
63
+ advice_dog 'TROLLFACE', 1030, 'Troll-Face'
64
+ advice_dog 'UNICORN_BOY', 57022, 'unicorn-boy'
65
+ advice_dog 'US_POINT', 131083, 'Uncle-Sam-Point', 'I WANT YOU'
66
+ advice_dog 'V_BABY', 11140, 'Victory-Baby'
67
+ advice_dog 'XZIBIT', 3114, 'XZIBIT'
68
+ advice_dog 'Y_U_NO', 165241, 'Y-U-NO', 'Y U NO'
69
+ advice_dog 'YODA', 963, 'Advice-Yoda-Gives'
70
+ @tmp_meme_list
71
+ end
72
+
73
+ def advice_dog(name, id, template_name, first_line = nil)
74
+ template = [id, 'AdviceDogSpinoff', template_name, first_line]
75
+ template.compact
76
+
77
+ @tmp_meme_list[name] = template
78
+ end
79
+
80
+ end; end
81
+ end
@@ -0,0 +1,13 @@
1
+ module Meminator
2
+ extend self
3
+
4
+ @@useragent = "meminator/#{VERSION} Ruby/#{RUBY_VERSION}"
5
+ def user_agent
6
+ @@useragent
7
+ end
8
+
9
+ def user_agent=(agent)
10
+ @@useragent = agent
11
+ end
12
+
13
+ end
data/meminator.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "meminator"
3
- s.version = "0.0.0"
3
+ s.version = "0.0.1"
4
4
  s.authors = ["Eric Hodel", "Rich Healey"]
5
5
  s.email = ["richo@psych0tik.net"]
6
6
  s.homepage = "http://github.com/richoH/meminator"
@@ -8,6 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.description = s.summary
9
9
 
10
10
  s.add_dependency "nokogiri"
11
+ s.add_development_dependency "mocha"
12
+ s.add_development_dependency "rspec"
11
13
 
12
14
  s.files = `git ls-files`.split("\n")
13
15
  # s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Meminator::List do
4
+
5
+ it "Should return the list of memes for #memes" do
6
+ Meminator::List.memes.should_not be_empty
7
+ end
8
+
9
+ it "Should return a meme on a call to #get" do
10
+ Meminator::List.get('I_WOLF').should_not be_nil
11
+ end
12
+
13
+ it "Should return nil for memes that don't exist" do
14
+ Meminator::List.get('rawk!').should be_nil
15
+ end
16
+
17
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Meminator do
4
+
5
+ it "Should return a default user_agent" do
6
+ ::Meminator.user_agent.should == "meminator/#{Meminator::VERSION} Ruby/#{RUBY_VERSION}"
7
+ end
8
+
9
+ it "Should update the user agent if asked to" do
10
+ ::Meminator.user_agent = "Test useragent"
11
+ ::Meminator.user_agent.should == "Test useragent"
12
+ end
13
+
14
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Meminator do
4
+
5
+ it "Should serialize it's arguments" do
6
+ meminator = Meminator::Meminator.new
7
+
8
+
9
+ meminator.expects(:fetch).with(URI.parse(Meminator::GENERATOR_URL),
10
+ {'templateType' => 'AdviceDogSpinoff', 'templateID' => 926, 'generatorName' => 'Insanity-Wolf', 'text0' => 'First line', 'text1' => 'Second line'}
11
+ )
12
+
13
+ meminator.get_url("I_WOLF", "First line", "Second line")
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'meminator'
2
+ require 'mocha'
3
+
4
+ Dir["#{File.expand_path(File.dirname(__FILE__))}/helpers/**/*.rb"].each do |f|
5
+ puts "Requiring #{f}"
6
+ require f
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :mocha
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meminator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -28,6 +28,38 @@ dependencies:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: mocha
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
31
63
  description: API to retrieve urls for memes
32
64
  email:
33
65
  - richo@psych0tik.net
@@ -36,12 +68,21 @@ extensions: []
36
68
  extra_rdoc_files: []
37
69
  files:
38
70
  - .gitignore
71
+ - .travis.yml
72
+ - Gemfile
39
73
  - History.txt
40
74
  - LICENSE
41
75
  - Manifest.txt
42
76
  - README.md
43
- - lib/meme.rb
77
+ - Rakefile
78
+ - lib/meminator.rb
79
+ - lib/meminator/list.rb
80
+ - lib/meminator/user_agent.rb
44
81
  - meminator.gemspec
82
+ - spec/meminator/list_spec.rb
83
+ - spec/meminator/user_agent_spec.rb
84
+ - spec/meminator_spec.rb
85
+ - spec/spec_helper.rb
45
86
  homepage: http://github.com/richoH/meminator
46
87
  licenses: []
47
88
  post_install_message:
data/lib/meme.rb DELETED
@@ -1,264 +0,0 @@
1
- require 'net/http'
2
- require 'rubygems'
3
- require 'nokogiri'
4
- require 'cgi'
5
-
6
- ##
7
- # Generate memes using http://memegenerator.net
8
-
9
- class Meme
10
-
11
- ##
12
- # Sometimes your meme will have an error, fix it!
13
-
14
- class Error < RuntimeError; end
15
-
16
- ##
17
- # Every meme generator needs a version
18
-
19
- VERSION = '1.10'
20
-
21
- ##
22
- # For statistics!
23
-
24
- USER_AGENT = "meme/#{VERSION} Ruby/#{RUBY_VERSION}"
25
-
26
- ##
27
- # We have some generators up-in-here
28
-
29
- GENERATORS = Hash.new do |_, k|
30
- raise Error, "unknown generator #{k}"
31
- end
32
-
33
- ##
34
- # For creating advice-dog type meme images.
35
- #
36
- # These can accept up to two lines of text
37
-
38
- def self.advice_dog name, id, template_name, first_line = nil
39
- template = [id, 'AdviceDogSpinoff', template_name, first_line]
40
- template.compact
41
-
42
- GENERATORS[name] = template
43
- end
44
-
45
- ##
46
- # For creating vertical type meme images
47
- #
48
- # These can accept multiple lines of text
49
-
50
- def self.vertical name, id, template_name
51
- GENERATORS[name] = [id, 'Vertical', template_name]
52
- end
53
-
54
- # keep generators in alphabetical order
55
- advice_dog 'ANTEATER', 41191, 'anteater'
56
- advice_dog 'A_DODSON', 106375, 'Antoine-Dodson'
57
- advice_dog 'A_DOG', 940, 'Advice-Dog'
58
- advice_dog 'A_FATHER', 1436, 'High-Expectations-Asian-Father'
59
- advice_dog 'ARNOLD', 1236, 'Angry-Arnold'
60
- advice_dog 'BEAR-GRYLLS', 89714, 'Bear-Grylls'
61
- advice_dog 'BUTTHURT_DWELLER', 1438, 'Butthurt-Dweller'
62
- advice_dog 'B_FROG', 1211, 'Foul-Bachelorette-Frog'
63
- advice_dog 'B_FROG2', 1045, 'Foul-Bachelor-Frog'
64
- advice_dog 'BEIBER', 11809, 'Justin-Beiber'
65
- advice_dog 'CATHY', 622381, 'AckCathy'
66
- advice_dog 'CHALLENGE_ACCEPTED', 275025, 'Challenge-Accepted'
67
- advice_dog 'COOL_STORY_HOUSE', 16948, 'cool-story-bro-house'
68
- advice_dog 'CREEPER', 173501, 'Minecraft-Creeper'
69
- advice_dog 'C_WOLF', 931, 'Courage-Wolf'
70
- advice_dog 'C_LEMUR', 1227, 'Chill-Out-Lemur'
71
- advice_dog 'F_FRY', 84688, 'Futurama-Fry'
72
- advice_dog 'G_GRANDPA', 185650, 'Grumpy-Grandpa'
73
- advice_dog 'H_MERMAID', 405224, 'Hipster-Mermaid'
74
- advice_dog 'I_DONT_ALWAYS', 38926, 'The-Most-Interesting-Man-in-the-World'
75
- advice_dog 'I_WOLF', 926, 'Insanity-Wolf'
76
- advice_dog 'JESUS', 1281, 'jesus-says'
77
- advice_dog 'J_DUCREUX', 1356, 'Joseph-Ducreux'
78
- advice_dog 'KANYE', 622033, 'kanyee'
79
- advice_dog 'KEANU', 47718, 'Keanu-reeves'
80
- advice_dog 'KJI', 5840, 'Kim-Jong-IL'
81
- advice_dog 'MILHOUSE', 228797, 'Millhouse'
82
- advice_dog 'MINECRAFT', 122309, 'Minecraft'
83
- advice_dog 'MORE_BEAR', 33675, 'More-Bear'
84
- advice_dog 'O-RLY-OWL', 117041, 'O-RLY-OWL', 'ORLY???'
85
- advice_dog 'OBAMA', 1332, 'Obama-'
86
- advice_dog 'PHILOSORAPTOR', 984, 'Philosoraptor'
87
- advice_dog 'P_OAK', 24321, 'Professor-Oak'
88
- advice_dog 'P_OBAMA', 45557, 'Pissed-off-Obama'
89
- advice_dog 'R_BLACK', 547955, 'Rebecca-Black-Meme'
90
- advice_dog 'REDSHIRT', 239371, 'red-shirt-guy'
91
- advice_dog 'SCUMBAG', 364688, 'Scumbag-Steve'
92
- advice_dog 'SERIOUS_FISH', 7054219, 'Serious-fish-spongebob'
93
- advice_dog 'SHEEN', 488762, 'Charlie-Sheen'
94
- advice_dog 'SNOB', 2994, 'Snob'
95
- advice_dog 'SPARTA', 1013, 'sparta'
96
- advice_dog 'SPIDERMAN', 1037, 'Question-Spiderman'
97
- advice_dog 'SPILLMAN', 622133, 'Sassy-Spillman'
98
- advice_dog 'SWEDISH_CHEF', 186651, 'Swedish-Chef'
99
- advice_dog 'S_AWKWARD_PENGUIN', 983, 'Socially-Awkward-Penguin'
100
- advice_dog 'TOWNCRIER', 434537, 'Towncrier'
101
- advice_dog 'TROLLFACE', 1030, 'Troll-Face'
102
- advice_dog 'UNICORN_BOY', 57022, 'unicorn-boy'
103
- advice_dog 'US_POINT', 131083, 'Uncle-Sam-Point', 'I WANT YOU'
104
- advice_dog 'V_BABY', 11140, 'Victory-Baby'
105
- advice_dog 'XZIBIT', 3114, 'XZIBIT'
106
- advice_dog 'Y_U_NO', 165241, 'Y-U-NO', 'Y U NO'
107
- advice_dog 'YODA', 963, 'Advice-Yoda-Gives'
108
-
109
- vertical 'BATMAN', 148359, 'batman-panal-ryan'
110
- vertical 'INCEPTION', 107949, 'Inception'
111
- vertical 'NEO', 173419, 'Neo'
112
- vertical 'THE_ROCK', 417195, 'The-Rock-driving'
113
-
114
- # keep generators in alphabetical order
115
-
116
- ##
117
- # Looks up generator name
118
-
119
- def GENERATORS.match(name)
120
- # TODO meme Y U NO DEMETAPHONE?
121
- return self[name] if has_key? name
122
- matcher = Regexp.new(name, Regexp::IGNORECASE)
123
- _, generator = find { |k,v| matcher =~ k || v.grep(matcher).any? }
124
- generator || self[name] # raises the error if generator is nil
125
- end
126
-
127
- ##
128
- # Interface for the executable
129
-
130
- def self.run argv = ARGV
131
- generator = ARGV.shift
132
-
133
- if generator == '--list' then
134
- width = GENERATORS.keys.map { |command| command.length }.max
135
-
136
- GENERATORS.sort.each do |command, (id, name, _)|
137
- puts "%-*s %s" % [width, command, name]
138
- end
139
-
140
- exit
141
- end
142
-
143
- text_only = if generator == '--text'
144
- generator = ARGV.shift
145
- true
146
- else
147
- false
148
- end
149
-
150
- # puts "text_only:#{text_only} generator:#{generator}"
151
-
152
- abort "#{$0} [GENERATOR|--list] LINE [ADDITONAL_LINES]" if ARGV.empty?
153
-
154
- meme = new generator
155
- link = meme.generate(*ARGV)
156
-
157
- meme.paste(link) unless text_only
158
-
159
- if $stdout.tty? || text_only
160
- puts link
161
- else
162
- puts meme.fetch link
163
- end
164
- link
165
- rescue Interrupt
166
- exit
167
- rescue SystemExit
168
- raise
169
- rescue Exception => e
170
- puts e.backtrace.join "\n\t" if $DEBUG
171
- abort "ERROR: #{e.message} (#{e.class})"
172
- end
173
-
174
- ##
175
- # Generates links for +generator+
176
-
177
- def initialize generator
178
- @template_id, @template_type, @generator_name, @default_line = GENERATORS.match generator
179
- end
180
-
181
- ##
182
- # Generates a meme with +line1+ and +line2+. For some generators you only
183
- # have to supply one line because the first line is defaulted for you.
184
- # Isn't that great?
185
-
186
- def generate *args
187
- url = URI.parse 'http://memegenerator.net/Instance/CreateOrEdit'
188
- res = nil
189
- location = nil
190
-
191
- # Prepend the default line if this meme has one and we only had 1 text input
192
- args.unshift @default_line if @default_line and args.size <= 1
193
-
194
- raise Error, "two lines are required for #{@generator_name}" unless
195
- args.size > 1
196
-
197
- post_data = { 'templateType' => @template_type,
198
- 'templateID' => @template_id,
199
- 'generatorName' => @generator_name }
200
-
201
- # go through each argument and add it back into the post data as textN
202
- (0..args.size).map {|num| post_data.merge! "text#{num}" => args[num] }
203
-
204
- Net::HTTP.start url.host do |http|
205
- post = Net::HTTP::Post.new url.path
206
- post['User-Agent'] = USER_AGENT
207
- post.set_form_data post_data
208
-
209
- res = http.request post
210
-
211
- location = res['Location']
212
- redirect = url + location
213
-
214
- get = Net::HTTP::Get.new redirect.request_uri
215
- get['User-Agent'] = USER_AGENT
216
-
217
- res = http.request get
218
- end
219
-
220
- if Net::HTTPSuccess === res then
221
- doc = Nokogiri.HTML res.body
222
- doc.css("a[href=\"#{location}\"] img").first['src']
223
- else
224
- raise Error, "memegenerator.net appears to be down, got #{res.code}"
225
- end
226
- end
227
-
228
- def fetch link
229
- url = URI.parse link
230
- res = nil
231
-
232
- Net::HTTP.start url.host do |http|
233
- get = Net::HTTP::Get.new url.request_uri
234
- get['User-Agent'] = USER_AGENT
235
-
236
- res = http.request get
237
- end
238
- res.body
239
- end
240
-
241
- ##
242
- # Tries to find clipboard copy executable and if found puts +link+ in your
243
- # clipboard.
244
-
245
- def paste link
246
- require 'pasteboard'
247
-
248
- clipboard = Pasteboard.new
249
-
250
- jpeg = fetch link
251
-
252
- clipboard.put_jpeg_url jpeg, link
253
- rescue LoadError
254
- clipboard = %w{
255
- /usr/bin/pbcopy
256
- /usr/bin/xclip
257
- }.find { |path| File.exist? path }
258
-
259
- if clipboard
260
- IO.popen clipboard, 'w' do |io| io.write link end
261
- end
262
- end
263
-
264
- end