Class: WeaselDiesel::Response

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

Overview

Response DSL class

Defined Under Namespace

Classes: Element, Params, Vector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



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

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

Instance Attribute Details

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

The list of all the arays inside the response

Returns:

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


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

def arrays
  @arrays
end

#elementsArray<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.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    the name of the element.

  • opts (Hash)

    the element options.

Yields:

  • (vector)

See Also:



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.

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:



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

Parameters:

  • The (String, Symbol)

    element name we want to match

Returns:



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

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

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

Lists all top level simple elements and array elements.

Returns:



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.

Parameters:

  • name (Symbol, String) (defaults to: nil)

    the name of the element.

  • opts (Hash) (defaults to: {})

    the options for the newly created element.

Yields:

Returns:



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

Returns:

  • (String)

    JSON representation of the response



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