Class: Gliffy::Response
- Inherits:
-
Object
- Object
- Gliffy::Response
- Defined in:
- lib/gliffy/response.rb
Overview
Base class for all response from gliffy
Constant Summary collapse
- @@normal_error_callback =
Proc.new do |response,exception| if response = response.inspect if response['response'] && response['response']['error'] http_status = response['response']['error']['http_status'] if http_status = "404" = "Not Found" else = "HTTP Status #{http_status}" end end raise exception.class.new() else raise exception end end
- @@error_callback =
@@normal_error_callback
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Class Method Summary collapse
-
.from_http_response(response, error_callback = nil) ⇒ Object
Factory for creating actual response subclasses.
-
.verify(response, error_callback) ⇒ Object
Verifies that the response represents success, calling the error callback if it doesn’t.
Instance Method Summary collapse
-
#initialize(params) ⇒ Response
constructor
A new instance of Response.
-
#method_missing(symbol, *args) ⇒ Object
Implements access to the object information.
Constructor Details
#initialize(params) ⇒ Response
Returns a new instance of Response.
86 87 88 |
# File 'lib/gliffy/response.rb', line 86 def initialize(params) @params = params end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
Implements access to the object information. Parameters should be typed appropriately. The names are those as defined by the Gliffy XSD, save for dashes are replaced with underscores.
94 95 96 97 98 99 100 |
# File 'lib/gliffy/response.rb', line 94 def method_missing(symbol,*args) if args.length == 0 @params[symbol] else super(symbol,args) end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
84 85 86 |
# File 'lib/gliffy/response.rb', line 84 def body @body end |
Class Method Details
.from_http_response(response, error_callback = nil) ⇒ Object
Factory for creating actual response subclasses. This takes the results of HTTParty’s response, which is a hash, essentially. This assumes that any checks for validity have been done.
- error_response
-
Set this to a Proc to handle errors if you don’t want the default behavior. The proc will get two arguments:
response
-
the raw response received (may be nil)
exception
-
One of NoResponseException, BadResponseException, or RequestFailedException. The message of that exception is a usable message if you want to ignore the exception
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gliffy/response.rb', line 52 def self.from_http_response(response,error_callback=nil) verify(response,error_callback) root = response['response'] klass = nil root.keys.each do |key| klassname = to_classname(key) begin this_klass = Gliffy.const_get(klassname) rescue NameError this_klass = nil end klass = this_klass unless this_klass.nil? end return Response.new(response.body) if !klass return klass.from_http_response(root) end |
.verify(response, error_callback) ⇒ Object
Verifies that the response represents success, calling the error callback if it doesn’t
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gliffy/response.rb', line 71 def self.verify(response,error_callback) error_callback = @@error_callback if error_callback.nil? return error_callback.call(response,NoResponseException.new('No response received at all')) if response.nil? return error_callback.call(response,BadResponseException.new('Not a Gliffy response')) if !response['response'] return error_callback.call(response,BadResponseException.new('No indication of success from Gliffy')) if !response['response']['success'] if response['response']['success'] != 'true' error = response['response']['error'] return error_callback.call(response,RequestFailedException.new('Request failed but no error inside response')) if !error return error_callback.call(response,RequestFailedException.new(error)) end end |