Class: WeaselDiesel::Response
- Inherits:
-
Object
- Object
- WeaselDiesel::Response
- Defined in:
- lib/response.rb
Overview
Response DSL class
Defined Under Namespace
Classes: Element, Params, Vector
Instance Attribute Summary collapse
-
#arrays ⇒ Array<WeaselDiesel::Response::Array>
readonly
The list of all the arays inside the response.
-
#elements ⇒ Array<WeaselDiesel::Response::Element>
readonly
The list of all the elements inside the response.
Instance Method Summary collapse
-
#array(name = nil, type = nil) {|vector| ... } ⇒ Object
Shortcut to automatically create a node of array type.
-
#element(opts = {}) {|WeaselDiesel::Response::Element| ... } ⇒ 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.
-
#element_named(name) ⇒ WeaselDiesel::Response::Element
Returns a response element object based on its name.
-
#initialize ⇒ Response
constructor
A new instance of Response.
-
#nodes ⇒ Array<WeaselDiesel::Response::Element, WeaselDiesel::Response::Array>
Lists all top level simple elements and array elements.
-
#object(name = nil, opts = {}) {|element(opts.merge(:name => name))| ... } ⇒ WeaselDiesel::Response
Defines an element/object in a consistent way with the way objects are defined in nested objects.
-
#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<WeaselDiesel::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<WeaselDiesel::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 = nil, 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=nil, type=nil) vector = Vector.new(name, type) yield(vector) if block_given? @arrays << vector end |
#element(opts = {}) {|WeaselDiesel::Response::Element| ... } ⇒ 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.
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) ⇒ WeaselDiesel::Response::Element
Returns a response element object based on its name
79 80 81 |
# File 'lib/response.rb', line 79 def element_named(name) @elements.find{|e| e.name.to_s == name.to_s} end |
#nodes ⇒ Array<WeaselDiesel::Response::Element, WeaselDiesel::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(name = nil, opts = {}) {|element(opts.merge(:name => name))| ... } ⇒ WeaselDiesel::Response
Defines an element/object in a consistent way with the way objects are defined in nested objects.
70 71 72 |
# File 'lib/response.rb', line 70 def object(name=nil, opts={}) yield element(opts.merge(:name => name)) end |
#to_json(*args) ⇒ String
Converts the object into a JSON representation
86 87 88 89 90 91 92 |
# File 'lib/response.rb', line 86 def to_json(*args) if nodes.size > 1 nodes.to_json(*args) else nodes.first.to_json(*args) end end |