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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 111e1704b68a53ad432849f6735c19a15fc40ac0
4
- data.tar.gz: 4f79a56c43bc1739f386c958f49ddfe25a02f92a
3
+ metadata.gz: d1bb029dfb75f302b33b7f55b85f625def044860
4
+ data.tar.gz: e82ea6c2d832a59824d24b958f5f50575d3b5980
5
5
  SHA512:
6
- metadata.gz: 72f30536a93b039091b9ea8b9e2581a176985c96038f9f3cadc07c0e6338e6af59c01f734e03b422e31dd87bd70d7fcd0aec000bfcaa013f819c57e6c1456da2
7
- data.tar.gz: 31fbc151b6d0034ba43675b77199a3d4199710d74d44f1d229f04a5c8b6619ea5754ad492d4a65ee507e60718009672adbfb4ec239ae6ce9a120a02e8ffd377a
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
- TODO: Add a description of the plugin.
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
- TODO: Describe any configuration attributes the plugin exposes.
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
- ## Usage
28
+ ```rb
29
+ ENV['IMGFLIP_API_USER']
30
+ ENV['IMGFLIP_API_PASSWORD']
31
+ ```
21
32
 
22
- TODO: Describe the plugin's features and how to use them.
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
- image = pull_image(template.fetch(:template_id), line1, line2)
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
- api_url = 'https://api.imgflip.com/caption_image'
44
- result = http.post api_url, {
45
- template_id: template_id,
46
- username: username,
47
- password: password,
48
- text0: line1,
49
- text1: line2
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)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-imgflip-memes"
3
- spec.version = "1.0.0"
3
+ spec.version = "1.0.1"
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"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-imgflip-memes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Pritchett