Class: WeaselDiesel::Response::Element

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

Overview

The Response element class describing each element of a service response. Instances are usually not instantiated directly but via the Response#element accessor.

See Also:

Direct Known Subclasses

Vector

Defined Under Namespace

Classes: Attribute, MetaAttribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil) ⇒ Element

param [String, Symbol] name The name of the element param [String, Symbol] type The optional type of the element



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/response.rb', line 148

def initialize(name, type=nil)
  # sets a documentation placeholder since the response doc is defined at the same time
  # the response is defined.
  @doc        = Documentation::ElementDoc.new(name)
  @name       = name
  @type       = type
  @attributes = []
  @meta_attributes = []
  @elements   = []
  @vectors    = []
  @key        = nil
  # we don't need to initialize the nested elements, by default they should be nil
end

Instance Attribute Details

#attributesArray<WeaselDiesel::Response::Element::Attribute> (readonly) Also known as: properties

Returns An array of attributes.

Returns:



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

def attributes
  @attributes
end

#docWeaselDiesel::Documentation::ElementDoc (readonly)

Returns Response element documentation.

Returns:



134
135
136
# File 'lib/response.rb', line 134

def doc
  @doc
end

#elementsNilClass, Array<WeaselDiesel::Response::Element> (readonly) Also known as: objects

Returns The optional nested elements.

Returns:



137
138
139
# File 'lib/response.rb', line 137

def elements
  @elements
end

#key(name = nil, opts = {}) ⇒ Object (readonly)

Getter/setter for the key meta attribute. A key name can be used to lookup an object by a primary key for instance.

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the key attribute.

  • opts (Hash) (defaults to: {})

    the options attached with the key.



118
119
120
# File 'lib/response.rb', line 118

def key
  @key
end

#meta_attributesArray<WeaselDiesel::Response::Element::MetaAttribute> (readonly)

Returns An array of meta attributes.

Returns:



126
127
128
# File 'lib/response.rb', line 126

def meta_attributes
  @meta_attributes
end

#nameString, #to_s (readonly)

Returns The name of the element.

Returns:

  • (String, #to_s)

    The name of the element



112
113
114
# File 'lib/response.rb', line 112

def name
  @name
end

#type(name = nil, opts = {}) ⇒ Object (readonly)

Getter/setter for the type meta attribute.

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the type attribute.

  • opts (Hash) (defaults to: {})

    the options attached with the key.



115
116
117
# File 'lib/response.rb', line 115

def type
  @type
end

#vectorsArray (readonly)

Returns An array of vectors/arrays.

Returns:

  • (Array)

    An array of vectors/arrays



130
131
132
# File 'lib/response.rb', line 130

def vectors
  @vectors
end

Instance Method Details

#array(name, type = nil) {|Vector| ... } ⇒ Array<WeaselDiesel::Response::Vector>

Defines an array aka vector of elements.

Examples:

Defining an element array called ‘player_creation_rating’

element.array 'player_creation_rating', 'PlayerCreationRating' do |a|
  a.attribute :comments  => :string
  a.attribute :player_id => :integer
  a.attribute :rating    => :integer
  a.attribute :username  => :string
end

Parameters:

  • name (String, Symbol)

    The name of the array element.

  • type (String, Symbol) (defaults to: nil)

    Optional type information, useful to store the represented object types for instance.

  • &block (Proc)

    A block to execute against the newly created array.

Yields:

  • (Vector)

    the newly created array/vector instance

Returns:

See Also:



234
235
236
237
238
# File 'lib/response.rb', line 234

def array(name, type=nil)
  vector = Vector.new(name, type)
  yield(vector) if block_given?
  @vectors << vector
end

#arraysArray<WeaselDiesel::Response::Vector>

Returns the arrays/vectors contained in the response. This is an alias to access @vectors

Returns:

See Also:

  • @vectors


246
247
248
# File 'lib/response.rb', line 246

def arrays
  @vectors
end

#attribute(opts) ⇒ Array<WeaselDiesel::Response::Attribute>

sets a new attribute and returns the entire list of attributes

Examples:

Creation of a response attribute called ‘best_lap_time’

service.response do |response|
 response.element(:name => "my_stats", :type => 'Leaderboard') do |e|
   e.attribute "best_lap_time"       => :float,    :doc => "Best lap time in seconds."
 end
end

Parameters:

  • opts (Hash)

    An element’s attribute options

Options Hash (opts):

  • attribute_name (String, Symbol)

    The name of the attribute, the value being the type

  • :doc (String, Symbol)

    The attribute documentation

  • :mock (String, Symbol)

    An optional mock value used by service related tools

Returns:

  • (Array<WeaselDiesel::Response::Attribute>)

Raises:

  • (ArgumentError)


178
179
180
181
182
183
184
185
186
187
188
# File 'lib/response.rb', line 178

def attribute(opts)
  raise ArgumentError unless opts.is_a?(Hash)
  new_attribute = Attribute.new(opts)
  @attributes << new_attribute
  # document the attribute if description available
  # we might want to have a placeholder message when a response attribute isn't defined
  if opts.has_key?(:doc)
    @doc.attribute(new_attribute.name, opts[:doc])
  end
  @attributes
end

#boolean(name = nil, opts = {}) ⇒ Object

Shortcut to create a string attribute

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the attribute.

  • opts (Hash) (defaults to: {})

    the attribute options.



326
327
328
# File 'lib/response.rb', line 326

def boolean(name=nil, opts={})
  attribute({name => :boolean}.merge(opts))
end

#datetime(name = nil, opts = {}) ⇒ Object

Shortcut to create a string attribute

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the attribute.

  • opts (Hash) (defaults to: {})

    the attribute options.



334
335
336
# File 'lib/response.rb', line 334

def datetime(name=nil, opts={})
  attribute({name => :datetime}.merge(opts))
end

#element(opts = {}) {|WeaselDiesel::Response::Element| ... } ⇒ Array<WeaselDiesel::Response::Element>

Defines a new element and yields the content of an optional block Each new element is then stored in the elements array.

Examples:

create an element called ‘my_stats’.

service.response do |response|
 response.element(:name => "my_stats", :type => 'Leaderboard')
end

Parameters:

  • opts (Hash) (defaults to: {})

    Options used to define the element

Options Hash (opts):

  • :name (String, Symbol)

    The element name

  • :type (String, Symbol)

    The optional type

Yields:

Returns:



265
266
267
268
269
270
271
# File 'lib/response.rb', line 265

def element(opts={})
  el = Element.new(opts[:name], opts[:type])
  yield(el) if block_given?
  @elements ||= []
  @elements << el
  el
end

#float(name = nil, opts = {}) ⇒ Object

Shortcut to create a string attribute

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the attribute.

  • opts (Hash) (defaults to: {})

    the attribute options.



318
319
320
# File 'lib/response.rb', line 318

def float(name=nil, opts={})
  attribute({name => :float}.merge(opts))
end

#integer(name = nil, opts = {}) ⇒ Object

Shortcut to create a string attribute

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the attribute.

  • opts (Hash) (defaults to: {})

    the attribute options.



310
311
312
# File 'lib/response.rb', line 310

def integer(name=nil, opts={})
  attribute({name => :integer}.merge(opts))
end

#meta_attribute(opts) ⇒ Array<WeaselDiesel::Response::MetaAttribute>

sets a new meta attribute and returns the entire list of meta attributes

Examples:

Creation of a response attribute called ‘best_lap_time’

service.response do |response|
 response.element(:name => "my_stats", :type => 'Leaderboard') do |e|
   e.meta_attribute "id"       => :key
 end
end

Parameters:

  • opts (Hash)

    An element’s attribute options

Options Hash (opts):

  • attribute_name (String, Symbol)

    The name of the attribute, the value being the type

  • :mock (String, Symbol)

    An optional mock value used by service related tools

Returns:

  • (Array<WeaselDiesel::Response::MetaAttribute>)

Raises:

  • (ArgumentError)


205
206
207
208
209
210
211
# File 'lib/response.rb', line 205

def meta_attribute(opts)
  raise ArgumentError unless opts.is_a?(Hash)
  # extract the documentation part and add it where it belongs
  new_attribute = MetaAttribute.new(opts)
  @meta_attributes << new_attribute
  @meta_attributes
end

#object(name = nil, opts = {}, &block) ⇒ Object

Shortcut to create a new element.

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the element.

  • opts (Hash) (defaults to: {})

    the options for the newly created element.



277
278
279
# File 'lib/response.rb', line 277

def object(name=nil, opts={}, &block)
  element(opts.merge(:name => name), &block)
end

#string(name = nil, opts = {}) ⇒ Object

Shortcut to create a string attribute

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the attribute.

  • opts (Hash) (defaults to: {})

    the attribute options.



302
303
304
# File 'lib/response.rb', line 302

def string(name=nil, opts={})
  attribute({name => :string}.merge(opts))
end

#to_hash(root_node = true) ⇒ Hash

Converts an element into a hash representation

Parameters:

  • root_node (Boolean) (defaults to: true)

    true if this node has no parents.

Returns:

  • (Hash)

    the element attributes formated in a hash



342
343
344
345
346
347
348
349
350
351
# File 'lib/response.rb', line 342

def to_hash(root_node=true)
  attrs = {}
  attributes.each{ |attr| attrs[attr.name] = attr.type }
  (vectors + elements).each{ |el| attrs[el.name] = el.to_hash(false) }
  if self.class == Vector
    (root_node && name) ? {name => [attrs]} : [attrs]
  else
    (root_node && name) ? {name => attrs} : attrs
  end
end

#to_htmlObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/response.rb', line 360

def to_html
  output = ""
  if name
    output << "<li>"
    output << "<span class='label notice'>#{name}</span> of type <span class='label success'>#{self.is_a?(Vector) ? 'Array' : 'Object'}</span>"
  end
  if self.is_a? Vector
    output << "<h6>Properties of each array item:</h6>"
  else
    output << "<h6>Properties:</h6>"
  end
  output << "<ul>"
  properties.each do |prop|
    output << "<li><span class='label notice'>#{prop.name}</span> of type <span class='label success'>#{prop.type}</span> #{'(Can be blank or missing) ' if prop.opts && prop.opts.respond_to?(:[]) && prop.opts[:null]} "
    output <<  prop.doc unless prop.doc.nil? or prop.doc.empty?
    output << "</li>"
  end
  arrays.each{ |arr| output << arr.to_html }
  elements.each {|el| output << el.to_html } if elements
  output << "</ul>"
  output << "</li>" if name
  output
end

#to_jsonString

Converts an element into a json representation

Returns:

  • (String)

    the element attributes formated in a json structure



356
357
358
# File 'lib/response.rb', line 356

def to_json
  to_hash.to_json
end