lita-imgflip-memes 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61806a949d88cdd70342fc2f2991494b1411fee0
4
- data.tar.gz: 8897c43080275c3b72c5b0abb01267668fe2792c
3
+ metadata.gz: 9bc9e1252500c8e6ea9580b06bea7cc4e628abe0
4
+ data.tar.gz: b266c475f7824d3c5ba3efe64c200b1657c8bae2
5
5
  SHA512:
6
- metadata.gz: acc6ecfef1d344bf24ddb2242466398e552dd03b67aa156138d7f0ec4e4fd9967e3d4d74960b8ac45906eccca969f52e761607e704927f5efa618a18e6f6ccf6
7
- data.tar.gz: 6fa2d4a56645b08a1bedc17a551cbebad59c7dacca0d9eed71ecb9333530671324d0e4ce502134e12ccd8f8ad98f6f1ed26c9ed293befbe0709091e465067ffe
6
+ metadata.gz: 3282da97f0d4d8c224699638045f5ec1594003e5a42ed1f3c208353b15a76391882307bde3529e90a2687190cdfdc0279671b55ab7e1b97d00238fb29fc7dfed
7
+ data.tar.gz: 9ebbec0c1a2c0cea7e3b949ccae03ed15f352f1b6d3b60e20a0c80d199c6deb53b366caa6858add5b720ff163e7053f34c6160bd1ceedb90029285030ab81b0b
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # lita-imgflip-memes
2
2
 
3
+ [![Gem](https://img.shields.io/gem/dt/lita-imgflip-memes.svg)](https://rubygems.org/gems/lita-imgflip-memes)
3
4
  [![Build Status](https://travis-ci.org/dpritchett/lita-imgflip-memes.svg?branch=master)](https://travis-ci.org/dpritchett/lita-imgflip-memes)
4
5
  [![Coverage Status](https://coveralls.io/repos/dpritchett/lita-imgflip-memes/badge.svg)](https://coveralls.io/r/dpritchett/lita-imgflip-memes)
5
6
 
@@ -0,0 +1,34 @@
1
+ module Lita
2
+ module Handlers
3
+ class ImgflipMemes < Handler
4
+ Lita.register_handler(self)
5
+
6
+ route /^(aliens)\s+(.+)/i, :make_meme, command: true,
7
+ help: 'Aliens guy meme'
8
+
9
+ def make_meme(message)
10
+ template_id = 101470
11
+ username = ENV.fetch('IMGFLIP_USERNAME', 'redacted')
12
+ password = ENV.fetch('IMGFLIP_USERNAME', 'redacted')
13
+
14
+ # generalize me
15
+ # figure out when i might have multiple matches
16
+ # instead of just :first
17
+ meme_name, text0, text1 = message.matches.first
18
+
19
+ api_url = 'https://api.imgflip.com/caption_image'
20
+ result = http.post api_url, {
21
+ template_id: template_id,
22
+ username: username,
23
+ password: password,
24
+ text0: text0,
25
+ text1: text1
26
+ }
27
+
28
+ # clean me up
29
+ image = JSON.parse(result.body).fetch("data").fetch("url")
30
+ message.reply image
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::ImgflipMemes, lita_handler: true do
4
+ let(:robot) { Lita::Robot.new(registry) }
5
+
6
+ subject { described_class.new(robot) }
7
+
8
+ describe 'routes' do
9
+ it { is_expected.to route("Lita aliens chat bots") }
10
+ end
11
+
12
+ describe ':make_meme' do
13
+ it 'responds with an image URL' do
14
+ send_message "Lita aliens chat bots"
15
+ expect(replies.last).to match(/http/i)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ def pull_image(template_id, line1, line2)
2
+ username = ENV.fetch('IMGFLIP_USERNAME', 'redacted')
3
+ password = ENV.fetch('IMGFLIP_USERNAME', 'redacted')
4
+
5
+ api_url = 'https://api.imgflip.com/caption_image'
6
+ result = http.post api_url, {
7
+ template_id: template_id,
8
+ username: username,
9
+ password: password,
10
+ text0: line1,
11
+ text1: line2
12
+ }
13
+
14
+ # clean me up
15
+ image = JSON.parse(result.body).fetch("data").fetch("url")
16
+ end
@@ -0,0 +1,11 @@
1
+ # from lita-imgflip-memes/spec/lita/handlers/imgflip_memes_spec.rb
2
+ let(:jpeg_url_match) { /http.*\.jpg/i }
3
+
4
+ describe ':pull_image' do
5
+ it 'returns a jpeg url' do
6
+ aliens_template_id = 101470
7
+ result = subject.pull_image(aliens_template_id, 'hello', 'world')
8
+
9
+ expect(result).to match(jpeg_url_match)
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ # from lita-imgflip-memes/lib/lita/handlers/imgflip_memes.rb
2
+ TEMPLATES = [
3
+ { template_id: 101470, pattern: /^aliens()\s+(.+)/i,
4
+ help: 'Ancient aliens guy' },
5
+ { template_id: 61579, pattern: /(one does not simply) (.+)/i,
6
+ help: 'one does not simply walk into mordor' },
7
+ ]
8
+
9
+ TEMPLATES.each do |t|
10
+ route t.fetch(:pattern), :make_meme, command: true, help: t.fetch(:help)
11
+ end
12
+
13
+ def make_meme(message)
14
+ match = message.matches.first
15
+ raise ArgumentError unless match.size == 2
16
+ line1, line2 = match
17
+
18
+ templates = TEMPLATES.select do |t|
19
+ t.fetch(:pattern) == message.pattern
20
+ end
21
+ template = templates.first
22
+
23
+ raise ArgumentError if template.nil?
24
+
25
+ image = pull_image(template.fetch(:template_id), line1, line2)
26
+
27
+ message.reply image
28
+ end
@@ -0,0 +1,5 @@
1
+ # from lita-imgflip-memes/spec/lita/handlers/imgflip_memes_spec.rb
2
+ it 'can handle two-line inputs' do
3
+ send_message 'lita one does not simply walk into mordor'
4
+ expect(replies.last).to match(jpeg_url_match)
5
+ end
@@ -1,3 +1,4 @@
1
+ # START:setup
1
2
  require 'pry'
2
3
 
3
4
  module Lita
@@ -12,7 +13,9 @@ module Lita
12
13
  API_URL = 'https://api.imgflip.com/caption_image'
13
14
 
14
15
  @@_templates = []
16
+ # END:setup
15
17
 
18
+ # START:make_meme
16
19
  def make_meme(message)
17
20
  line1, line2 = extract_meme_text(message.match_data)
18
21
 
@@ -27,13 +30,20 @@ module Lita
27
30
 
28
31
  message.reply image
29
32
  end
33
+ # END:make_meme
30
34
 
35
+ # START:etl
31
36
  def extract_meme_text(match_data)
32
37
  _, line1, line2 = match_data.to_a
33
38
  return line1, line2
34
39
  end
35
40
 
36
41
  def find_template(pattern)
42
+ templates = registered_templates.select do |t|
43
+ t.fetch(:pattern) == pattern
44
+ end
45
+ template = templates.first
46
+
37
47
  template = registered_templates.select { |t| t.fetch(:pattern) == pattern }.first
38
48
  raise ArgumentError if template.nil?
39
49
  return template
@@ -57,6 +67,19 @@ module Lita
57
67
 
58
68
  # clean me up
59
69
  parsed = JSON.parse(result.body)
70
+
71
+ if parsed.keys.include?('error_message')
72
+ raise(ImgflipApiError, parsed['error_message'])
73
+ end
74
+
75
+ parsed.fetch('data', {}).fetch('url')
76
+ end
77
+ # END:etl
78
+
79
+ # START:rest
80
+ def self.add_meme(template_id:, pattern:, help:)
81
+ @@_templates << { template_id: template_id, pattern: pattern,
82
+ help: help }
60
83
  raise(ImgflipApiError, parsed['error_message']) if parsed.keys.include?('error_message')
61
84
  image = parsed.fetch('data', {}).fetch('url')
62
85
  end
@@ -75,10 +98,13 @@ module Lita
75
98
  @@_templates
76
99
  end
77
100
 
78
- add_meme(template_id: 101470, pattern: /^aliens()\s+(.+)/i, help: 'Ancient aliens guy')
79
- add_meme(template_id: 61579, pattern: /(one does not simply) (.*)/i, help: 'one does not simply walk into mordor')
101
+ add_meme(template_id: 101470, pattern: /^aliens()\s+(.+)/i,
102
+ help: { 'aliens invisible sandwich' => 'Ancient Aliens Guy meme' })
103
+ add_meme(template_id: 61579, pattern: /(one does not simply) (.*)/i,
104
+ help: { 'one does not simply walk into mordor' => 'Boromir meme'})
80
105
 
81
106
  Lita.register_handler(self)
82
107
  end
83
108
  end
84
109
  end
110
+ # END:rest
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-imgflip-memes"
3
- spec.version = "1.1.1"
3
+ spec.version = "1.1.2"
4
4
  spec.authors = ["Daniel J. Pritchett"]
5
5
  spec.email = ["dpritchett@gmail.com"]
6
6
  spec.description = "Turns chatbot input text into meme images"
@@ -22,7 +22,7 @@ describe Lita::Handlers::ImgflipMemes, lita_handler: true do
22
22
  describe ':make_meme' do
23
23
  after { send_command "aliens chat bots" }
24
24
  it 'responds with an image URL' do
25
- send_message "Lita aliens chat bots"
25
+ send_message 'Lita aliens chat bots'
26
26
  expect(replies.last).to match(jpeg_url_match)
27
27
  end
28
28
 
@@ -32,6 +32,7 @@ describe Lita::Handlers::ImgflipMemes, lita_handler: true do
32
32
  end
33
33
  end
34
34
 
35
+ # START:cleaned_up
35
36
  describe ':extract_meme_text' do
36
37
  let(:matchers) { described_class.registered_templates }
37
38
 
@@ -55,4 +56,5 @@ describe Lita::Handlers::ImgflipMemes, lita_handler: true do
55
56
  expect(result).to eql(['one does not simply', 'walk into mordor'])
56
57
  end
57
58
  end
59
+ # END:cleaned_up
58
60
  end
@@ -1,8 +1,8 @@
1
1
  require "simplecov"
2
- require "coveralls"
2
+ #require "coveralls"
3
3
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
4
  SimpleCov::Formatter::HTMLFormatter,
5
- Coveralls::SimpleCov::Formatter
5
+ #Coveralls::SimpleCov::Formatter
6
6
  ]
7
7
  SimpleCov.start { add_filter "/spec/" }
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-imgflip-memes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Pritchett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-13 00:00:00.000000000 Z
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -132,12 +132,24 @@ files:
132
132
  - ".gitignore"
133
133
  - ".travis.yml"
134
134
  - Gemfile
135
+ - Gemfile.lock
135
136
  - README.md
136
137
  - Rakefile
138
+ - examples/001_make_meme.rb
139
+ - examples/002_make_meme_test.rb
140
+ - examples/003_pull_image.rb
141
+ - examples/004_pull_image_test.rb
142
+ - examples/005_add_templates.rb
143
+ - examples/006_test_two_line_inputs.rb
137
144
  - lib/lita-imgflip-memes.rb
138
145
  - lib/lita/handlers/imgflip_memes.rb
139
146
  - lita-imgflip-memes.gemspec
140
147
  - locales/en.yml
148
+ - pkg/lita-imgflip-memes-0.1.0.gem
149
+ - pkg/lita-imgflip-memes-0.2.0.gem
150
+ - pkg/lita-imgflip-memes-1.0.0.gem
151
+ - pkg/lita-imgflip-memes-1.0.1.gem
152
+ - pkg/lita-imgflip-memes-1.1.1.gem
141
153
  - spec/lita/handlers/imgflip_memes_spec.rb
142
154
  - spec/spec_helper.rb
143
155
  - templates/.gitkeep