Class: WSDSL::Response
- Inherits:
-
Object
- Object
- WSDSL::Response
- Defined in:
- lib/response.rb
Overview
Response DSL class
Defined Under Namespace
Classes: Element, Params, Vector
Instance Attribute Summary collapse
-
#arrays ⇒ Array<WSDSL::Response::Array>
readonly
The list of all the arays inside the response.
-
#elements ⇒ Array<WSDSL::Response::Element>
readonly
The list of all the elements inside the response.
Instance Method Summary collapse
-
#array(name, type = nil) {|vector| ... } ⇒ Object
Shortcut to automatically create a node of array type.
-
#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.
-
#element_named(name) ⇒ WSDSL::Response::Element
Returns a response element object based on its name.
-
#initialize ⇒ Response
constructor
A new instance of Response.
-
#nodes ⇒ Array<WSDSL::Response::Element, WSDSL::Response::Array>
Lists all top level simple elements and array elements.
-
#object {|element| ... } ⇒ WSDSL::Response::Element
Defines an anonymous element Useful for JSON response description.
-
#to_json(*args) ⇒ String
Converts the object into a JSON representation.
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
19 20 21 22 |
# File 'lib/response.rb', line 19 def initialize @elements = [] @arrays = [] end |
Instance Attribute Details
#arrays ⇒ Array<WSDSL::Response::Array> (readonly)
The list of all the arays inside the response
17 18 19 |
# File 'lib/response.rb', line 17 def arrays @arrays end |
#elements ⇒ Array<WSDSL::Response::Element> (readonly)
The list of all the elements inside the response
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.
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.
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
78 79 80 |
# File 'lib/response.rb', line 78 def element_named(name) @elements.find{|e| e.name.to_s == name.to_s} end |
#nodes ⇒ Array<WSDSL::Response::Element, WSDSL::Response::Array>
Lists all top level simple elements and array elements.
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
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
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 |