Class: Sphero::Response::AsyncResponse

Inherits:
Sphero::Response show all
Defined in:
lib/sphero/response.rb

Direct Known Subclasses

CollisionDetected, SensorData

Constant Summary collapse

ID_CODE =
2
DLEN_MSB =
3
DLEN_LSB =
4
POWER_NOTIFICATION =
0x01
LEVEL_1_DIAGNOSTIC =
0x02
SENSOR_DATA =
0x03
CONFIG_BLOCK =
0x04
PRESLEEP_WARNING =
0x05
MACRO_MARKERS =
0x06
COLLISION_DETECTED =
0x07
VALID_REPONSES =
{POWER_NOTIFICATION => 'Sphero::Response::PowerNotification',
#LEVEL_1_DIAGNOSTIC => 'AsyncResponse',
SENSOR_DATA => 'Sphero::Response::SensorData',
#CONFIG_BLOCK => 'AsyncResponse',
#PRESLEEP_WARNING => 'AsyncResponse',
#MACRO_MARKERS => 'AsyncResponse',
COLLISION_DETECTED => 'Sphero::Response::CollisionDetected'}

Constants inherited from Sphero::Response

ASYNC_RESPONSE, CODE_OK, DLEN, MRSP, SEQ, SIMPLE_RESPONSE, SOP1, SOP2

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sphero::Response

async?, #body, #initialize, simple?, #valid?

Constructor Details

This class inherits a constructor from Sphero::Response

Class Method Details

.constantize(camel_cased_word) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/sphero/response.rb', line 132

def self.constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

.response(header, body) ⇒ Object



127
128
129
130
# File 'lib/sphero/response.rb', line 127

def self.response header, body
  raise "no good" unless self.valid?(header)
  constantize(VALID_REPONSES[header[ID_CODE]]).new(header, body)
end

.valid?(header) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/sphero/response.rb', line 123

def self.valid?(header)
  VALID_REPONSES.keys.include?(header[ID_CODE])
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/sphero/response.rb', line 143

def empty?
  @header[DLEN_LSB] == 1
end

#seqObject



151
152
153
# File 'lib/sphero/response.rb', line 151

def seq
  1
end

#success?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/sphero/response.rb', line 147

def success?
  AsyncResponse.valid?(@header)
end