class Lita::Interactors::AddQuantity

Adds the given quantity to the customer's balance

Constants

DEFAULT_QUANTITY

Attributes

data[R]

Public Class Methods

new(handler, data) click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 13
def initialize(handler, data)
  @handler = handler
  @data = data
end

Public Instance Methods

perform() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 18
def perform
  if service_exists?
    update_costumer_if_exist
  else
    @error = msg_not_found(service_name: name)
  end
  self
end

Private Instance Methods

customer_exists?() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 49
def customer_exists?
  service[:customers].keys.include?(customer_name.to_sym)
end
customer_name() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 33
def customer_name
  @customer_name ||= data[3].delete('@')
end
customer_quantity() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 37
def customer_quantity
  @customer_quantity ||= data[4].to_s
end
increment_quantity() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 71
def increment_quantity
  service[:customers][customer_name.to_sym][:quantity] += quantity_calculated
end
name() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 29
def name
  @name ||= data[1]
end
quantity_calculated() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 75
def quantity_calculated
  return customer_quantity.to_i unless customer_quantity.empty?
  DEFAULT_QUANTITY
end
service() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 41
def service
  @service ||= repository.find(name)
end
service_exists?() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 45
def service_exists?
  repository.exists?(name)
end
update_costumer_if_exist() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 53
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
update_customer_quantity() click to toggle source
# File lib/lita/interactors/add_quantity.rb, line 62
def update_customer_quantity
  new_quantity = increment_quantity
  repository.update(service)
  @message = I18n.t('lita.handlers.service.add.success',
                    quantity: quantity_calculated,
                    customer_name: customer_name,
                    customer_quantity: new_quantity)
end