Class: Lita::Handlers::Spendo
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::Spendo
- Defined in:
- lib/lita/handlers/spendo.rb
Constant Summary collapse
- LAST_RECORD_KEY =
'last_record'
Instance Attribute Summary collapse
Instance Method Summary collapse
- #alert_level_changed?(previous, current) ⇒ Boolean
- #check_for_alerts ⇒ Object
- #create_message ⇒ Object
-
#load_billing_data ⇒ Object
read previous data from redis.
-
#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).
- #show(response) ⇒ Object
Instance Attribute Details
#billing_history ⇒ Object
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.aws_account_id, dynamodb_table: config.dynamodb_table } @billing_history = BillingHistory.new(params) end @billing_history end |
Instance Method Details
#alert_level_changed?(previous, current) ⇒ 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_alerts ⇒ Object
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) , url = target = Source.new(room: config.room) robot.(target, , url) else log.debug "alert level unchanged" end save_billing_data current_data end |
#create_message ⇒ Object
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 data = billing_history.latest account = data['Account'] current_fees = data['TotalFees'].to_f expected_fees = data['ExpectedFees'].to_f alert_level = data['AlertLevel'].to_i categorized_fees = data['FeesByCategory'] = "The current fees alert threshold has been reached.\n" << "\nAccount: #{account}" << "\nCurrent fees: $#{current_fees}" << "\nExpected monthly fees: $#{expected_fees}" # TODO << "\nFee level is at #{alert_level * 25}% of expected" << "\n\n Fee Category Breakdown\n\n" categorized_fees.each_pair do |k,v| value = v.to_f next if value == 0.0 << "#{k.ljust(20)}: $#{sprintf('%8.2f', value.round(2))}\n" end url = config.base_image_url + "/#{alert_level}.jpg" return [, url] end |
#load_billing_data ⇒ Object
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) , url = response.reply response.reply url end |