Class: Gliffy::Response

Inherits:
Object
  • Object
show all
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
    message = response.inspect
    if response['response'] && response['response']['error']
      http_status = response['response']['error']['http_status']
      # It seems that HTTParty is not able to figure out the http-status when the contents of
      # the tag is text.  Weird.  This hack seems to work reasonably well.
      if !http_status
        http_status = response.body.gsub(/^.*http-status=\"/,'').gsub(/\".*$/,'')
      end
      if http_status == '404'
        message = 'Not Found'
      elsif http_status == '401'
        raise BadAuthorizationException.new(response['response']['error'])
      else
        message = "HTTP Status #{http_status} : #{response['response']['error']}"
      end
    end
    raise exception.class.new(message)
  else
    raise exception 
  end
end
@@error_callback =
@@normal_error_callback

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Response

Returns a new instance of Response.



99
100
101
# File 'lib/gliffy/response.rb', line 99

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.



107
108
109
110
111
112
113
# File 'lib/gliffy/response.rb', line 107

def method_missing(symbol,*args)
  if args.length == 0
    @params[symbol]
  else
    super(symbol,args)
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



97
98
99
# File 'lib/gliffy/response.rb', line 97

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gliffy/response.rb', line 65

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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gliffy/response.rb', line 84

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