class PostcodeAnywhere::Response::ParseJson

Constants

WHITESPACE_REGEX

Public Instance Methods

breakout_items(response_body) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 56
def breakout_items(response_body)
  if response_body.class == Hash && response_body.keys.include?(:items)
    return response_body[:items]
  end
  response_body
end
convert_boolean(value) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 40
def convert_boolean(value)
  return nil unless value
  return value unless value.class == String
  return true if (value.downcase == 'true')
  return false if (value.downcase == 'false')
  value
end
convert_hash_keys(value) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 29
def convert_hash_keys(value)
  case value
  when Array
    value.map { |v| convert_hash_keys(v) }
  when Hash
    Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
  else
    convert_boolean value
  end
end
error_klass_for(code) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 79
def error_klass_for(code)
  klass = PostcodeAnywhere::Error.postcode_anywhere_errors[code]
  if !klass
    if code > 1000
      klass = PostcodeAnywhere::Error::ServiceSpecificError
    else
      PostcodeAnywhere::Error::UnknownError
    end
  end
  klass
end
on_complete(response) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 48
def on_complete(response)
  response.body =
    parse(response.body) if
      respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
  raise_errors_in response.body
  response.body = breakout_items response.body
end
parse(body) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 8
def parse(body)
  case body
  when WHITESPACE_REGEX, nil
    nil
  else
    convert_hash_keys JSON.parse(body)
  end
end
raise_errors_in(response_body) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 63
def raise_errors_in(response_body)
  (
    first_response = response_body
    if response_body.class == Array
      first_response = response_body.first
    end
    if first_response.class == Hash
      if first_response.keys.include? :error
        code = first_response[:error].to_i
        klass = error_klass_for code
        fail(klass.from_response(first_response))
      end
    end
  ) if response_body
end
to_snake_case(string) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 17
def to_snake_case(string)
  string.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
  gsub(/([a-z\d])([A-Z])/, '\1_\2').
  tr('-', '_').
  downcase
end
underscore_key(k) click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 25
def underscore_key(k)
  to_snake_case(k.to_s).to_sym
end
unparsable_status_codes() click to toggle source
# File lib/postcode_anywhere/response/parse_json.rb, line 91
def unparsable_status_codes
  [204, 301, 302, 304, 400]
end