Sha256: 5845085fca5085b94dceb677a9a127b57d3564488f992eeb1d7097c9dbedab4b

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

module Zenvia
  class Request
    BASE_URL  = "http://www.zenvia360.com.br"
    POST_PATH = "/GatewayIntegration/msgSms.do"

    attr_reader :params, :response

    def initialize
      session.base_url = BASE_URL
      session.timeout  = Zenvia.config.timeout
    end

    def post(params)
      @params   = params
      @response = session.post POST_PATH, parse_params

      parse_response
    end

    private

    def parse_response
      code, message = response.body.split " - "
      raise Error, message if code != "000"

      {code: code, message: message}
    end

    def parse_params
      {
        account:        Zenvia.config.account,
        code:           Zenvia.config.code,
        dispatch:       params[:dispatch],
        to:             params[:to],
        msg:            params[:message],
        id:             params[:id],
        callbackOption: params[:callback_option]
      }
    end

    def session
      @session ||= Patron::Session.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zenvia-0.0.4 lib/zenvia/request.rb