Class: UState::Dash

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/ustate/dash.rb,
lib/ustate/dash/controller/css.rb,
lib/ustate/dash/helper/renderer.rb,
lib/ustate/dash/controller/index.rb

Defined Under Namespace

Classes: Static

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clientObject



29
30
31
# File 'lib/ustate/dash.rb', line 29

def self.client
  @client ||= UState::Client.new(config[:client])
end

.configObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ustate/dash.rb', line 13

def self.config
  @config ||= {
    client: {},
    age_scale: 60 * 30,
    state_order: {
      'critical' => 3,
      'warning' => 2,
      'ok' => 1
    },
    strftime: '%H:%M:%S',
    controllers: [File.join(File.dirname(__FILE__), 'dash', 'controller')],
    helpers: [File.join(File.dirname(__FILE__), 'dash', 'helper')],
    views: 'views'
  }
end

.loadObject



33
34
35
36
37
38
# File 'lib/ustate/dash.rb', line 33

def self.load
  load_config
  config[:controllers].each { |d| load_controllers d }
  config[:helpers].each { |d| load_helpers d }
  set :views, File.expand_path(config[:views])
end

.load_configObject

Executes the configuration file.



41
42
43
# File 'lib/ustate/dash.rb', line 41

def self.load_config
  instance_eval File.read('config.rb')
end

.load_controllers(dir) ⇒ Object

Load controllers. Controllers can be regular old one-file-per-class, but if you prefer a little more modularity, this method will allow you to define all controller methods in their own files. For example, get “/posts/*/edit” can live in controller/posts/_/edit.rb. The sorting system provided here requires files in the correct order to handle wildcards appropriately.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ustate/dash.rb', line 51

def self.load_controllers(dir)
  rbs = []
  Find.find(
    File.expand_path(dir)
  ) do |path|
    rbs << path if path =~ /\.rb$/
  end

  # Sort paths with _ last, becase those are wildcards.
  rbs.sort! do |a, b|
    as = a.split File::SEPARATOR
    bs = b.split File::SEPARATOR

    # Compare common subpaths
    l = [as.size, bs.size].min
    catch :x do
      (0...l).each do |i|
        a, b = as[i], bs[i]
        if a[/^_/] and not b[/^_/]
          throw :x, 1
        elsif b[/^_/] and not a[/^_/]
          throw :x, -1
        elsif ord = (a <=> b) and ord != 0
          throw :x, ord
        end
      end

      # All subpaths are identical; sort longest first
      if as.size > bs.size
        throw :x, -1
      elsif as.size < bs.size
        throw :x, -1
      else
        throw :x, 0
      end
    end
  end

  rbs.each do |r|
    require r
  end 
end

.load_helpers(dir) ⇒ Object

Load helpers



95
96
97
98
99
100
101
# File 'lib/ustate/dash.rb', line 95

def self.load_helpers(dir)
  Find.find(
    File.expand_path(dir)
  ) do |path|
    require path if path =~ /\.rb$/
  end
end

.public_dir(dir) ⇒ Object

Add an additional public directory.



104
105
106
107
# File 'lib/ustate/dash.rb', line 104

def self.public_dir(dir)
  require 'ustate/dash/rack/static'
  use UState::Dash::Static, :root => dir
end

Instance Method Details

#clientObject



109
110
111
# File 'lib/ustate/dash.rb', line 109

def client
  self.class.client
end

#query(*a) ⇒ Object



113
114
115
# File 'lib/ustate/dash.rb', line 113

def query(*a)
  self.class.client.query(*a).states || []
end