Module: Easemob
- Defined in:
- lib/easemob.rb,
lib/easemob/token.rb,
lib/easemob/users.rb,
lib/easemob/groups.rb,
lib/easemob/chatlog.rb,
lib/easemob/version.rb,
lib/easemob/messages.rb,
lib/easemob/chatrooms.rb,
lib/easemob/fileoperation.rb,
lib/easemob/message/base_message.rb,
lib/easemob/message/chat_message.rb,
lib/easemob/message/user_message.rb,
lib/easemob/message/group_message.rb,
lib/easemob/message/fileoperation_message.rb
Defined Under Namespace
Modules: Chatlog, Chatrooms, Fileoperation, Groups, Messages, Token, Users Classes: BaseMessage, ChatMessage, FileoperationMessage, GroupMessage, QuotaLimitError, UserMessage, UserNameError
Constant Summary collapse
- VERSION =
'0.3.5'.freeze
Class Attribute Summary collapse
-
.app_name ⇒ Object
writeonly
同一“企业”下“APP”唯一标识,在环信开发者管理后台创建应用时填写的“应用名称”.
-
.base_url ⇒ Object
writeonly
Sets the attribute base_url.
-
.client_id ⇒ Object
使用 APP 的 client_id.
-
.client_secret ⇒ Object
和 client_secret 获取授权管理员 token.
-
.http_pool ⇒ Object
writeonly
Sets the attribute http_pool.
-
.http_timeout ⇒ Object
writeonly
Sets the attribute http_timeout.
-
.org_name ⇒ Object
writeonly
企业的唯一标识,开发者管理后台注册账号时填写的企业 ID.
- .token_file_path ⇒ Object
Class Method Summary collapse
- .chatfile_url(chatfile_uuid) ⇒ Object
- .do_request(verb, http, resource, options) ⇒ Object
- .head_url ⇒ Object
- .httprbs ⇒ Object
-
.request(verb, resource, options = {}) ⇒ HTTP::Response
Make an HTTP request with the given verb to easemob server.
-
.token ⇒ Object
Get admin access token from easemob server.
Methods included from Messages
audio_to, command_to, image_to, message_to, video_to
Methods included from Fileoperation
download_chatfile, upload_chatfile
Methods included from Chatlog
Methods included from Chatrooms
chatroom_add_users, chatroom_remove_users, create_chatroom, delete_chatroom, get_chatroom, modify_chatroom, query_chatrooms, user_join_chatroom, user_joined_chatrooms, user_leave_chatroom
Methods included from Groups
add_to_group_block, create_group, delete_group, get_groups, group_add_users, group_remove_users, group_set_owner, modify_group, query_group_blocks, query_group_users, query_groups, remove_from_group_block, user_join_group, user_joined_chatgroups, user_leave_group
Methods included from Users
activate_user, add_to_user_block, add_user_friend, create_user, create_users, deactivate_user, delete_user, delete_users!, disconnect_user, get_user, get_user_offline_msg_count, get_user_offline_msg_status, get_user_status, query_user_blocks, query_user_friends, query_users, remove_from_user_block, remove_user_friend, reset_user_password, set_user_nickname
Class Attribute Details
.app_name=(value) ⇒ Object (writeonly)
同一“企业”下“APP”唯一标识,在环信开发者管理后台创建应用时填写的“应用名称”
23 24 25 |
# File 'lib/easemob.rb', line 23 def app_name=(value) @app_name = value end |
.base_url=(value) ⇒ Object (writeonly)
Sets the attribute base_url
21 22 23 |
# File 'lib/easemob.rb', line 21 def base_url=(value) @base_url = value end |
.client_id ⇒ Object
使用 APP 的 client_id
18 19 20 |
# File 'lib/easemob.rb', line 18 def client_id @client_id end |
.client_secret ⇒ Object
和 client_secret 获取授权管理员 token
19 20 21 |
# File 'lib/easemob.rb', line 19 def client_secret @client_secret end |
.http_pool=(value) ⇒ Object (writeonly)
Sets the attribute http_pool
26 27 28 |
# File 'lib/easemob.rb', line 26 def http_pool=(value) @http_pool = value end |
.http_timeout=(value) ⇒ Object (writeonly)
Sets the attribute http_timeout
27 28 29 |
# File 'lib/easemob.rb', line 27 def http_timeout=(value) @http_timeout = value end |
.org_name=(value) ⇒ Object (writeonly)
企业的唯一标识,开发者管理后台注册账号时填写的企业 ID
22 23 24 |
# File 'lib/easemob.rb', line 22 def org_name=(value) @org_name = value end |
.token_file_path ⇒ Object
113 114 115 |
# File 'lib/easemob.rb', line 113 def self.token_file_path @token_file_path || "/tmp/easemob_#{@org_name}_#{@app_name}_token" end |
Class Method Details
.chatfile_url(chatfile_uuid) ⇒ Object
70 71 72 |
# File 'lib/easemob.rb', line 70 def self.chatfile_url(chatfile_uuid) "#{@base_url}/#{@org_name}/#{@app_name}/chatfiles/#{chatfile_uuid}" end |
.do_request(verb, http, resource, options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/easemob.rb', line 85 def self.do_request(verb, http, resource, ) http = http.headers('Authorization' => "Bearer #{token}") case verb when :upload restrict_access = .delete(:restrict_access) || true http.headers('restrict-access' => restrict_access) .request(:post, "#{head_url}/#{resource}", ) when :download header = { 'Accept' => 'application/octet-stream' } share_secret = .delete(:share_secret) header['share-secret'] = share_secret unless share_secret.nil? thumbnail = .delete(:thumbnail) header['thumbnail'] = thumbnail if thumbnail http.headers(header) .request(:get, "#{head_url}/#{resource}") else http.request(verb, "#{head_url}/#{resource}", ) end end |
.head_url ⇒ Object
109 110 111 |
# File 'lib/easemob.rb', line 109 def self.head_url "#{@base_url}/#{@org_name}/#{@app_name}" end |
.httprbs ⇒ Object
105 106 107 |
# File 'lib/easemob.rb', line 105 def self.httprbs @httprbs ||= ConnectionPool.new(size: @http_pool, timeout: @http_timeout) { HTTP.persistent @base_url } end |
.request(verb, resource, options = {}) ⇒ HTTP::Response
Make an HTTP request with the given verb to easemob server
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/easemob.rb', line 40 def self.request(verb, resource, = {}) httprbs.with do |http| res = do_request(verb, http, resource, ) case res.code # 401:(未授权)请求要求身份验证。对于需要 token 的接口,服务器可能返回此响应。 when 401 Token.refresh res = do_request(verb, http, resource, ) # 408:(请求超时)服务器等候请求时发生超时。 when 408 res = do_request(verb, http, resource, ) # 503:(服务不可用)请求接口超过调用频率限制,即接口被限流。 when 429, 503 sleep 1 res = do_request(verb, http, resource, ) 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 ⇒ Object
Get admin access token from easemob server
63 64 65 66 67 68 |
# 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 |