Class: Smsc::Response

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

Overview

Response parse

Class Method Summary collapse

Class Method Details

.parse(body, parse_configuration) ⇒ Object

Parse and wrap response in models, raise errors

Parameters:

  • body (String)

    response body

  • parse_configuration (Hash)

    response parse configuration

Raises:



14
15
16
17
18
19
# File 'lib/smsc/response.rb', line 14

def parse(body, parse_configuration)
  json = JSON.parse(body, symbolize_names: true)
  raise_client_errors(json, parse_configuration[:errors] || {})

  parse_configuration[:model].new(json)
end

.raise_client_errors(json, error_map) ⇒ Object

Raise errors if response contain error code

Raises:

  • (error_class)


24
25
26
27
28
29
30
31
32
# File 'lib/smsc/response.rb', line 24

def raise_client_errors(json, error_map)
  error_code = json[:error_code]
  error_message = json[:error]
  return if error_code.nil?

  error_class = error_map[error_code.to_i]
  raise error_class, error_message unless error_class.nil?
  raise Smsc::ClientError, error_message
end