Class: OMDBGateway::ResponseWrapper
- Inherits:
-
Object
- Object
- OMDBGateway::ResponseWrapper
- Defined in:
- lib/omdbgateway/response_wrapper.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#http_status ⇒ Object
readonly
Returns the value of attribute http_status.
Instance Method Summary collapse
- #app_success? ⇒ Boolean
- #array_first(default = nil) ⇒ Object
- #as_array ⇒ Object
- #as_hash ⇒ Object
- #http_success? ⇒ Boolean
-
#initialize(body, http_status = 200, http_error_message = nil) ⇒ ResponseWrapper
constructor
A new instance of ResponseWrapper.
- #prune_array(index, default = nil) ⇒ Object
- #prune_hash(tag, default = nil) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(body, http_status = 200, http_error_message = nil) ⇒ ResponseWrapper
Returns a new instance of ResponseWrapper.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/omdbgateway/response_wrapper.rb', line 5 def initialize body, http_status = 200, = nil @http_status = http_status @body = body @app_failed = false if http_success? if @body.instance_of?(Hash) if @body.key?('Response') && @body['Response'] == 'False' @app_failed = true if @body.key?('Error') = @body['Error'] end end end else = end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
3 4 5 |
# File 'lib/omdbgateway/response_wrapper.rb', line 3 def body @body end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
3 4 5 |
# File 'lib/omdbgateway/response_wrapper.rb', line 3 def end |
#http_status ⇒ Object (readonly)
Returns the value of attribute http_status.
3 4 5 |
# File 'lib/omdbgateway/response_wrapper.rb', line 3 def http_status @http_status end |
Instance Method Details
#app_success? ⇒ Boolean
27 28 29 |
# File 'lib/omdbgateway/response_wrapper.rb', line 27 def app_success? !@app_failed end |
#array_first(default = nil) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/omdbgateway/response_wrapper.rb', line 69 def array_first default = nil if !as_array.empty? as_array[0] else default end end |
#as_array ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/omdbgateway/response_wrapper.rb', line 43 def as_array if success? && @body.instance_of?(Array) @body else [] end end |
#as_hash ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/omdbgateway/response_wrapper.rb', line 35 def as_hash if success? && @body.instance_of?(Hash) @body else {} end end |
#http_success? ⇒ Boolean
23 24 25 |
# File 'lib/omdbgateway/response_wrapper.rb', line 23 def http_success?; @http_status >= 200 && @http_status < 300 end |
#prune_array(index, default = nil) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/omdbgateway/response_wrapper.rb', line 60 def prune_array index, default = nil if as_array.length > index @body = as_array[index] else @body = default end self end |
#prune_hash(tag, default = nil) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/omdbgateway/response_wrapper.rb', line 51 def prune_hash tag, default = nil if as_hash.has_key? tag @body = as_hash[tag] else @body = default end self end |
#success? ⇒ Boolean
31 32 33 |
# File 'lib/omdbgateway/response_wrapper.rb', line 31 def success?; http_success? && app_success? end |