Class: SBF::Client::Api::Response

Inherits:
BaseEntity show all
Defined in:
lib/stbaldricks/entities/response.rb

Constant Summary

Constants inherited from BaseEntity

BaseEntity::ELSE

Instance Attribute Summary collapse

Attributes inherited from BaseEntity

#errors

Instance Method Summary collapse

Methods inherited from BaseEntity

allow_instantiation?, attr_accessor, attr_reader, attr_writer, collection_attributes, defined_attributes, #destroyed?, #dirty_data, entity_attr_accessor, entity_attr_reader, entity_attr_writer, entity_attributes, entity_collection_attr_accessor, entity_collection_attr_reader, entity_collection_attr_writer, inherited, #keys_hash, #model_name, multitype_attr_accessor, multitype_attr_reader, multitype_attr_writer, multitype_collection_attr_accessor, multitype_collection_attr_reader, multitype_collection_attr_writer, #not_provided_attributes, optional_attributes, #persisted?, #reload!, #reload_recursive, #rollback!, #to_partial

Methods included from EntityResponseConcern

#add_errors, #error?, #errors?, #errors_http_code=

Constructor Details

#initialize(http_code: nil, data: nil, error: nil) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
# File 'lib/stbaldricks/entities/response.rb', line 9

def initialize(http_code: nil, data: nil, error: nil)
  @http_code = http_code
  @data = data
  @error = error || SBF::Client::ErrorEntity.new(nil)
  @errors = SBF::Client::Entity::Errors.new(self, http_code)
  add_errors(@error) unless @error.nil? || @error.empty?
  @errors.instance_variable_set(:@http_code, http_code)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/stbaldricks/entities/response.rb', line 7

def data
  @data
end

#errorObject

Returns the value of attribute error.



7
8
9
# File 'lib/stbaldricks/entities/response.rb', line 7

def error
  @error
end

#http_codeObject

Returns the value of attribute http_code.



7
8
9
# File 'lib/stbaldricks/entities/response.rb', line 7

def http_code
  @http_code
end

Instance Method Details

#success?Boolean

Returns:



18
19
20
# File 'lib/stbaldricks/entities/response.rb', line 18

def success?
  http_code.to_s.start_with?('2')
end

#to_hashObject



26
27
28
29
30
31
32
# File 'lib/stbaldricks/entities/response.rb', line 26

def to_hash
  {
    http_code: http_code,
    data: data,
    error: error
  }
end

#to_json(opts = nil) ⇒ Object



22
23
24
# File 'lib/stbaldricks/entities/response.rb', line 22

def to_json(opts = nil)
  to_hash.to_json(opts)
end