Class: ServiceCore::Response
- Inherits:
-
Object
- Object
- ServiceCore::Response
- Defined in:
- lib/service_core/response.rb
Overview
Value object carrying the four-key service response.
Constant Summary collapse
- ALLOWED_KEYS =
%i[status data message errors].freeze
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Object
NOTE: writes police the four-key contract; reads (fetch, dig) follow Hash semantics so callers can treat Response like a Hash.
- #[]=(key, value) ⇒ Object
- #as_json(options = nil) ⇒ Object
- #dig(key, *rest) ⇒ Object
- #each_pair ⇒ Object (also: #each)
- #fetch(key, default = FETCH_DEFAULT_OMITTED, &block) ⇒ Object
-
#initialize(status: "initialized", data: nil, message: nil, errors: nil) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #key?(key) ⇒ Boolean (also: #has_key?, #include?)
- #keys ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(status: "initialized", data: nil, message: nil, errors: nil) ⇒ Response
Returns a new instance of Response.
12 13 14 15 16 17 |
# File 'lib/service_core/response.rb', line 12 def initialize(status: "initialized", data: nil, message: nil, errors: nil) @status = status @data = data @message = @errors = errors end |
Instance Method Details
#==(other) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/service_core/response.rb', line 76 def ==(other) case other when Response then to_h == other.to_h when Hash then to_h == other else super end end |
#[](key) ⇒ Object
NOTE: writes police the four-key contract; reads (fetch, dig) follow Hash semantics so callers can treat Response like a Hash.
21 22 23 24 |
# File 'lib/service_core/response.rb', line 21 def [](key) ensure_allowed_key!(key) public_send(key) end |
#[]=(key, value) ⇒ Object
26 27 28 29 |
# File 'lib/service_core/response.rb', line 26 def []=(key, value) ensure_allowed_key!(key) public_send(:"#{key}=", value) end |
#as_json(options = nil) ⇒ Object
93 94 95 |
# File 'lib/service_core/response.rb', line 93 def as_json( = nil) to_h.as_json() end |
#dig(key, *rest) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/service_core/response.rb', line 84 def dig(key, *rest) return nil unless ALLOWED_KEYS.include?(key) value = public_send(key) return value if rest.empty? || value.nil? value.respond_to?(:dig) ? value.dig(*rest) : nil end |
#each_pair ⇒ Object Also known as: each
53 54 55 56 57 |
# File 'lib/service_core/response.rb', line 53 def each_pair return to_enum(:each_pair) unless block_given? keys.each { |key| yield(key, public_send(key)) } end |
#fetch(key, default = FETCH_DEFAULT_OMITTED, &block) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/service_core/response.rb', line 31 def fetch(key, default = FETCH_DEFAULT_OMITTED, &block) return public_send(key) if key?(key) return block.call(key) if block return default unless default.equal?(FETCH_DEFAULT_OMITTED) raise(KeyError, "key not found: #{key.inspect}") end |
#inspect ⇒ Object
72 73 74 |
# File 'lib/service_core/response.rb', line 72 def inspect "#<#{self.class.name} #{to_h.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?, include?
39 40 41 |
# File 'lib/service_core/response.rb', line 39 def key?(key) ALLOWED_KEYS.include?(key) && !public_send(key).nil? end |
#keys ⇒ Object
45 46 47 |
# File 'lib/service_core/response.rb', line 45 def keys ALLOWED_KEYS.select { |key| key?(key) } end |
#to_h ⇒ Object Also known as: to_hash
60 61 62 63 64 65 |
# File 'lib/service_core/response.rb', line 60 def to_h ALLOWED_KEYS.each_with_object({}) do |key, hash| value = public_send(key) hash[key] = value unless value.nil? end end |
#to_json(*args) ⇒ Object
97 98 99 |
# File 'lib/service_core/response.rb', line 97 def to_json(*args) to_h.to_json(*args) end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/service_core/response.rb', line 68 def to_s to_h.to_s end |
#values ⇒ Object
49 50 51 |
# File 'lib/service_core/response.rb', line 49 def values keys.map { |key| public_send(key) } end |