Sha256: 62aa96fbe151078239df16a957bb3e137d08049b51383c617eb51d29a0177b8d

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require 'uri'
require 'net/http'
require 'net/https'

Net::HTTP.version_1_2 # to make sure version 1.2 is used

module OpenTok
  class Request

    def initialize(api_host, token, partner_id=nil, partner_secret=nil)
      @api_host       = api_host
      @token          = token
      @partner_id     = partner_id
      @partner_secret = partner_secret
    end

    def sendRequest(path, params)
      url = URI.parse(@api_host + path)

      if params.empty?
        req = Net::HTTP::Get.new(url.path)
      else
        req = Net::HTTP::Post.new(url.path)
        req.set_form_data(params)
      end

      if @token
        req.add_field 'X-TB-TOKEN-AUTH', @token
      elsif @partner_id && @partner_secret
        req.add_field 'X-TB-PARTNER-AUTH', "#{@partner_id}:#{@partner_secret}"
      end

      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = @api_host.start_with?("https")
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE

      res = http.start {|h| h.request(req) }
      return res
    end

    def fetch(path, params={})
      res = sendRequest(path, params)

      case res
      when Net::HTTPSuccess, Net::HTTPRedirection
        return res.read_body
      else
        res.error!
      end

    rescue Net::HTTPExceptions => e
      raise OpenTokException.new "Unable to create fufill request: #{e}"
    rescue NoMethodError => e
      raise OpenTokException.new "Unable to create a fufill request at this time: #{e}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opentok-0.0.92 lib/open_tok/request.rb
opentok-0.0.91 lib/open_tok/request.rb
opentok-0.0.9 lib/open_tok/request.rb
opentok-0.0.73 lib/open_tok/request.rb