Module: Appboard

Extended by:
Bootstrap, RestAPI
Defined in:
lib/appboard/version.rb,
lib/appboard.rb,
lib/appboard/widget.rb,
lib/appboard/rest_api.rb,
lib/appboard/bootstrap.rb,
lib/appboard/exceptions.rb,
lib/appboard/widgetpusher.rb,
lib/appboard/dashboardsetup.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Bootstrap, RestAPI, Version Classes: DashboardSetup, Error, Widget, WidgetPusher

Constant Summary collapse

@@dashboards =
{}

Class Method Summary collapse

Methods included from RestAPI

default_headers, send_data, send_setup

Methods included from Bootstrap

default_settings, settings, settings=

Class Method Details

.apiKey(value = nil) ⇒ Object

Define API key



24
25
26
27
# File 'lib/appboard.rb', line 24

def apiKey(value = nil)                                                                                                   
  @apiKey = value unless value.nil?                                                                                   
  @apiKey                                                                                     
end

.handle_error(e) ⇒ Object



167
168
169
170
171
172
# File 'lib/appboard.rb', line 167

def handle_error(e)
  log.error "#{e.class}: #{e.message}"#, :exception => e
  e.backtrace.each do |line|
    log.error line
  end
end

.logObject



174
175
176
# File 'lib/appboard.rb', line 174

def log
  @@log ||= Logger.new($stdout)
end

.log=(log) ⇒ Object



178
179
180
# File 'lib/appboard.rb', line 178

def log=(log)
  @@log = log
end

.push(options = {}, &block) ⇒ Object

Push on dashboard all defined widgets in block

appboard.push {

widget() {
  uid "..."

  data {
    # Data is here
  }
}

}

Options

  • :apiKey => ‘API key’

  • :logging => false

Refer to default config below for how to set these as defaults



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/appboard.rb', line 96

def push(options = {}, &block)
  config = settings.update(options)
  
  if block_given?
    pusher = WidgetPusher.new.instance_eval(&block)
           
    pusher.get_widgets.each do |widget|
      dashboard = nil
  
      if widget.name
        @@dashboards.each do |name, db|
          dashboard = db if db.has_widget(widget.name)
          break if dashboard
        end
      
        if dashboard
          widget_tmpl = dashboard.get_widget(widget.name)
          widget.uid(widget_tmpl.uid)
        end
      end          
      
      raise ArgumentError, "Widget 'uid' and 'name' are not defined" if widget.uid.nil? && widget.name.nil?

      apiKey = pusher.apiKey || config[:apiKey]
      url = "#{config[:url]}/#{config[:version]}/#{apiKey}/data/#{widget.uid}"
  
      send_data url, widget.get_data
    end
  else
    raise ArgumentError, "Block is expected as argument for Appboard.push()"
  end
end

.setup(options = {}, &block) ⇒ Object

Creates new or updates existing dashboard with defined widgets in block

appboard.setup {

dashboard('name') {
  width 5 

  widget('widget 1') {
    type "..."
    size "..."      
  }

  widget('widget 2') {
    # Widget configuration is here  
  }
}

}

Options

  • :apiKey => ‘API key’

  • :logging => false

  • :debug => false

Refer to default config below for how to set these as defaults



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/appboard.rb', line 53

def setup(options = {}, &block)
  config = settings.update(options)
  
  if block_given?
    ds = DashboardSetup.new.instance_eval(&block) 
    
    apiKey = ds.apiKey || config[:apiKey]
    url = "#{config[:url]}/#{config[:version]}/#{apiKey}/setup"
  
    result = send_setup url, ds
  
    if result && config[:debug]
      puts "Dashboard with name '#{ds.get_dashboard.name}' has been set, and following widgets have been added:"
  
      ds.get_dashboard.get_widgets.each do |widget|
        puts " * #{widget.name}, with uid '#{widget.uid}'"
      end
    end	
  
    @@dashboards[ds.get_dashboard.name] = ds if result
  else
    raise ArgumentError, "Block is expected as argument for Appboard.setup()"
  end
end

.widget(widget_name) ⇒ Object

Define a widget. May be called inside push loop or separately

widget(‘name’) {

data {
  # Data is here
}

}

Raises:

  • (ArgumentError)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/appboard.rb', line 137

def widget(widget_name)
  config = settings
  dashboard = nil
  
  @@dashboards.each do |name, db|
    dashboard = db if db.has_widget(widget_name)
    break if dashboard
  end
  
  raise ArgumentError, "Widget '#{widget_name}' is not defined, to use this method setup dashboard with Appboard.setup()" if dashboard.nil?
  
  if block_given?
    block = Proc.new
  
    apiKey = dashboard.apiKey || config[:apiKey]
    uid = dashboard.get_widget(widget_name).uid
    url = "#{config[:url]}/#{config[:version]}/#{apiKey}/data/#{uid}"
  
    widget = Widget.new(widget_name, &block)
    send_data url, widget.get_data
  else
    block = Proc.new {}
    
    widget = Widget.new(widget_name, &block)
    widget.uid(dashboard.get_widget(widget_name).uid)
  end 
  
  widget
end