Skip to content

Commit

Permalink
Merge pull request #13 from equintana/reset
Browse files Browse the repository at this point in the history
✨ Reset command
  • Loading branch information
erlinis committed Nov 30, 2016
2 parents 50b41e3 + 0014808 commit 8b5584b
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem "lita-service"
lita service <NAME> add|sum <CUSTOMER> <*QUANTITY> # Quantity will be set to 1 if empty.
lita service <NAME> add|sum all <*QUANTITY> # Quantity will be set to 1 if empty.
lita service <NAME> value <CUSTOMER> <VALUE>
lita service <NAME> reset <CUSTOMER>
```

To show the available commands
Expand All @@ -50,4 +51,5 @@ To show the available commands
lita service awesome-service delete erlinis
lita service awesome-service remove erlinis
lita service awesome-service value erlinis 300
lita service awesome-service reset erlinis
```
1 change: 1 addition & 0 deletions lib/lita-service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'lita/interactors/inscribe_customer'
require 'lita/interactors/change_value'
require 'lita/interactors/add_quantity'
require 'lita/interactors/reset_quantity'
require 'lita/interactors/add_all'
require 'lita/interactors/delete_customer'
require 'lita/helpers/messages_helper'
Expand Down
12 changes: 12 additions & 0 deletions lib/lita/handlers/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def self.template_root
help: { 'service _<name>_ add|sum all _<*quantity>_' =>
'Adds _<*quantity>_ or 1 to all users on service' })

route(/service ([\w-]+) reset ([\@\w-]+)/, :reset,
command: true,
help: { 'service _<name>_ reset _<user>_' =>
'Sets the quantity of the _<user>_ to zero' })

route(/service ([\w-]+) value ([\@\w-]+) ([0-9-]*)$/, :change_value,
command: true,
help: { 'service _<name>_ value _<user> <value>_' =>
Expand Down Expand Up @@ -64,6 +69,13 @@ def add_all(response)
reply(template, message, response, interactor)
end

def reset(response)
interactor = Interactors::ResetQuantity.new(self, response.match_data).perform
template = :message
message = { message: interactor.message }
reply(template, message, response, interactor)
end

def delete_customer(response)
interactor = Interactors::DeleteCustomer.new(self, response.match_data).perform
template = :message
Expand Down
2 changes: 1 addition & 1 deletion lib/lita/interactors/add_quantity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Lita
module Interactors
# Inscribes a customer in a service
# Adds the given quantity to the customer's balance
class AddQuantity < BaseInteractor
include Lita::Helpers::MessagesHelper

Expand Down
2 changes: 1 addition & 1 deletion lib/lita/interactors/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def name
end

def value
@value ||= data[2].to_f
@value ||= data[2].to_i
end

def service_exists?
Expand Down
74 changes: 74 additions & 0 deletions lib/lita/interactors/reset_quantity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true
require 'lita/helpers/messages_helper'

module Lita
module Interactors
# Set the customer quantity to zero
class ResetQuantity < BaseInteractor
include Lita::Helpers::MessagesHelper

attr_reader :data

def initialize(handler, data)
@handler = handler
@data = data
end

def perform
if service_exists?
update_costumer_if_exist
else
@error = msg_not_found(service_name: name)
end
self
end

private

def name
@name ||= data[1]
end

def customer_name
@customer_name ||= data[2].delete('@')
end

def service
@service ||= repository.find(name)
end

def customer
service[:customers][customer_name.to_sym]
end

def service_exists?
repository.exists?(name)
end

def customer_exists?
service[:customers].keys.include?(customer_name.to_sym)
end

def update_costumer_if_exist
if customer_exists?
update_customer_quantity
else
@error = msg_customer_not_found(service_name: name,
customer_name: customer_name)
end
end

def update_customer_quantity
reset_quantity
repository.update(service)
end

def reset_quantity
customer[:quantity] = 0

@message = I18n.t('lita.handlers.service.reset.success',
customer_name: customer_name)
end
end
end
end
4 changes: 2 additions & 2 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ en:
tip: "Add customers with:\nlita service *%{service_name}* inscribe < CUSTOMER > < *VALUE >"
set_value:
success: "*%{customer_name}* value was changed from *%{old_value}* to *%{customer_value}*."
reset:
success: "*%{customer_name}* quantity was changed to *0*."
add:
success: "*%{quantity}* was added to *%{customer_name}*, new quantity: *%{customer_quantity}*"
add_all:
Expand All @@ -28,5 +30,3 @@ en:
customer:
duplicated: "*%{customer_name}* is already in *%{service_name}*."
not_found: "There is no *%{customer_name}* in *%{service_name}*."


27 changes: 25 additions & 2 deletions spec/lita/handlers/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
additional_lita_handlers: Lita::Handlers::Service do
describe 'routes' do
it { is_expected.to route_command('service XYZ inscribe @jhon').to(:inscribe) }
it { is_expected.to route_command('service XYZ inscribe @jhon 20').to(:inscribe) }
it { is_expected.to route_command('service XYZ value @jhon 2000').to(:change_value) }
it { is_expected.to route_command('service XYZ inscribe @jhon 200').to(:inscribe) }
it { is_expected.to route_command('service XYZ value @jhon 200').to(:change_value) }
it { is_expected.to route_command('service XYZ add @jhon 2').to(:add) }
it { is_expected.to route_command('service XYZ add @jhon -2').to(:add) }
it { is_expected.to route_command('service XYZ add @jhon').to(:add) }
Expand All @@ -16,6 +16,7 @@
it { is_expected.to route_command('service XYZ add all').to(:add_all) }
it { is_expected.to route_command('service XYZ add all 2').to(:add_all) }
it { is_expected.to route_command('service XYZ sum all 2').to(:add_all) }
it { is_expected.to route_command('service XYZ reset @jhon').to(:reset) }
it { is_expected.to route_command('service XYZ delete jhon').to(:delete_customer) }
it { is_expected.to route_command('service XYZ remove jhon').to(:delete_customer) }
end
Expand Down Expand Up @@ -109,6 +110,28 @@
end
end

describe '#reset' do
describe 'when service exists' do
before do
send_command('service create TheService')
send_command('service TheService inscribe @erlinis 2000')
end

it 'sets the customer quantity in zero' do
success_message = '_*2* was added to *erlinis*, new quantity: *2*_'
send_command('service TheService add @erlinis 2')
expect(replies.last).to eq(success_message)
end
end

describe ' when service does not exit' do
it 'replys with an error' do
send_command('service TheService reset @erlinis')
expect(replies.last).to eq(service_not_found_error)
end
end
end

describe '#add' do
describe 'when service exists' do
before do
Expand Down
96 changes: 96 additions & 0 deletions spec/lita/interactors/reset_quantity_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: false
require 'spec_helper'

describe Lita::Interactors::ResetQuantity do
let(:data) { ['service the-service reset @erlinis', name, 'erlinis'] }
let(:interactor) { described_class.new(handler, data) }
let(:handler) { double('handler') }
let(:fake_repository) { double('redis-repository') }

before do
allow(interactor).to receive(:repository).and_return(fake_repository)
end

describe '#perform' do
let(:name) { 'the-service' }

describe 'when the service does not exist' do
let(:error_message) do
I18n.t('lita.handlers.service.errors.not_found', service_name: name)
end

before do
allow(fake_repository).to receive(:exists?).with(name).and_return(false)
end

it 'shows an error message' do
interactor.perform
expect(interactor.success?).to eq false
expect(interactor.error).to eq error_message
end
end

describe 'when service exists' do
describe 'when customer is not in the service' do
let(:service) do
{ name: name,
value: 2000,
state: 'active',
customers: { khal: { quantity: 1, value: 2000 } } }
end

let(:error_message) do
I18n.t('lita.handlers.service.customer.not_found',
service_name: name, customer_name: 'erlinis')
end

before do
allow(fake_repository).to receive(:exists?).with(name).and_return(true)
allow(fake_repository).to receive(:find).with(name).and_return(service)
end

it 'shows an error message' do
interactor.perform
expect(interactor.success?).to eq false
expect(interactor.error).to eq error_message
end
end

describe 'when customer is in service' do
let(:success_message) do
I18n.t('lita.handlers.service.reset.success', customer_name: 'erlinis')
end

before do
service = {
name: name,
value: 2000,
state: 'active',
customers: {
erlinis: { quantity: 3, value: 2000 }
}
}

service_customer_updated = {
name: name,
value: 2000,
state: 'active',
customers: {
erlinis: { quantity: 0, value: 2000 }
}
}

allow(fake_repository).to receive(:exists?).with(name).and_return(true)
allow(fake_repository).to receive(:find).with(name).and_return(service)
allow(fake_repository).to receive(:update).with(service_customer_updated)
end

it 'reset the customer quantity to zero' do
interactor.perform
expect(interactor.success?).to eq true
expect(interactor.message).to eq success_message
end
end
end
end
end

0 comments on commit 8b5584b

Please sign in to comment.