Sha256: 7ce32b339b73f05823891f9af6087bb01fb0dcc05ee2631eea406a43932c6a98

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

require 'uri'
require 'httpi'

module Smess
  class HttpBase < Output
    include Smess::Logging

    def initialize(config)
      super
      HTTPI.log_level = :debug
    end


    def sender_id
      @sender_id || Smess.config.default_sender_id
    end

    private

    attr_reader :sms

    def from
      sms.originator || sender_id
    end

    def message_id
      @message_id ||= Digest::MD5.hexdigest "#{Time.now.strftime('%Y%m%d%H%M%S')}#{sms.to}-#{SecureRandom.hex(6)}"
    end

    def request
      @request ||= HTTPI::Request.new
    end

    def http_get(request)
      http_request(:get, request)
    end

    def http_post(request)
      http_request(:post, request)
    end

    def http_request(method, request)
      response = HTTPI.send(method, request)
      normal_result(response)
    end

    def result_for_error(e)
      {
        response_code: '-1',
        response: {
          temporaryError: 'true',
          responseCode: '-1',
          responseText: e.message
        },
        data: result_data
      }
    end

    def result_data
      {
        to: sms.to,
        text: sms.message.strip_nongsm_chars,
        from: from
      }
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smess-3.3.0 lib/smess/outputs/http_base.rb
smess-3.2.0 lib/smess/outputs/http_base.rb
smess-3.1.2 lib/smess/outputs/http_base.rb
smess-3.1.1 lib/smess/outputs/http_base.rb
smess-3.1.0 lib/smess/outputs/http_base.rb
smess-3.0.0 lib/smess/outputs/http_base.rb