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(name, type = nil, required = nil) ⇒ Element

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

Since:

  • 0.0.3



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/response.rb', line 81

def initialize(name, type=nil, required=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
  @required   = !(required==false)
  @attributes = []
  @vectors    = []
  # 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



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

def attributes
  @attributes
end

#docWSDSL::Documentation::ElementDoc (readonly)

Returns Response element documentation.

Returns:

Since:

  • 0.0.3



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

def doc
  @doc
end

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

Returns The optional nested elements.

Returns:

Since:

  • 0.0.3



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

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

#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



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

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



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

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



155
156
157
# File 'lib/response.rb', line 155

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



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/response.rb', line 109

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



174
175
176
177
178
179
# File 'lib/response.rb', line 174

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