Class: Inflect::Response
- Inherits:
-
Object
- Object
- Inflect::Response
- Defined in:
- lib/inflect/response.rb
Overview
Responsible of encapsulate all the content of the service response.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Response attributes like served_by and query_words.
-
#content ⇒ Object
readonly
Response content as itself.
-
#errors ⇒ Object
readonly
If is not a valid Response, errors lists the not valid attributes.
-
#timestamp ⇒ Object
readonly
Response timestamp.
Class Method Summary collapse
-
.attribute_keys ⇒ Object
Keys of the required attributes.
Instance Method Summary collapse
- #initialize(content = nil, attributes = {}) ⇒ Inflect::Response constructor
-
#to_hash ⇒ Object
Returns the Object as Hash.
-
#to_json ⇒ Object
Returns the response in json format.
-
#valid? ⇒ true, false
Validates required attributes for a valid response.
Constructor Details
#initialize(content = nil, attributes = {}) ⇒ Inflect::Response
23 24 25 26 27 28 |
# File 'lib/inflect/response.rb', line 23 def initialize(content=nil, attributes={}) @content = content = Time.now @errors = {} @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Response attributes like served_by and query_words.
11 12 13 |
# File 'lib/inflect/response.rb', line 11 def attributes @attributes end |
#content ⇒ Object (readonly)
Response content as itself.
9 10 11 |
# File 'lib/inflect/response.rb', line 9 def content @content end |
#errors ⇒ Object (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 |
#timestamp ⇒ Object (readonly)
Response timestamp.
13 14 15 |
# File 'lib/inflect/response.rb', line 13 def end |
Class Method Details
.attribute_keys ⇒ Object
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_hash ⇒ Object
Returns the Object as 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_json ⇒ Object
Returns the response in json format
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.
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 |