Sha256: a4cf3002c01d3e70929e0076c33a46a66db8b437c96a20b292139296bbb62c94

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require "instasent/version"
require "net/https"
require "json"

module Instasent
  class Client

    @@rootEndpoint = "http://api.instasent.com/"
    @@secureChannel = "https://api.instasent.com/"
    @@use_secure_channel = true

    def initialize(token, use_secure_channel=true)
      @@token = token
      @@use_secure_channel = use_secure_channel
    end

    def send_sms(sender, to, text, client_id='')

      if @use_secure_channel
        url = @@secureChannel + "sms/"
      else
        url = @@rootEndpoint + "sms/"
      end

      http_method = 'POST'

      data = {'from' => sender, 'to' => to, 'text' => text}

      response = self.execute_request(url, http_method, data)

      return response

    end

    def get_sms(page=1, per_page=10)

      if @use_secure_channel
        url = @@secureChannel + "sms/?page=" + page.to_s + "per_page=" + per_page.to_s
      else
        url = @@rootEndpoint + "sms/?page=" + page.to_s + "per_page=" + per_page.to_s
      end

      http_method = 'GET'

      response = self.execute_request(url, http_method)

      return response

    end

    def get_sms_by_id(id)

      if @@use_secure_channel
        url = @@secureChannel + 'sms/' + id
      else
        url = @@rootEndpoint + 'sms/' + id
      end

      http_method = 'GET'

      response = self.execute_request(url, http_method)

      return response

    end

    def execute_request(url='', http_method='', data='')

      url_parsed = URI.parse(url)

      if http_method=='GET'
        req = Net::HTTP::Get.new(url_parsed.to_s)
      else
        req = Net::HTTP::Post.new(url_parsed.to_s, initheader = {'Content-Type' => 'application/json'})
        req.body = data.to_json
      end

      req.add_field("Authorization", "Bearer "+@@token.to_s)

      res = Net::HTTP.start(url_parsed.host, url_parsed.port) { |http|
        http.request(req)
      }

      return {'response_body' => res.body, 'response_code' => res.code}

    end

  end


end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
instasent-0.1.3 lib/instasent.rb