Class: Wit::REST::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/wit_ruby/rest/result.rb

Overview

Wrapper for all results that is returned by the session and API calls.

Direct Known Subclasses

Entity, Expression, Intent, Message, MultiEntity, MultiIntent

Instance Method Summary collapse

Constructor Details

#initialize(resultData, requestRest = nil, requestPath = nil, requestBody = nil) ⇒ Wit::REST::Result

Instantiates with a given hash and its REST parameters.

Parameters:

  • resultData (Hash)

    data from the call.

  • requestRest (String) (defaults to: nil)

    rest code for the call.

  • requestPath (String) (defaults to: nil)

    request path for the call.

  • requestBody (Hash) (defaults to: nil)

    body of the call.



19
20
21
22
23
24
25
26
27
28
# File 'lib/wit_ruby/rest/result.rb', line 19

def initialize(resultData, requestRest=nil, requestPath=nil, requestBody=nil)
  @rawdata = resultData
  @requestRest = requestRest
  @requestPath = requestPath
  @requestBody = requestBody
  ## Sets self class to be the last index of the split class name.
  @selfClass = self.class.name.split("::")[-1]
  ## Setup lists / instance variables given the current class that is inheriting
  setup_entities if ["Intent", "Expression"].include?(@selfClass)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(possible_key, *args, &block) ⇒ Class

Checks if the method is one of the keys in the hash. If it is then we can return the given value. If not, then raise a NoMethodError.

Parameters:

  • possible_key (Symbol)

    possible method or key in the hash

Returns:

  • (Class)

    depending on the given results in the data.



79
80
81
# File 'lib/wit_ruby/rest/result.rb', line 79

def method_missing(possible_key, *args, &block)
  @rawdata.has_key?(possible_key.to_s) ? @rawdata[possible_key.to_s] : super
end

Instance Method Details

#empty?Boolean

Checks to see if the data in the Result object is empty (empty array or hash).

Returns:

  • (Boolean)

    indicating if the current object is empty.



94
95
96
# File 'lib/wit_ruby/rest/result.rb', line 94

def empty?
  @rawdata.empty?
end

#entitiesArray

Returns the list of entities from the given results. Only applicable to a few objects. If the current class is not applicable, then raise an error.

Returns:

  • (Array)

    array of entities.



64
65
66
67
68
69
70
71
# File 'lib/wit_ruby/rest/result.rb', line 64

def entities
  unless ["Intent", "Expression"].include?(@selfClass)
    raise NoMethodError.new(%(The current class "#{@selfClass}' does not incorporate the '#{__method__}' method.))
  end

  ## Included so return it.
  return @entities
end

#raw_dataHash

Returns the orginalHash instance variable.

Returns:

  • (Hash)

    the raw data from the call.



33
34
35
# File 'lib/wit_ruby/rest/result.rb', line 33

def raw_data
  return @rawdata
end

#refreshable?Boolean

Method to check if this current object is refreshable. It is refresahble if the request parameters for checking is not nil;

Returns:

  • (Boolean)

    indicating if the current object is refreshable.



87
88
89
# File 'lib/wit_ruby/rest/result.rb', line 87

def refreshable?
  !@requestRest.nil?
end

#restBodyString

Returns the request’s body.

Returns:

  • (String)

    request body from the call.



47
48
49
# File 'lib/wit_ruby/rest/result.rb', line 47

def restBody
  return @requestBody
end

#restCodeString

Returns the REST code from the given request.

Returns:

  • (String)

    request code from the call.



40
41
42
# File 'lib/wit_ruby/rest/result.rb', line 40

def restCode
  return @requestRest
end

#restPathString

Returns the request’s path.

Returns:

  • (String)

    request path from the call.



54
55
56
# File 'lib/wit_ruby/rest/result.rb', line 54

def restPath
  return @requestPath
end