Class: Clickatell::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/clickatell/response.rb

Overview

Used to parse HTTP responses returned from Clickatell API calls.

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.parse(http_response) ⇒ Object

Returns the HTTP response body data as a hash.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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