Skip to content

Commit

Permalink
Add tests, syllable algorithm, gunrack. Party on.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswoodrich committed Mar 8, 2015
0 parents commit d02ca13
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
8 changes: 8 additions & 0 deletions .travis.yml
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gemspec
23 changes: 23 additions & 0 deletions README.md
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)
6 changes: 6 additions & 0 deletions Rakefile
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
12 changes: 12 additions & 0 deletions lib/lita-poetry.rb
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__
)
90 changes: 90 additions & 0 deletions lib/lita/handlers/poetry.rb
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
25 changes: 25 additions & 0 deletions lita-poetry.gemspec
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
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
lita:
handlers:
poetry:
23 changes: 23 additions & 0 deletions spec/lita/handlers/poetry_spec.rb
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
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
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 added templates/.gitkeep
Empty file.

0 comments on commit d02ca13

Please sign in to comment.