lita-urban-dictionary 1.0.0 → 1.1.0

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: 323a8f255dee327849a89352508f56af535d2d3a
4
- data.tar.gz: f2f7fbe6591cc4f1707e3ab5e0179386111178ac
3
+ metadata.gz: 7167246d1e220c391d201369c8e7b2d26cc18d9a
4
+ data.tar.gz: b555175ce6e7de9e78fa7c3dcf92e477542a4f23
5
5
  SHA512:
6
- metadata.gz: 044c6ae27ec921c9719b4a68da759647eec10d1b0a92883d24b99d7ae371bbfbfafff14e8e8a0270cad5092a476f1c3f434764af60b5c8aa9b33e1741fbdc185
7
- data.tar.gz: d0da7847d0c7248669c9b6afa1e9921c05325342969ad911e020e0627e1fff66b6d4551b7cd958ac7e620c33aeeb1ba7d07663ed727f2ed2636c34207bc7d91a
6
+ metadata.gz: 58d9c92c22ae343ffec0c33ca987a3cf888f758dbc3000a45d2b203b437b127fdf486bf71906afb5f1273b350e71b4bfeb24b2197911ec2a10120af7b3c89b6f
7
+ data.tar.gz: efd09970015c2b943c07066fa084d221bb0341ea688a179416d3961957044a56ed8078913b47ea19cdd8ea9878af2b4eb68d3b21e7a681909100cc02561c5749
data/README.md CHANGED
@@ -14,6 +14,23 @@ Add lita-urban-dictionary to your Lita instance's Gemfile:
14
14
  gem "lita-urban-dictionary"
15
15
  ```
16
16
 
17
+ ## Configuration
18
+
19
+ ### Optional attributes
20
+
21
+ * **max_response_size** (`Integer` || `nil`) - Sets a ceiling on the response size of the queried defintion. Default: `20` (20 lines of text). `nil` will set no ceiling on responses.
22
+
23
+ ### Example
24
+
25
+ This example configuration sets a max response size of 5 lines.
26
+
27
+ ``` ruby
28
+ Lita.configure do |config|
29
+ config.handlers.urban_dictionary.max_response_size = 5
30
+ end
31
+ ```
32
+
33
+ >>>>>>> d050f60... Add max_response_size to config, add corresponding tests, handle case of nil values, document chages in README.
17
34
  ## Usage
18
35
 
19
36
  To get the definition of a word:
Binary file
@@ -4,6 +4,8 @@ module Lita
4
4
  module Handlers
5
5
  # Looks up words on Urban Dictionary.
6
6
  class UrbanDictionary < Handler
7
+ config :max_response_size, types: [Integer, nil], default: 20
8
+
7
9
  route(
8
10
  /^(?:ud|urban\s*dictionary)(?:\s+me)? (.+)/,
9
11
  :define,
@@ -17,8 +19,12 @@ module Lita
17
19
  term = response.matches[0][0]
18
20
  word, definition, example = fetch_definition(term)
19
21
  if word
20
- lines = "#{word}: #{definition}".split("\n")
21
- lines << "Example: #{example}" if example
22
+ lines = "#{word}: #{definition}".split(/\r?\n/)
23
+ if example
24
+ example_lines = example.split(/\r?\n/)
25
+ example_lines[0] = "Example: #{example_lines.first}"
26
+ lines.concat(example_lines)
27
+ end
22
28
  message = message_from_lines(lines, term)
23
29
  response.reply(message)
24
30
  else
@@ -43,9 +49,19 @@ module Lita
43
49
  end
44
50
  end
45
51
 
52
+ def determine_max_size(lines)
53
+ if config.max_response_size.nil?
54
+ max_size = lines.size
55
+ else
56
+ max_size = config.max_response_size - 1
57
+ end
58
+ max_size
59
+ end
60
+
46
61
  def message_from_lines(lines, term)
47
- if lines.size > 20
48
- lines = lines[0..19]
62
+ max_size = determine_max_size(lines)
63
+ if lines.size > max_size
64
+ lines = lines[0..max_size]
49
65
  lines << <<-TRUNCATION_MESSAGE.chomp
50
66
  Read the entire definition at: http://www.urbandictionary.com/define.php?term=#{URI.encode(term)}
51
67
  TRUNCATION_MESSAGE
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-urban-dictionary"
3
- spec.version = "1.0.0"
3
+ spec.version = "1.1.0"
4
4
  spec.authors = ["Jimmy Cuadra"]
5
5
  spec.email = ["jimmy@jimmycuadra.com"]
6
6
  spec.description = %q{A Lita handler for fetching definitions from Urban Dictionary.}
@@ -24,12 +24,18 @@ earth.","thumbs_up":565,"thumbs_down":141,"current_vote":""}]}}
24
24
 
25
25
  let(:json_large) do
26
26
  %Q{{"list":[{"defid":1369633,"word":"robot","author":"Ralius","permalink"\
27
- :"http://robot.urbanup.com/1369633","definition":"#{large_string}","thumbs_up":565,\
28
- "thumbs_down":141,"current_vote":""}]}}
27
+ :"http://robot.urbanup.com/1369633","definition":"#{large_string}","example":\
28
+ "#{large_string}","thumbs_up":565,"thumbs_down":141,"current_vote":""}]}}
29
+ end
30
+
31
+ let(:json_large_example) do
32
+ %Q{{"list":[{"defid":1369633,"word":"robot","author":"Ralius","permalink"\
33
+ :"http://robot.urbanup.com/1369633","definition":"scariest fucking thing on earth.","example":\
34
+ "#{large_string}","thumbs_up":565,"thumbs_down":141,"current_vote":""}]}}
29
35
  end
30
36
 
31
37
  let(:large_string) do
32
- 30.times.map { |n| "#{n}" }.join("\\n")
38
+ 30.times.map { |n| "#{n}" }.join("\\r\\n")
33
39
  end
34
40
 
35
41
  before do
@@ -57,7 +63,7 @@ DEF
57
63
  expect(replies.last).to eq("robot: scariest fucking thing on earth.")
58
64
  end
59
65
 
60
- it "truncates responses at 20 lines" do
66
+ it "truncates responses at 20 lines when the definition is very long" do
61
67
  response = double("Faraday::Response", status: 200, body: json_large)
62
68
  allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
63
69
  send_command("ud lita bot")
@@ -65,6 +71,14 @@ DEF
65
71
  expect(replies.last).to include("http://www.urbandictionary.com/define.php?term=lita%20bot")
66
72
  end
67
73
 
74
+ it "truncates responses at 20 lines when the example is very long" do
75
+ response = double("Faraday::Response", status: 200, body: json_large_example)
76
+ allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
77
+ send_command("ud lita bot")
78
+ expect(replies.last.split("\n").size).to eq(21)
79
+ expect(replies.last).to include("http://www.urbandictionary.com/define.php?term=lita%20bot")
80
+ end
81
+
68
82
  it "responds with a warning if no definition was found" do
69
83
  response = double("Faraday::Response", status: 200, body: "{}")
70
84
  allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(
@@ -82,5 +96,23 @@ DEF
82
96
  send_command("ud bar")
83
97
  expect(replies.last).to eq("No definition found for bar.")
84
98
  end
99
+
100
+ it "truncates responses according to the user-defined max_repsonse_size in the handler config" do
101
+ registry.config.handlers.urban_dictionary.max_response_size = 10
102
+ response = double("Faraday::Response", status: 200, body: json_large)
103
+ allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
104
+ send_command("ud lita bot")
105
+ expect(replies.last.split("\n").size).to eq(11)
106
+ expect(replies.last).to include("http://www.urbandictionary.com/define.php?term=lita%20bot")
107
+ end
108
+
109
+ it "does not truncate responses if the user-defined max_repsonse_size is set to nil" do
110
+ registry.config.handlers.urban_dictionary.max_response_size = nil
111
+ response = double("Faraday::Response", status: 200, body: json_large)
112
+ allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
113
+ send_command("ud lita bot")
114
+ expect(replies.last.split("\n").size).to eq(60)
115
+ end
116
+
85
117
  end
86
118
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-urban-dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jimmy Cuadra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-23 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -107,6 +107,7 @@ files:
107
107
  - LICENSE
108
108
  - README.md
109
109
  - Rakefile
110
+ - dump.rdb
110
111
  - lib/lita-urban-dictionary.rb
111
112
  - lib/lita/handlers/urban_dictionary.rb
112
113
  - lita-urban-dictionary.gemspec
@@ -133,10 +134,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
134
  version: '0'
134
135
  requirements: []
135
136
  rubyforge_project:
136
- rubygems_version: 2.2.2
137
+ rubygems_version: 2.4.5
137
138
  signing_key:
138
139
  specification_version: 4
139
140
  summary: A Lita handler for fetching definitions from Urban Dictionary.
140
141
  test_files:
141
142
  - spec/lita/handlers/urban_dictionary_spec.rb
142
143
  - spec/spec_helper.rb
144
+ has_rdoc: