module Easemob

Constants

VERSION

Attributes

app_name[W]
base_url[W]
client_id[RW]
client_secret[RW]
http_pool[W]
http_timeout[W]
org_name[W]
token_file_path[W]

Public Class Methods

chatfile_url(chatfile_uuid) click to toggle source
# File lib/easemob.rb, line 70
def self.chatfile_url(chatfile_uuid)
  "#{@base_url}/#{@org_name}/#{@app_name}/chatfiles/#{chatfile_uuid}"
end
request(verb, resource, options = {}) click to toggle source

Make an HTTP request with the given verb to easemob server @param verb [Symbol] @param resource [String] @option options [Hash] @return [HTTP::Response]

# File lib/easemob.rb, line 40
def self.request(verb, resource, options = {})
  httprbs.with do |http|
    res = do_request(verb, http, resource, options)
    case res.code
    # 401:(未授权)请求要求身份验证。对于需要 token 的接口,服务器可能返回此响应。
    when 401
      Token.refresh
      res = do_request(verb, http, resource, options)
    # 408:(请求超时)服务器等候请求时发生超时。
    when 408
      res = do_request(verb, http, resource, options)
    # 503:(服务不可用)请求接口超过调用频率限制,即接口被限流。
    when 429, 503
      sleep 1
      res = do_request(verb, http, resource, options)
      raise QuotaLimitError, 'Return http status code is 429/503, hit quota limit of Easemob service,' if [429, 503].include?(res.code)
    end
    res
  end
end
token() click to toggle source

Get admin access token from easemob server @return access_token [String]

# File lib/easemob.rb, line 63
def self.token
  # Possible two worker running, one worker refresh token, other unaware, so must read every time
  access_token, remain_life_seconds = Token.read_from_store
  Token.refresh if remain_life_seconds < @random_generator.rand(30..3 * 60)
  access_token
end

Private Class Methods

do_request(verb, http, resource, options) click to toggle source
# File lib/easemob.rb, line 85
def self.do_request(verb, http, resource, options)
  http = http.headers('Authorization' => "Bearer #{token}")
  case verb
  when :upload
    restrict_access = options.delete(:restrict_access) || true
    http.headers('restrict-access' => restrict_access)
        .request(:post, "#{head_url}/#{resource}", options)
  when :download
    header = { 'Accept' => 'application/octet-stream' }
    share_secret = options.delete(:share_secret)
    header['share-secret'] = share_secret unless share_secret.nil?
    thumbnail = options.delete(:thumbnail)
    header['thumbnail'] = thumbnail if thumbnail
    http.headers(header)
        .request(:get, "#{head_url}/#{resource}")
  else
    http.request(verb, "#{head_url}/#{resource}", options)
  end
end
head_url() click to toggle source
# File lib/easemob.rb, line 109
def self.head_url
  "#{@base_url}/#{@org_name}/#{@app_name}"
end
httprbs() click to toggle source
# File lib/easemob.rb, line 105
def self.httprbs
  @httprbs ||= ConnectionPool.new(size: @http_pool, timeout: @http_timeout) { HTTP.persistent @base_url }
end
token_file_path() click to toggle source
# File lib/easemob.rb, line 113
def self.token_file_path
  @token_file_path || "/tmp/easemob_#{@org_name}_#{@app_name}_token"
end