Class: Ticuna::Response
- Inherits:
-
Object
- Object
- Ticuna::Response
- Defined in:
- lib/ticuna/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#parsed ⇒ Object
readonly
Returns the value of attribute parsed.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #errors? ⇒ Boolean
-
#initialize(raw, provider: nil) ⇒ Response
constructor
A new instance of Response.
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(raw, provider: nil) ⇒ Response
Returns a new instance of Response.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ticuna/response.rb', line 10 def initialize(raw, provider: nil) @provider = provider @raw_response = raw @parsed = case raw when String begin JSON.parse(raw) rescue JSON::ParserError { content: raw } end when Hash raw else raise ArgumentError, "Ticuna::Response only accepts Hash or String, received: #{raw.class}" end @data = deep_symbolize_keys(@parsed) error_payload = if @parsed.is_a?(Hash) @parsed["error"] || @parsed[:error] || [] else [] end @errors = deep_symbolize_keys(error_payload) @response = resolve_response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/ticuna/response.rb', line 54 def method_missing(name, *args, &block) value = @data[name] return wrap(value) if @data.key?(name) super end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/ticuna/response.rb', line 8 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/ticuna/response.rb', line 8 def errors @errors end |
#parsed ⇒ Object (readonly)
Returns the value of attribute parsed.
8 9 10 |
# File 'lib/ticuna/response.rb', line 8 def parsed @parsed end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
8 9 10 |
# File 'lib/ticuna/response.rb', line 8 def provider @provider end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
8 9 10 |
# File 'lib/ticuna/response.rb', line 8 def raw_response @raw_response end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
8 9 10 |
# File 'lib/ticuna/response.rb', line 8 def response @response end |
Instance Method Details
#[](key) ⇒ Object
38 39 40 |
# File 'lib/ticuna/response.rb', line 38 def [](key) @data[key.to_sym] end |
#errors? ⇒ Boolean
50 51 52 |
# File 'lib/ticuna/response.rb', line 50 def errors? !errors.empty? end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
61 62 63 |
# File 'lib/ticuna/response.rb', line 61 def respond_to_missing?(name, include_private = false) @data.key?(name) || super end |
#to_h ⇒ Object
42 43 44 |
# File 'lib/ticuna/response.rb', line 42 def to_h @data end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/ticuna/response.rb', line 46 def to_s @data.inspect end |