lib/shorty/trim.rb in shorty-0.1.1 vs lib/shorty/trim.rb in shorty-0.1.2

- old
+ new

@@ -65,13 +65,12 @@ # api.trim_url(:url => 'http://google.com', :custom => 'thisismygoogle', :sandbox => 'true') # => http://tr.im/szMj def trim_url( options = {} ) options.merge!(@options) response = self.class.get( '/trim_url.xml', :query => options ) - response = Crack::XML.parse(response) - raise_error(response['trim']['status']['code'], response['trim']['status']['message']) if response['trim']['status']['code'] >= '205' - return response['trim']['url'] + response = handle_response(Crack::XML.parse(response)) + return response.url end TRIM_ERRORS = { 400 => Shorty::Trim::MissingParameter, # Required Parameter URL Not Submitted. 401 => Shorty::Trim::URLInvalid, # Submitted URL Invalid. @@ -102,9 +101,17 @@ # Raise an error depending on the problem def raise_error(code, message = '(no error message)') # raise Shorty::Trim::APIError.new(TRIM_ERRORS[code]) raise TRIM_ERRORS[code], message end + + # Process the response + def handle_response(resp) + response = resp['trim'].to_openstruct + raise_error(response.status.code, response.status.message) if response.status.code >= '205' + response + end + end end