class Inflect::Response

Responsible of encapsulate all the content of the service response.

Attributes

attributes[R]

Response attributes like served_by and query_words.

content[R]

Response content as itself.

errors[R]

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

timestamp[R]

Response timestamp.

Public Class Methods

attribute_keys() click to toggle source

Keys of the required attributes.

# File lib/inflect/response.rb, line 31
def self.attribute_keys
  @@attribute_keys = [:served_by, :query_words]
end
new(content=nil, attributes={}) click to toggle source

@param content [String, Hash] The response of the service. Required. @param attributes [Hash] Contains all the attributes of service response. @option attributes [Class] :served_by Handler Service. Instance of AbstractService. Required. @option attributes [Array<String>] :query_words The list of words received on the request. Required. @option attributes [String] :handled_word The handled word by the service. Required. @return [Inflect::Response]

# File lib/inflect/response.rb, line 23
def initialize(content=nil, attributes={})
  @content        =   content
  @timestamp      =   Time.now
  @errors         =   {}
  @attributes     =   attributes
end

Public Instance Methods

to_hash() click to toggle source

Returns the Object as Hash @return Hash

# 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_json() click to toggle source

Returns the response in json format @return String

# File lib/inflect/response.rb, line 65
def to_json
  to_hash.to_json
end
valid?() click to toggle source

Validates required attributes for a valid response. @return [true, false]

# 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

Private Instance Methods

splitted_instance_variables() click to toggle source
# File lib/inflect/response.rb, line 79
def splitted_instance_variables
  instance_variables.map { |var| var.to_s.split('@')[1] }
end
valid_attribute_content() click to toggle source
# File lib/inflect/response.rb, line 71
def valid_attribute_content
  unless [String, Hash].include? content.class
    errors[:content] = I18n.errors('content')
    return false
  end
  true
end