Class: KazeClient::Response

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

Overview

Represents response from a Kaze API

See Also:

Author:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • response (HTTParty::Response)

    The response object from HTTParty call

Since:

  • 0.1.0



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

def initialize(response)
  @original_response = response
  @parsed_response   = response.parsed_response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Since:

  • 0.1.0



24
25
26
27
28
29
30
# File 'lib/kaze_client/response.rb', line 24

def method_missing(method_name, *args, &block)
  if %i[original_response parsed_response].include?(method_name)
    send(method_name)
  else
    @parsed_response.send(method_name, *args, &block)
  end
end

Instance Attribute Details

#original_responseHTTParty::Response (readonly)

Returns The response object from HTTParty call.

Returns:

  • (HTTParty::Response)

    The response object from HTTParty call

Since:

  • 0.1.0



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

def original_response
  @original_response
end

#parsed_responseHash (readonly)

Returns The JSON object resulting from HTTParty parsed response.

Returns:

  • (Hash)

    The JSON object resulting from HTTParty parsed response

Since:

  • 0.1.0



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

def parsed_response
  @parsed_response
end

Instance Method Details

#fetch_child(id) ⇒ Hash?

Find the child whose id is the given one in this response

Parameters:

  • id (String)

    The id of the child to find

Returns:

  • (Hash, nil)

    The child or nil if not found

See Also:

Since:

  • 0.1.0



41
42
43
# File 'lib/kaze_client/response.rb', line 41

def fetch_child(id)
  ::KazeClient::JsonUtils.fetch_node(self, %{$..children[?(@['id'] == '#{id}')]})
end

#fetch_data_from_child(id, field: 'data') ⇒ Object?

Find the first child with the given id and return the given field

Parameters:

  • id (String)

    The id of the child to find

  • field (String) (defaults to: 'data')

    The name of the searched field

Returns:

  • (Object, nil)

    The value of the field or nil if not found

See Also:

Since:

  • 0.1.0



51
52
53
# File 'lib/kaze_client/response.rb', line 51

def fetch_data_from_child(id, field: 'data')
  fetch_child(id)&.[](field)
end

#fetch_widgetsArray

Find all the widget in this response. Technically, it fetch all the children whose type is widget_*

Returns:

  • (Array)

    The list of widgets

See Also:

Since:

  • 0.1.0



60
61
62
# File 'lib/kaze_client/response.rb', line 60

def fetch_widgets
  ::KazeClient::JsonUtils.fetch_nodes(self, %{$..children[?(@['type'] =~ /^widget_/)]})
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0



32
33
34
# File 'lib/kaze_client/response.rb', line 32

def respond_to_missing?(method_name, include_private=false)
  %i[original_response parsed_response].include?(method_name) || super
end

#update_data_in_child(id, value, field: 'data') ⇒ KazeClient::Response

Find the first child with the given id and update value of the given field Does nothing when id was not found. Add a field when field is not found.

Parameters:

  • id (String)

    The id of the child to find

  • value (Object)

    The value to set on the field

  • field (String) (defaults to: 'data')

    The name of the searched field

Returns:

See Also:

Since:

  • 0.1.0



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

def update_data_in_child(id, value, field: 'data')
  fetch_child(id)&.[]=(field.to_s, value)

  self
end