Class Clickatell::Response
In: lib/clickatell/response.rb
Parent: Object

Used to parse HTTP responses returned from Clickatell API calls.

Methods

parse  

Constants

PARSE_REGEX = /[A-Za-z0-9]+:.*?(?:(?=[A-Za-z0-9]+:)|$)/

Public Class methods

Returns the HTTP response body data as a hash.

[Source]

# File lib/clickatell/response.rb, line 12
      def parse(http_response)
        return { 'OK' => 'session_id' } if API.test_mode
        
        if http_response.body.scan(/ERR/).any?
          raise Clickatell::API::Error.parse(http_response.body)
        end
        results = http_response.body.split("\n").map do |line|
          # YAML.load converts integer strings that have leading zeroes into integers
          # using octal rather than decimal.  This isn't what we want, so we'll strip out any
          # leading zeroes in numbers here.
          response_fields = line.scan(PARSE_REGEX)
          response_fields = response_fields.collect { |field| field.gsub(/\b0+(\d+)\b/, '\1') }
          YAML.load(response_fields.join("\n"))
        end
        results.length == 1 ? results.first : results
      end

[Validate]