lita-spendo 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0dee1763aed5676a874615dbdaa9ae9609d0b728
4
- data.tar.gz: 4b237c902a4fdca531c2248fc3cf1b92b371e291
3
+ metadata.gz: 529456b3bdf3fc77c3beaa9b53fa8637414df145
4
+ data.tar.gz: 43805a4f1e72389fb1cfd734200f98cb25611c77
5
5
  SHA512:
6
- metadata.gz: 9eb5d1886ec72288ff5587c6448e68309516e95e2234b39c549cd8f5619196144a452b7b43e63b47425c026fb5203646ce34c86ede91eec5fd6cf1c5dffc68f6
7
- data.tar.gz: 22855627345554166f97e8cc76e0e10d344af3509abb31b909c96c01199a6a4ddfcc4a50675d99345952f153963dbbd03e20e4551fe63771ebd979166429f169
6
+ metadata.gz: 3541b6ba32df59527cdaa955216f298b50be57dadef8661169fb56e06be5de7aad2da7a2113a236b6588d63e2842c734d6d47f86168919f11ca80e74a5b51cfc
7
+ data.tar.gz: a93c84f6d65caf9bab6815b953454f4900074d6792ae0685ee6946919f47b411b37c90eebd5bd5e7c49140e18e7e8b618da6a5842dfcc70aaf479003ef0e99d9
@@ -1,4 +1,5 @@
1
1
  require 'aws-sdk'
2
+ require 'json'
2
3
 
3
4
  module Lita
4
5
  module Handlers
@@ -26,15 +27,51 @@ module Lita
26
27
 
27
28
  class Spendo < Handler
28
29
 
30
+ LAST_RECORD_KEY = 'last_record'
31
+
29
32
  config :aws_account_id, type: String
30
33
  config :dynamodb_table, type: String, default: 'BillingHistory'
31
34
  config :base_image_url, type: String
35
+ config :room, type: String
36
+ config :time_between_polls, type: Integer, default: 60*60
32
37
 
33
38
  route(/^spendo$/, :show, command: true, help: {
34
39
  "spendo" => "show current billing level"
35
40
  })
36
41
 
42
+ on(:connected) do |payload|
43
+ if config.room
44
+ # join the alert room
45
+ robot.join config.room
46
+ end
47
+
48
+ # set up a timer to poll DynamoDB
49
+ every(config.time_between_polls) do |timer|
50
+ check_for_alerts
51
+ end
52
+ end
53
+
37
54
  def show(response)
55
+ message, url = create_message
56
+
57
+ response.reply message
58
+ response.reply url
59
+ end
60
+
61
+ attr_writer :billing_history
62
+
63
+ def billing_history
64
+ if @billing_history.nil?
65
+ params = {
66
+ aws_account_id: config.aws_account_id,
67
+ dynamodb_table: config.dynamodb_table
68
+ }
69
+ @billing_history = BillingHistory.new(params)
70
+ end
71
+ @billing_history
72
+ end
73
+
74
+ def create_message
38
75
  data = billing_history.latest
39
76
 
40
77
  account = data['Account']
@@ -58,21 +95,42 @@ module Lita
58
95
 
59
96
  url = config.base_image_url + "/#{alert_level}.jpg"
60
97
 
61
- response.reply message
62
- response.reply url
98
+ return [message, url]
63
99
  end
64
100
 
65
- attr_writer :billing_history
101
+ # write current data to redis
102
+ # BigDecimals get saved as strings which fail to
103
+ # deserialize as integers (but do deserialize as floats)
104
+ def save_billing_data(data)
105
+ redis.set LAST_RECORD_KEY, data.to_json
106
+ end
66
107
 
67
- def billing_history
68
- if @billing_history.nil?
69
- params = {
70
- aws_account_id: config.aws_account_id,
71
- dynamodb_table: config.dynamodb_table
72
- }
73
- @billing_history = BillingHistory.new(params)
108
+ # read previous data from redis
109
+ def load_billing_data
110
+ data = redis.get LAST_RECORD_KEY
111
+ return nil if data.nil?
112
+
113
+ JSON.parse(data)
114
+ end
115
+
116
+ def alert_level_changed?(previous, current)
117
+ previous['AlertLevel'].to_f != current['AlertLevel'].to_f
118
+ end
119
+
120
+ def check_for_alerts
121
+ log.debug "checking for alerts"
122
+ current_data = billing_history.latest
123
+ last_data = load_billing_data
124
+
125
+ if last_data && alert_level_changed?(last_data, current_data)
126
+ message, url = create_message
127
+ target = Source.new(room: config.room)
128
+ robot.send_messages(target, message, url)
129
+ else
130
+ log.debug "alert level unchanged"
74
131
  end
75
- @billing_history
132
+
133
+ save_billing_data current_data
76
134
  end
77
135
  end
78
136
 
data/lita-spendo.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-spendo"
3
- spec.version = "0.1.2"
3
+ spec.version = "0.2.0"
4
4
  spec.authors = ["Chris Chalfant"]
5
5
  spec.email = ["cchalfant@leafsoftwaresolutions.com"]
6
6
  spec.description = "Lita handler for displaying AWS billing"
@@ -31,6 +31,19 @@ describe Lita::Handlers::Spendo, lita_handler: true do
31
31
  it 'shows the url' do
32
32
  expect(replies).to include("http://foo.com/3.jpg")
33
33
  end
34
+ end
35
+
36
+ describe '#alert_level_changed?' do
37
+ it 'is true if the levels are different' do
38
+ prev = {'AlertLevel' => "1"}
39
+ curr = {'AlertLevel' => "2"}
40
+ expect(subject.alert_level_changed?(prev, curr)).to be_truthy
41
+ end
34
42
 
43
+ it 'is false if the levels are same' do
44
+ prev = {'AlertLevel' => "1"}
45
+ curr = {'AlertLevel' => "1"}
46
+ expect(subject.alert_level_changed?(prev, curr)).to be_falsey
47
+ end
35
48
  end
36
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-spendo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Chalfant
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita