Class: WSDSL::Response

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

Overview

Response DSL class

Since:

  • 0.0.3

Defined Under Namespace

Classes: Element, Params, Vector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.

Since:

  • 0.0.3



19
20
21
22
# File 'lib/response.rb', line 19

def initialize
  @elements = []
  @arrays  = []
end

Instance Attribute Details

#arraysArray<WSDSL::Response::Array> (readonly)

The list of all the arays inside the response

Returns:

  • (Array<WSDSL::Response::Array>)

Since:

  • 0.0.3



17
18
19
# File 'lib/response.rb', line 17

def arrays
  @arrays
end

#elementsArray<WSDSL::Response::Element> (readonly)

The list of all the elements inside the response

Returns:

Since:

  • 0.0.3



12
13
14
# File 'lib/response.rb', line 12

def elements
  @elements
end

Instance Method Details

#array(name, type = nil) {|vector| ... } ⇒ Object

Shortcut to automatically create a node of array type. Useful when describing a JSON response.

Parameters:

  • name (String, Symbol)

    the name of the element.

  • opts (Hash)

    the element options.

Yields:

  • (vector)

See Also:

Since:

  • 0.0.3



37
38
39
40
41
# File 'lib/response.rb', line 37

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

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



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

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

#element_named(name) ⇒ WSDSL::Response::Element

Returns a response element object based on its name

Parameters:

  • The (String, Symbol)

    element name we want to match

Returns:

Since:

  • 0.0.3



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

def element_named(name)
  @elements.find{|e| e.name.to_s == name.to_s}
end

#nodesArray<WSDSL::Response::Element, WSDSL::Response::Array>

Lists all top level simple elements and array elements.

Returns:

Since:

  • 0.0.3



27
28
29
# File 'lib/response.rb', line 27

def nodes 
  elements + arrays
end

#object {|element| ... } ⇒ WSDSL::Response::Element

Defines an anonymous element Useful for JSON response description

Yields:

Returns:

Since:

  • 0.0.3



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

def object
  yield element
end

#to_json(*args) ⇒ String

Converts the object into a JSON representation

Returns:

  • (String)

    JSON representation of the response

Since:

  • 0.0.3



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

def to_json(*args)
  if nodes.size > 1
    nodes.to_json(*args)
  else
    nodes.first.to_json(*args)
  end
end