Sha256: 26a71e77ec130e124f627816f5d285d5abb1ebb1ca3aed067d9aa81da5a0ab90

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

module GlobalSMS
  class SMS
    TIME = 'now'
    ENGLISH = 'O'
    TURKISH = '1'
    SELECTED = [:time, :turkish_character, :originator]

    attr_reader :api_key, :api_secret, :time, :turkish_character, :originator

    def initialize(args)
      @api_key = args[:api_key]
      @api_secret = args[:api_secret]
      @default_args = {
        time: TIME,
        turkish_character: ENGLISH
      }
      @default_args.merge!(args.select { |key, value| SELECTED.include?(key) })
    end

    def single_send(message)
      message = @default_args.merge(message)
      post_request('single', message)
    end

    def multi_send(messages)
      messages = messages.map { |message| @default_args.merge(message) }
      post_request('multi', messages)
    end

    def cancel(*args)
      args.each do |message_id|
        uri = "/sms/cancel/#{message_id}?key=#{@api_key}&secret=#{@api_secret}"
        get_request(uri)
      end
    end

    private

    def post_request(to, data)
      client = HTTPClient.new
      data = "data=#{data.to_json.to_s}"
      uri = "#{API_BASE_URL}/sms/send/#{to}?key=#{@api_key}&secret=#{@api_secret}"
      response = client.post(uri, data)
      JSON.parse(response.body)
    end

    def get_request(uri)
      client = HTTPClient.new
      response = client.get("#{API_BASE_URL}#{uri}")
      JSON.parse(response.body)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
globalsms-1.0.7 lib/globalsms/sms.rb
globalsms-1.0.6 lib/globalsms/sms.rb
globalsms-1.0.5 lib/globalsms/sms.rb