lita-imgflip-memes 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -4
- data/lib/lita/handlers/imgflip_memes.rb +25 -12
- data/lita-imgflip-memes.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1bb029dfb75f302b33b7f55b85f625def044860
|
4
|
+
data.tar.gz: e82ea6c2d832a59824d24b958f5f50575d3b5980
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 522ea81eaf4c6a9ebe2a9df08803907cfbba59bb51ae62e66cb1d301d3dc24303e1c6b4c9180624c9e75a905ea0d52675a9554ad45469ce618ed7550c6ecd397
|
7
|
+
data.tar.gz: 540c81fe39be7571ca53d0a181f03716b2b9512caf6dbf705a99ca17a9561738246f5791247a26aa2f0e6fc5f490fcfa25b2e986b28f290f5658af3eba0e6ebb
|
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/dpritchett/lita-imgflip-memes.svg?branch=master)](https://travis-ci.org/dpritchett/lita-imgflip-memes)
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/dpritchett/lita-imgflip-memes/badge.svg)](https://coveralls.io/r/dpritchett/lita-imgflip-memes)
|
5
5
|
|
6
|
-
|
6
|
+
Add imgflip meme generation to your Lita bot!
|
7
|
+
|
8
|
+
![ancient aliens guy saying 'chat bots'](http://i.imgur.com/FxGSzjVl.jpg)
|
7
9
|
|
8
10
|
## Installation
|
9
11
|
|
@@ -14,10 +16,23 @@ gem "lita-imgflip-memes"
|
|
14
16
|
```
|
15
17
|
|
16
18
|
## Configuration
|
19
|
+
The 'lita way' to do this is to open up your `lita_config.rb` and set up file-based configuration:
|
20
|
+
|
21
|
+
```rb
|
22
|
+
config.handlers.imgflip_memes.api_user = 'myusername'
|
23
|
+
config.handlers.imgflip_memes.api_password = 'mypassword'
|
24
|
+
```
|
17
25
|
|
18
|
-
|
26
|
+
I have set up this gem to deafult to the following environment variables if the config entries aren't set in `lita_config.rb`:
|
19
27
|
|
20
|
-
|
28
|
+
```rb
|
29
|
+
ENV['IMGFLIP_API_USER']
|
30
|
+
ENV['IMGFLIP_API_PASSWORD']
|
31
|
+
```
|
21
32
|
|
22
|
-
|
33
|
+
## Usage
|
23
34
|
|
35
|
+
```
|
36
|
+
Lita > lita aliens chatbots
|
37
|
+
http://i.imgflip.com/1tzqwt.jpg
|
38
|
+
```
|
@@ -1,12 +1,16 @@
|
|
1
1
|
require 'pry'
|
2
|
+
|
2
3
|
module Lita
|
3
4
|
module Handlers
|
4
|
-
class ImgflipApiError < Error; end
|
5
|
-
|
6
5
|
class ImgflipMemes < Handler
|
6
|
+
class ImgflipApiError < StandardError; end
|
7
|
+
class ConnectionError < StandardError; end
|
8
|
+
|
7
9
|
config :api_user, default: ENV['IMGFLIP_API_USER']
|
8
10
|
config :api_password, default: ENV['IMGFLIP_API_PASSWORD']
|
9
11
|
|
12
|
+
API_URL = 'https://api.imgflip.com/caption_image'
|
13
|
+
|
10
14
|
TEMPLATES = [
|
11
15
|
{ template_id: 101470, pattern: /^aliens()\s+(.+)/i, help: 'Ancient aliens guy' },
|
12
16
|
{ template_id: 61579, pattern: /(one does not simply) (.*)/i, help: 'one does not simply walk into mordor' },
|
@@ -20,7 +24,13 @@ module Lita
|
|
20
24
|
line1, line2 = extract_meme_text(message.match_data)
|
21
25
|
|
22
26
|
template = find_template(message.pattern)
|
23
|
-
|
27
|
+
|
28
|
+
begin
|
29
|
+
image = pull_image(template.fetch(:template_id), line1, line2)
|
30
|
+
rescue ConnectionError, ImgflipApiError => err
|
31
|
+
Lita.logger.error(err.message)
|
32
|
+
return message.reply 'Bummer - can\'t connect to Imgflip.'
|
33
|
+
end
|
24
34
|
|
25
35
|
message.reply image
|
26
36
|
end
|
@@ -40,19 +50,22 @@ module Lita
|
|
40
50
|
username = config.api_user
|
41
51
|
password = config.api_password
|
42
52
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
53
|
+
begin
|
54
|
+
result = http.post API_URL, {
|
55
|
+
template_id: template_id,
|
56
|
+
username: username,
|
57
|
+
password: password,
|
58
|
+
text0: line1,
|
59
|
+
text1: line2
|
60
|
+
}
|
61
|
+
rescue Faraday::Error => err
|
62
|
+
raise ConnectionError, err.message
|
63
|
+
end
|
51
64
|
|
52
65
|
# clean me up
|
53
66
|
parsed = JSON.parse(result.body)
|
54
67
|
raise(ImgflipApiError, parsed['error_message']) if parsed.keys.include?('error_message')
|
55
|
-
image = parsed.fetch('data').fetch('url')
|
68
|
+
image = parsed.fetch('data', {}).fetch('url')
|
56
69
|
end
|
57
70
|
|
58
71
|
Lita.register_handler(self)
|
data/lita-imgflip-memes.gemspec
CHANGED