Class: Inflect::Response

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

Overview

Responsible of encapsulate all the content of the service response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil, attributes = {}) ⇒ Inflect::Response

Parameters:

  • content (String, Hash) (defaults to: nil)

    The response of the service. Required.

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

    Contains all the attributes of service response.

Options Hash (attributes):

  • :served_by (Class)

    Handler Service. Instance of AbstractService. Required.

  • :query_words (Array<String>)

    The list of words received on the request. Required.

  • :handled_word (String)

    The handled word by the service. Required.



23
24
25
26
27
28
# File 'lib/inflect/response.rb', line 23

def initialize(content=nil, attributes={})
  @content        =   content
  @timestamp      =   Time.now
  @errors         =   {}
  @attributes     =   attributes
end

Instance Attribute Details

#attributesObject (readonly)

Response attributes like served_by and query_words.



11
12
13
# File 'lib/inflect/response.rb', line 11

def attributes
  @attributes
end

#contentObject (readonly)

Response content as itself.



9
10
11
# File 'lib/inflect/response.rb', line 9

def content
  @content
end

#errorsObject (readonly)

If is not a valid Response, errors lists the not valid attributes.



15
16
17
# File 'lib/inflect/response.rb', line 15

def errors
  @errors
end

#timestampObject (readonly)

Response timestamp.



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

def timestamp
  @timestamp
end

Class Method Details

.attribute_keysObject

Keys of the required attributes.



31
32
33
# File 'lib/inflect/response.rb', line 31

def self.attribute_keys
  @@attribute_keys = [:served_by, :query_words]
end

Instance Method Details

#to_hashObject

Returns the Object as Hash

Returns:

  • Hash



56
57
58
59
60
61
# File 'lib/inflect/response.rb', line 56

def to_hash
  attrs = {}
  vars  = splitted_instance_variables
  vars.each { |var| attrs[var.to_s] = send(var) }
  attrs
end

#to_jsonObject

Returns the response in json format

Returns:

  • String



65
66
67
# File 'lib/inflect/response.rb', line 65

def to_json
  to_hash.to_json
end

#valid?true, false

Validates required attributes for a valid response.

Returns:

  • (true, false)


48
49
50
51
52
# File 'lib/inflect/response.rb', line 48

def valid?
  private_methods.grep(/valid_attribute/).collect do |method_name|
    self.send method_name
  end.all?
end