blinkenstein 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,32 @@
1
+ require "logger"
2
+
1
3
  module Blinkenstein
4
+ class << self
5
+ attr_accessor :logger
6
+ end
7
+
8
+ @logger = Logger.new(STDOUT)
9
+ @logger.level = ENV["DEBUG"] ? Logger::DEBUG : Logger::INFO
10
+
2
11
  module Logging
3
- def logger
4
- @logger ||= Logging.logger_for(self.class.name)
12
+ def debug(*args)
13
+ Blinkenstein.logger.debug(*args) if Blinkenstein.logger
5
14
  end
6
15
 
7
- @loggers = {}
16
+ def info(*args)
17
+ Blinkenstein.logger.info(*args) if Blinkenstein.logger
18
+ end
19
+
20
+ def warn(*args)
21
+ Blinkenstein.logger.warn(*args) if Blinkenstein.logger
22
+ end
8
23
 
9
- class << self
10
- def logger_for(classname)
11
- @loggers[classname] ||= configure_logger_for(classname)
12
- end
24
+ def error(*args)
25
+ Blinkenstein.logger.error(*args) if Blinkenstein.logger
26
+ end
13
27
 
14
- def configure_logger_for(classname)
15
- logger = Logger.new(STDOUT)
16
- logger.progname = classname
17
- logger
18
- end
28
+ def fatal(*args)
29
+ Blinkenstein.logger.error(*args) if Blinkenstein.logger
19
30
  end
20
31
  end
21
32
  end
@@ -1,14 +1,13 @@
1
1
  module Blinkenstein
2
2
  module Monitor
3
- extend Logging
4
-
5
- def self.repository
6
- @repository ||= []
3
+ class << self
4
+ attr_accessor :monitors
7
5
  end
8
6
 
7
+ @monitors = []
8
+
9
9
  def self.included(klass)
10
- logger.info "Registering monitor #{klass}"
11
- repository << klass.new
10
+ @monitors << klass
12
11
  end
13
12
  end
14
13
  end
@@ -7,18 +7,25 @@ module Blinkenstein
7
7
  include Monitor
8
8
  include Logging
9
9
 
10
+ def initialize
11
+ @skillQueue ||= Eve::SkillQueue.new
12
+ end
13
+
10
14
  def refresh
11
15
  update_blink
12
16
  end
13
17
 
14
18
  def hours_left
15
- @skillQueue ||= Eve::SkillQueue.new
16
19
  @skillQueue.hours_left
20
+ rescue => e
21
+ error "Something is wrong: #{e}"
22
+ error e.backtrace.join("\n")
23
+ failure
17
24
  end
18
25
 
19
26
  def update_blink
20
27
  case
21
- when hours_left < 0 then error
28
+ when hours_left < 0 then failure
22
29
  when hours_left < 8 && hours_left >= 0 then panic
23
30
  when hours_left > 8 && hours_left <= 24 then nervous
24
31
  when hours_left > 24 then cool
@@ -26,22 +33,21 @@ module Blinkenstein
26
33
  end
27
34
 
28
35
  def cool
29
- logger.info "Everything is cool. #{hours_left}h left."
36
+ info "Everything is cool. #{hours_left}h left."
30
37
  Blink::Patterns.breath("#00ff00", 4, 0.2)
31
38
  end
32
39
 
33
40
  def nervous
34
- logger.info "There's room in the queue. #{hours_left}h left."
41
+ info "There's room in the queue. #{hours_left}h left."
35
42
  Blink::Patterns.breath("#ff0000", 3, 0.3)
36
43
  end
37
44
 
38
45
  def panic
39
- logger.info "Queue runs out soon. #{hours_left}h left."
46
+ info "Queue runs out soon. #{hours_left}h left."
40
47
  Blink::Patterns.police
41
48
  end
42
49
 
43
- def error
44
- logger.info "Ehm. Something is wrong"
50
+ def failure
45
51
  Blink::Patterns.breath("#ff0000", 0.25, 0.75)
46
52
  end
47
53
  end
@@ -1,4 +1,5 @@
1
1
  require 'celluloid'
2
+ require 'blink1'
2
3
 
3
4
  module Blinkenstein
4
5
  class Runner
@@ -6,6 +7,7 @@ module Blinkenstein
6
7
  include Logging
7
8
 
8
9
  def initialize
10
+ register_all
9
11
  refresh_all
10
12
 
11
13
  every(15) do
@@ -13,9 +15,23 @@ module Blinkenstein
13
15
  end
14
16
  end
15
17
 
18
+ def register_all
19
+ @monitors = Monitor.monitors.map do |monitor|
20
+ info "Registering montitor: #{monitor}"
21
+ monitor.new
22
+ end
23
+ end
24
+
16
25
  def refresh_all
17
- logger.debug "Refreshing all monitors"
18
- Monitor.repository.each(&:refresh)
26
+ debug "Refreshing all monitors"
27
+ @monitors.each(&:refresh)
28
+ end
29
+
30
+ def finalize
31
+ info "Shutting down. Turning off the lights..."
32
+ Blink1.open do |blink1|
33
+ blink1.off
34
+ end
19
35
  end
20
36
  end
21
37
  end
@@ -1,3 +1,3 @@
1
1
  module Blinkenstein
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/blinkenstein.rb CHANGED
@@ -12,3 +12,5 @@ module Blinkenstein
12
12
  end
13
13
 
14
14
  require "blinkenstein/monitors/eve_skill_queue_monitor"
15
+
16
+ Celluloid.logger.level = Logger::ERROR
data/lib/eve/base.rb CHANGED
@@ -14,7 +14,7 @@ module Eve
14
14
  begin
15
15
  config = YAML.load(File.read(File.expand_path('~/.eve-api')))
16
16
  rescue Errno::ENOENT
17
- raise "No ~/.eve-monitor config. The skill queue monitor can't start..."
17
+ raise "No ~/.eve-api config. The skill queue monitor can't start..."
18
18
  rescue Psych::SyntaxError
19
19
  raise "Invalid syntax in ~/.eve-api. The skill queue monitor can't start..."
20
20
  end
@@ -1,66 +1,68 @@
1
1
  require "eve/base"
2
2
 
3
3
  class Eve::SkillQueue < Eve::Base
4
- attr_reader :expire_time
4
+ include Blinkenstein::Logging
5
5
 
6
- def initialize
7
- @expire_time = Time.now - 1
8
- end
6
+ attr_reader :expire_time
9
7
 
10
- def hours_left
11
- refresh
8
+ def initialize
9
+ @expire_time = Time.now - 1
10
+ end
12
11
 
13
- return ((end_time - current_time) * 24).to_i if end_time
14
- return 0 if paused? || empty?
15
- return -1 if blocked?
16
- -1
17
- end
12
+ def hours_left
13
+ refresh
18
14
 
19
- def paused?
20
- return false if blocked?
15
+ return ((end_time - current_time) * 24).to_i if end_time
16
+ return 0 if paused? || empty?
17
+ return -1 if blocked?
18
+ -1
19
+ end
21
20
 
22
- last_skill.fetch("endTime", false) == ""
23
- end
21
+ def paused?
22
+ return false if blocked?
24
23
 
25
- def blocked?
26
- @response.fetch("eveapi", {}).fetch("error", false)
27
- end
24
+ last_skill.fetch("endTime", false) == ""
25
+ end
28
26
 
29
- def empty?
30
- return false if blocked?
31
-
32
- last_skill.empty?
33
- end
27
+ def blocked?
28
+ @response.fetch("eveapi", {}).fetch("error", false)
29
+ end
34
30
 
35
- def last_skill
36
- @last_skill ||= Array[@response.fetch("eveapi", {}).fetch("result", {}).fetch("rowset", {}).fetch("row", {})].flatten.last
37
- end
31
+ def empty?
32
+ return false if blocked?
38
33
 
39
- def current_time
40
- parse_date(@response.fetch("eveapi", {}).fetch("currentTime", {}))
41
- end
34
+ last_skill.empty?
35
+ end
42
36
 
43
- def cached_until
44
- parse_date(@response.fetch("eveapi", {}).fetch("cachedUntil", {}))
45
- end
37
+ def last_skill
38
+ @last_skill ||= Array[@response.fetch("eveapi", {}).fetch("result", {}).fetch("rowset", {}).fetch("row", {})].flatten.last
39
+ end
46
40
 
47
- def end_time
48
- parse_date(last_skill.fetch("endTime", ""))
49
- end
41
+ def current_time
42
+ parse_date(@response.fetch("eveapi", {}).fetch("currentTime", {}))
43
+ end
50
44
 
51
- def update_cache
52
- if current_time && cached_until
53
- @expire_time = Time.now + ((cached_until - current_time) * 24 * 60 * 60).to_i
54
- else
55
- @expire_time = Time.now + 60
56
- end
57
- end
45
+ def cached_until
46
+ parse_date(@response.fetch("eveapi", {}).fetch("cachedUntil", {}))
47
+ end
58
48
 
59
- def refresh
60
- return if @response && Time.now < @expire_time
49
+ def end_time
50
+ parse_date(last_skill.fetch("endTime", ""))
51
+ end
61
52
 
62
- @response = self.class.get('/char/SkillQueue.xml.aspx', query: query)
63
- update_cache
53
+ def update_cache
54
+ if current_time && cached_until
55
+ @expire_time = Time.now + ((cached_until - current_time) * 24 * 60 * 60).to_i
56
+ else
57
+ @expire_time = Time.now + 60
64
58
  end
65
59
  end
66
60
 
61
+ def refresh
62
+ return if @response && Time.now < @expire_time
63
+ info "Updating Skillqueue from Eve-API"
64
+ @response = self.class.get('/char/SkillQueue.xml.aspx', query: query)
65
+ update_cache
66
+ end
67
+ end
68
+
@@ -1,5 +1,5 @@
1
1
  require "spec_helper"
2
- require "blinkenstein/monitors/eve_skill_queue_monitor"
2
+ require "blinkenstein"
3
3
 
4
4
  module Blinkenstein
5
5
  describe EveSkillQueueMonitor do
@@ -8,7 +8,7 @@ module Blinkenstein
8
8
  describe "refresh" do
9
9
  it "goes into error mode if queue left is < 0" do
10
10
  monitor.stub(:hours_left).and_return(-1)
11
- monitor.should_receive(:error)
11
+ monitor.should_receive(:failure)
12
12
  monitor.refresh
13
13
  end
14
14
 
@@ -30,5 +30,13 @@ module Blinkenstein
30
30
  monitor.refresh
31
31
  end
32
32
  end
33
+
34
+ describe ".hours_left" do
35
+ it "is showing a failure if something is wrong while calling the API" do
36
+ Eve::SkillQueue.any_instance.stub(:hours_left).and_raise("whatever")
37
+ monitor.should_receive(:failure)
38
+ monitor.hours_left
39
+ end
40
+ end
33
41
  end
34
42
  end
@@ -1,3 +1,5 @@
1
+ require "spec_helper"
2
+
1
3
  module Eve
2
4
  describe Base do
3
5
  describe "parse_date" do
@@ -1,5 +1,4 @@
1
1
  require "spec_helper"
2
- require "eve/skill_queue"
3
2
 
4
3
  module Eve
5
4
  describe SkillQueue do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "blinkenstein/logging"
2
+ Blinkenstein.logger = nil
3
+
1
4
  Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
2
5
 
3
6
  RSpec.configure do |config|
@@ -11,3 +14,4 @@ RSpec.configure do |config|
11
14
  # --seed 1234
12
15
  config.order = 'random'
13
16
  end
17
+
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: blinkenstein
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Michael Schmidt
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2013-02-17 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -142,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  segments:
144
144
  - 0
145
- hash: 4488431557902824984
145
+ hash: 2791825359093468587
146
146
  version: '0'
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  - !ruby/object:Gem::Version
152
152
  segments:
153
153
  - 0
154
- hash: 4488431557902824984
154
+ hash: 2791825359093468587
155
155
  version: '0'
156
156
  requirements: []
157
157
  rubyforge_project: