Sha256: 95cd77623b08f5dd937710738e36b4b51295726e42152f9a5a0eb80677491902

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module OpenMarket
  module SMS
    module Responses

      class SendMessage
        attr_reader :message_id, :status, :headers, :body

        def initialize(request, service_response)
          @request = request
          @response = service_response

          parse_response
        end

        def success?
          status == 202
        end

        def from
          @request.from
        end

        def to
          @request.to
        end

        def location
          @response.headers['location']
        end

        def request_id
          @response.headers['x-request-id']
        end

        def error_message
          body.dig('error', 'description')
        end

        def error_code
          body.dig('error', 'code')&.to_i
        end

        def to_s
          "Response: #{status}, #{error}"
        end

        private

        def parse_response
          @status = @response.status

          parse_message_id
          parse_body
        end

        def parse_message_id
          return unless success?

          location = @response.headers['location']
          *, @message_id = location.split('/')
        end

        def parse_body
          @body = \
            if !@response.body || @response.body.empty?
              {}
            else
              JSON.parse(@response.body)
            end
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sms_broker-1.0.8 lib/open_market/sms/responses/send_message.rb
sms_broker-1.0.7 lib/open_market/sms/responses/send_message.rb