class Inflect::Response
Responsible of encapsulate all the content of the service response.
Attributes
Response
attributes like served_by and query_words.
Response
content as itself.
If is not a valid Response
, errors lists the not valid attributes.
Response
timestamp.
Public Class Methods
Keys of the required attributes.
# File lib/inflect/response.rb, line 31 def self.attribute_keys @@attribute_keys = [:served_by, :query_words] end
@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
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
Returns the response in json format @return String
# File lib/inflect/response.rb, line 65 def to_json to_hash.to_json end
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
# File lib/inflect/response.rb, line 79 def splitted_instance_variables instance_variables.map { |var| var.to_s.split('@')[1] } end
# 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