Class: WSDSL::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 instiated directly but via the Response#element accessor.

See Also:

Since:

  • 0.0.3

Defined Under Namespace

Classes: Attribute, Vector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Element

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

Since:

  • 0.0.3



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/response.rb', line 84

def initialize(opts={})
  opts[:type] ||= nil
  opts[:required] = nil if !opts.has_key?(:required)

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

Instance Attribute Details

#attributesArray<WSDSL::Response::Element::Attribute> (readonly)

Returns An array of attributes.

Returns:

Since:

  • 0.0.3



68
69
70
# File 'lib/response.rb', line 68

def attributes
  @attributes
end

#docWSDSL::Documentation::ElementDoc (readonly)

Returns Response element documentation.

Returns:

Since:

  • 0.0.3



76
77
78
# File 'lib/response.rb', line 76

def doc
  @doc
end

#elementsNilClass, Array<WSDSL::Response::Element> (readonly)

Returns The optional nested elements.

Returns:

Since:

  • 0.0.3



79
80
81
# File 'lib/response.rb', line 79

def elements
  @elements
end

#nameString, #to_s (readonly)

Returns The name of the element.

Returns:

  • (String, #to_s)

    The name of the element

Since:

  • 0.0.3



55
56
57
# File 'lib/response.rb', line 55

def name
  @name
end

#optsObject (readonly)

Since:

  • 0.0.3



64
65
66
# File 'lib/response.rb', line 64

def opts
  @opts
end

#requiredObject (readonly)

Since:

  • 0.0.3



58
59
60
# File 'lib/response.rb', line 58

def required
  @required
end

#typeObject (readonly)

Since:

  • 0.0.3



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

def type
  @type
end

#vectorsArray (readonly)

Returns An array of vectors/arrays.

Returns:

  • (Array)

    An array of vectors/arrays

Since:

  • 0.0.3



72
73
74
# File 'lib/response.rb', line 72

def vectors
  @vectors
end

Instance Method Details

#array(opts) {|Vector| ... } ⇒ Array<WSDSL::Response::Element::Vector>

Defines an array aka vector of elements.

Examples:

Defining an element array called ‘player_creation_rating’

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

Parameters:

  • opts (Hash)

    A hash representing the array information, usually a name and a type.

  • &block (Proc)

    A block to execute against the newly created array.

Options Hash (opts):

  • :name (String, Symbol)

    The name of the defined array

  • :type (String, Symbol)

    The class name of the element inside the array

Yields:

  • (Vector)

    the newly created array/vector instance

Returns:

See Also:

Since:

  • 0.0.3



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

def array(opts)
  vector = Vector.new(opts)
  yield(vector) if block_given?
  @vectors << vector
end

#arraysArray<WSDSL::Response::Element::Vector>

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

Returns:

See Also:

  • @vectors

Since:

  • 0.0.3



162
163
164
# File 'lib/response.rb', line 162

def arrays
  @vectors
end

#attribute(opts) ⇒ Array<WSDSL::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<WSDSL::Response::Attribute>)

Raises:

  • (ArgumentError)

Since:

  • 0.0.3



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/response.rb', line 116

def attribute(opts)
  raise ArgumentError unless opts.is_a?(Hash)
  # extract the documentation part and add it where it belongs
  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

#element(opts = {}) {|WSDSL::Response::Element| ... } ⇒ Array<WSDSL::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:

Since:

  • 0.0.3



181
182
183
184
185
186
# File 'lib/response.rb', line 181

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