-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests, syllable algorithm, gunrack. Party on.
- Loading branch information
0 parents
commit d02ca13
Showing
13 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: ruby | ||
rvm: | ||
- 2.0.0 | ||
script: bundle exec rake | ||
before_install: | ||
- gem update --system | ||
services: | ||
- redis-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "https://rubygems.org" | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# lita-poetry | ||
|
||
TODO: Add a description of the plugin. | ||
|
||
## Installation | ||
|
||
Add lita-poetry to your Lita instance's Gemfile: | ||
|
||
``` ruby | ||
gem "lita-poetry" | ||
``` | ||
|
||
## Configuration | ||
|
||
TODO: Describe any configuration attributes the plugin exposes. | ||
|
||
## Usage | ||
|
||
TODO: Describe the plugin's features and how to use them. | ||
|
||
## License | ||
|
||
[MIT](http://opensource.org/licenses/MIT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "lita" | ||
|
||
Lita.load_locales Dir[File.expand_path( | ||
File.join("..", "..", "locales", "*.yml"), __FILE__ | ||
)] | ||
|
||
require "lita/handlers/poetry" | ||
|
||
Lita::Handlers::Poetry.template_root File.expand_path( | ||
File.join("..", "..", "templates"), | ||
__FILE__ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
module Lita | ||
module Handlers | ||
class Poetry < Handler | ||
|
||
class << self | ||
attr_reader :lock | ||
end | ||
|
||
@lock = Mutex.new | ||
|
||
route(/.+/, :process) | ||
on(:loaded, :populate) | ||
|
||
|
||
def process(request) | ||
one = two = three = nil | ||
|
||
self.class.lock.synchronize do | ||
three = { | ||
'id' => request.message.user.id, | ||
'count' => process_sentence(request.message.body) | ||
} | ||
|
||
redis.lpop('data') | ||
one, two = redis.lrange('data', 0, 2) | ||
redis.rpush('data', MultiJson.dump(three)) | ||
end | ||
|
||
|
||
return if one.nil? || two.nil? | ||
|
||
one = MultiJson.load(one) | ||
two = MultiJson.load(two) | ||
|
||
return unless one['id'] == two['id'] && two['id'] == three['id'] | ||
if one['count'] == 5 && two['count'] == 7 && three['count'] == 5 | ||
request.reply('Garth, that was a haiku!') | ||
end | ||
end | ||
|
||
def process_sentence(sentence) | ||
|
||
words = sentence.split(' ') | ||
|
||
return words.map{|word| count_syllables(word)}.inject(:+) | ||
|
||
end | ||
|
||
|
||
def count_syllables(word) | ||
|
||
tokenizer = /([aeiouy]{1,3})/ | ||
len = 0 | ||
|
||
if word[-3..-1] == 'ing' then | ||
len += 1 | ||
word = word[0...-3] | ||
end | ||
|
||
got = word.scan(tokenizer) | ||
len += got.size() | ||
|
||
if got.size() > 1 and got[-1] == ['e'] and | ||
word[-1].chr() == 'e' and | ||
word[-2].chr() != 'l' then | ||
len -= 1 | ||
end | ||
|
||
return len | ||
|
||
end | ||
|
||
def populate payload | ||
|
||
self.class.lock.synchronize do | ||
length = redis.llen('data') | ||
|
||
(3 - length).times do | ||
redis.rpush('data', MultiJson.dump({'id' => 0, 'count' => 0 })) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
|
||
end | ||
Lita.register_handler(Poetry) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Gem::Specification.new do |spec| | ||
spec.name = "lita-poetry" | ||
spec.version = "0.0.1" | ||
spec.authors = ["Chris Woodrich"] | ||
spec.email = ["cwoodrich@gmail.com"] | ||
spec.description = "A Haiku detector for Lita" | ||
spec.summary = "A Haiku detector for Lita" | ||
spec.homepage = "https://github.com/chriswoodrich/lita-poetry" | ||
spec.license = "MIT" | ||
spec.metadata = { "lita_plugin_type" => "handler" } | ||
|
||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_runtime_dependency "lita", ">= 4.2" | ||
|
||
spec.add_development_dependency "bundler", "~> 1.3" | ||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rack-test" | ||
spec.add_development_dependency "rspec", ">= 3.0.0" | ||
spec.add_development_dependency "simplecov" | ||
spec.add_development_dependency "coveralls" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
en: | ||
lita: | ||
handlers: | ||
poetry: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "spec_helper" | ||
|
||
describe Lita::Handlers::Poetry, lita_handler: true do | ||
|
||
it {is_expected.to route_event(:loaded).to(:populate)} | ||
it {is_expected.to route('a string').to(:process)} | ||
|
||
|
||
describe '#process' do | ||
before {robot.trigger(:loaded)} | ||
it 'tells Garth or the poetic user that (s)he has written a Haiku' do | ||
|
||
send_message("dainty daffodil") | ||
send_message("your golden trumpet fanfare") | ||
send_message("the dawning of spring") | ||
|
||
expect(replies.last).to eq('Garth, that was a haiku!') | ||
end | ||
end | ||
|
||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "simplecov" | ||
require "coveralls" | ||
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ | ||
SimpleCov::Formatter::HTMLFormatter, | ||
Coveralls::SimpleCov::Formatter | ||
] | ||
SimpleCov.start { add_filter "/spec/" } | ||
|
||
require "lita-poetry" | ||
require "lita/rspec" | ||
|
||
# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin | ||
# was generated with Lita 4, the compatibility mode should be left disabled. | ||
Lita.version_3_compatibility_mode = false |
Empty file.