Class: Ovh::Http2sms::Response

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

Overview

Response object for OVH HTTP2SMS API responses

Parses responses in all supported formats: JSON, XML, HTML, and text/plain. Provides a unified interface for accessing response data.

Examples:

Successful response

response = Response.parse(body, content_type: "application/json")
response.success? # => true
response.sms_ids # => ["123456789"]
response.credits_remaining # => 1987.0

Error response

response = Response.parse(body, content_type: "application/json")
response.success? # => false
response.error_message # => "Missing message"

Constant Summary collapse

SUCCESS_CODES =

Success status codes

[100, 101].freeze
ERROR_CODES =

Error status codes

{
  201 => :missing_parameter,
  202 => :invalid_parameter,
  241 => :sender_not_found,
  401 => :authentication_error
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, credits_remaining: nil, sms_ids: [], error_message: nil, raw_response: nil, content_type: nil) ⇒ Response

rubocop:disable Metrics/ParameterLists



53
54
55
56
57
58
59
60
61
62
# File 'lib/ovh/http2sms/response.rb', line 53

def initialize(status:, credits_remaining: nil, sms_ids: [], error_message: nil,
               raw_response: nil, content_type: nil)
  # rubocop:enable Metrics/ParameterLists
  @status = status
  @credits_remaining = credits_remaining
  @sms_ids = Array(sms_ids).map(&:to_s)
  @error_message = error_message
  @raw_response = raw_response
  @content_type = content_type
end

Instance Attribute Details

#content_typeString (readonly)

Returns Content type of the response.

Returns:

  • (String)

    Content type of the response



39
40
41
# File 'lib/ovh/http2sms/response.rb', line 39

def content_type
  @content_type
end

#credits_remainingFloat? (readonly)

Returns Remaining SMS credits.

Returns:

  • (Float, nil)

    Remaining SMS credits



27
28
29
# File 'lib/ovh/http2sms/response.rb', line 27

def credits_remaining
  @credits_remaining
end

#error_messageString? (readonly)

Returns Error message if request failed.

Returns:

  • (String, nil)

    Error message if request failed



33
34
35
# File 'lib/ovh/http2sms/response.rb', line 33

def error_message
  @error_message
end

#raw_responseString (readonly)

Returns Raw response body.

Returns:

  • (String)

    Raw response body



36
37
38
# File 'lib/ovh/http2sms/response.rb', line 36

def raw_response
  @raw_response
end

#sms_idsArray<String> (readonly)

Returns SMS IDs for sent messages.

Returns:

  • (Array<String>)

    SMS IDs for sent messages



30
31
32
# File 'lib/ovh/http2sms/response.rb', line 30

def sms_ids
  @sms_ids
end

#statusInteger (readonly)

Returns API status code (100, 101 = success; 201, 202, 401 = error).

Returns:

  • (Integer)

    API status code (100, 101 = success; 201, 202, 401 = error)



24
25
26
# File 'lib/ovh/http2sms/response.rb', line 24

def status
  @status
end

Class Method Details

.parse(body, content_type: "text/plain") ⇒ Response

Parse a raw API response

Examples:

Parse JSON response

Response.parse('{"status":100,"creditLeft":"1987","SmsIds":["123"]}', content_type: "application/json")

Parameters:

  • body (String)

    Raw response body

  • content_type (String) (defaults to: "text/plain")

    Content-Type header value

Returns:



93
94
95
96
# File 'lib/ovh/http2sms/response.rb', line 93

def self.parse(body, content_type: "text/plain")
  parser = ResponseParser.new(body, content_type)
  parser.parse
end

Instance Method Details

#error_typeSymbol?

Get the error type based on status code

Returns:

  • (Symbol, nil)

    Error type (:missing_parameter, :invalid_parameter, :authentication_error)



81
82
83
# File 'lib/ovh/http2sms/response.rb', line 81

def error_type
  ERROR_CODES[status]
end

#failure?Boolean

Check if the request failed

Returns:

  • (Boolean)

    true if status code indicates failure



74
75
76
# File 'lib/ovh/http2sms/response.rb', line 74

def failure?
  !success?
end

#success?Boolean

Check if the request was successful

Returns:

  • (Boolean)

    true if status code indicates success (100 or 101)



67
68
69
# File 'lib/ovh/http2sms/response.rb', line 67

def success?
  SUCCESS_CODES.include?(status)
end