Class: Lita::Handlers::Spendo

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/spendo.rb

Constant Summary collapse

LAST_RECORD_KEY =
'last_record'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#billing_historyObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/lita/handlers/spendo.rb', line 63

def billing_history
  if @billing_history.nil?
    params = {
      aws_account_id: config.,
      dynamodb_table: config.dynamodb_table
    }
    @billing_history = BillingHistory.new(params)
  end
  @billing_history
end

Instance Method Details

#alert_level_changed?(previous, current) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/lita/handlers/spendo.rb', line 116

def alert_level_changed?(previous, current)
  previous['AlertLevel'].to_f != current['AlertLevel'].to_f
end

#check_for_alertsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lita/handlers/spendo.rb', line 120

def check_for_alerts
  log.debug "checking for alerts"
  current_data = billing_history.latest
  last_data    = load_billing_data

  if last_data && alert_level_changed?(last_data, current_data)
    message, url = create_message
    target = Source.new(room: config.room)
    robot.send_messages(target, message, url)
  else
    log.debug "alert level unchanged"
  end

  save_billing_data current_data
end

#create_messageObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lita/handlers/spendo.rb', line 74

def create_message
  data = billing_history.latest

            = data['Account']
  current_fees     = data['TotalFees'].to_f
  expected_fees    = data['ExpectedFees'].to_f
  alert_level      = data['AlertLevel'].to_i
  categorized_fees = data['FeesByCategory']

  message = "The current fees alert threshold has been reached.\n"
  message << "\nAccount: #{}"
  message << "\nCurrent fees: $#{current_fees}"
  message << "\nExpected monthly fees: $#{expected_fees}" # TODO
  message << "\nFee level is at #{alert_level * 25}% of expected"
  message << "\n\n Fee Category Breakdown\n\n"

  categorized_fees.each_pair do |k,v|
    value = v.to_f
    next if value == 0.0
    message << "#{k.ljust(20)}: $#{sprintf('%8.2f', value.round(2))}\n"
  end

  url = config.base_image_url + "/#{alert_level}.jpg"

  return [message, url]
end

#load_billing_dataObject

read previous data from redis



109
110
111
112
113
114
# File 'lib/lita/handlers/spendo.rb', line 109

def load_billing_data
  data = redis.get LAST_RECORD_KEY
  return nil if data.nil?

  JSON.parse(data)
end

#save_billing_data(data) ⇒ Object

write current data to redis BigDecimals get saved as strings which fail to deserialize as integers (but do deserialize as floats)



104
105
106
# File 'lib/lita/handlers/spendo.rb', line 104

def save_billing_data(data)
  redis.set LAST_RECORD_KEY, data.to_json
end

#show(response) ⇒ Object



54
55
56
57
58
59
# File 'lib/lita/handlers/spendo.rb', line 54

def show(response)
  message, url = create_message

  response.reply message
  response.reply url
end