Class: Sonia::Widget Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/sonia/widget.rb

Overview

This class is abstract.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Widget

Initalizes the widget

Parameters:

  • config (Hash)

    Configuration of the widget from the config file


24
25
26
27
28
29
30
31
32
33
# File 'lib/sonia/widget.rb', line 24

def initialize(config)
  @log       = Sonia.log
  @channel   = EM::Channel.new
  @config    = config || Config.new({})
  @widget_id = Digest::SHA1.hexdigest([
    @config.to_hash.keys.map { |s| s.to_s }.sort.join,
    @config.to_hash.values.map { |s| s.to_s }.sort.join,
    self.class
  ].join)
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel


19
20
21
# File 'lib/sonia/widget.rb', line 19

def channel
  @channel
end

#configObject (readonly)

Returns the value of attribute config


19
20
21
# File 'lib/sonia/widget.rb', line 19

def config
  @config
end

#logObject (readonly)

Returns the value of attribute log


19
20
21
# File 'lib/sonia/widget.rb', line 19

def log
  @log
end

#sidObject (readonly)

Returns the value of attribute sid


19
20
21
# File 'lib/sonia/widget.rb', line 19

def sid
  @sid
end

#widget_idObject (readonly)

Returns the value of attribute widget_id


19
20
21
# File 'lib/sonia/widget.rb', line 19

def widget_id
  @widget_id
end

Class Method Details

.inherited(subclass) ⇒ Object


10
11
12
# File 'lib/sonia/widget.rb', line 10

def inherited(subclass)
  (@widgets ||= []) << subclass unless widgets.include?(subclass)
end

.widgetsObject


14
15
16
# File 'lib/sonia/widget.rb', line 14

def widgets
  @widgets ||= []
end

Instance Method Details

#encoderYajl::Encoder

Returns JSON encode

Returns:

  • (Yajl::Encoder)

38
39
40
# File 'lib/sonia/widget.rb', line 38

def encoder
  @encoder ||= Yajl::Encoder.new
end

#log_backtrace(exception) ⇒ Object


136
137
138
# File 'lib/sonia/widget.rb', line 136

def log_backtrace(exception)
  log.fatal(widget_name) { [exception.message, exception.backtrace].join("\n") }
end

#log_fatal_error(http) ⇒ Object


132
133
134
# File 'lib/sonia/widget.rb', line 132

def log_fatal_error(http)
  log.fatal(widget_name) { http.inspect }
end

#log_info(message) ⇒ Object


140
141
142
# File 'lib/sonia/widget.rb', line 140

def log_info(message)
  log.info(widget_name) { message }
end

#log_unsuccessful_response_body(response_body) ⇒ Object


128
129
130
# File 'lib/sonia/widget.rb', line 128

def log_unsuccessful_response_body(response_body)
  log.warn(widget_name) { "Bad response: #{response_body.inspect}" }
end

#parse_json(payload) ⇒ Hash

Parses JSON

Parameters:

  • payload (String)

    JSON string

Returns:

  • (Hash)

    Parsed JSON represented as a hash


53
54
55
# File 'lib/sonia/widget.rb', line 53

def parse_json(payload)
  Yajl::Parser.parse(payload)
end

#parse_xml(payload) ⇒ Nokogiri::XML::Document

Parse XML

Parameters:

  • payload (String)

    XML string

Returns:

  • (Nokogiri::XML::Document)

    Parsed Nokogiri document


69
70
71
# File 'lib/sonia/widget.rb', line 69

def parse_xml(payload)
  Nokogiri(payload)
end

#parse_yaml(payload) ⇒ Hash

Parses YAML

Parameters:

  • payload (String)

    YAML string

Returns:

  • (Hash)

    Parsed YAML represented as a hash


61
62
63
# File 'lib/sonia/widget.rb', line 61

def parse_yaml(payload)
  YAML.load(payload)
end

#parserYajl::Parser

Returns JSON parser

Returns:

  • (Yajl::Parser)

45
46
47
# File 'lib/sonia/widget.rb', line 45

def parser
  @decoder ||= Yajl::Parser.new
end

#push(msg) ⇒ Object

Pushes data to the channel

Parameters:

  • msg (Hash)

    Data which can be JSONified


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sonia/widget.rb', line 96

def push(msg)
  payload = {
    :payload   => msg,
    :widget    => self.widget_name,
    :widget_id => self.widget_id
  }

  message = { :message => payload }

  channel.push self.encoder.encode(message)
ensure
  log.info(widget_name) { "Pushing #{message.inspect} via #{channel}" }
end

#setupHash

Initial widget setup data that gets pushed to the client

Returns:

  • (Hash)

    Initial widget information and configuration


120
121
122
123
124
125
126
# File 'lib/sonia/widget.rb', line 120

def setup
  {
    :widget    => self.widget_name,
    :widget_id => self.widget_id,
    :config    => self.config.to_hash
  }
end

#subscribe!(websocket) ⇒ String

Subscribes a websocket to widget's data channel

Parameters:

  • websocket (EventMachine::WebSocket)

Returns:

  • (String)

    Subscriber ID


80
81
82
83
84
# File 'lib/sonia/widget.rb', line 80

def subscribe!(websocket)
  @sid = channel.subscribe { |msg| websocket.send msg }
ensure
  log.info(widget_name) { "Subscribed #{sid} via #{channel}" }
end

#unsubscribe!Object

Unsubscribes a subscriber id from data channel


87
88
89
90
91
# File 'lib/sonia/widget.rb', line 87

def unsubscribe!
  channel.unsubscribe(sid)
ensure
  log.info(widget_name) { "Unsubscribed #{sid} via #{channel}" }
end

#widget_nameString

Returns widget name

Returns:

  • (String)

    Name of the widget


113
114
115
# File 'lib/sonia/widget.rb', line 113

def widget_name
  self.class.name.split("::").last
end