easemob 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b342046e707d8e8ea41d2faa6f22dce83708306
4
- data.tar.gz: 58388e41f977c65347c0f81bac8afccf82e8333f
3
+ metadata.gz: 8468de1f0eb25f0cb4f5fd7182cd537f5a065f22
4
+ data.tar.gz: d9ded95865eb429592ac84d43e6bfdefa77e427f
5
5
  SHA512:
6
- metadata.gz: 08f38b249d8348590cc4fd5c8dfc491d1d6877d02f0143739e51fee90ac93d754b072f445eb14df6fde63393838e7af0724fbe95b6f9e031fe7491e77876eacb
7
- data.tar.gz: 1f9df96100bd62db5d9674388101792a80160b0a1fa60c6624a0851fcd2efe5d12857f438185abc86309db4b00c1ba5dd65607a04a812fb52d2476ad729cc1a8
6
+ metadata.gz: d2b122d07608493b2857b6baf14ae02260d00149542342954b19e5409684a178152635480d8c3eac9f9597451e85005508577d24b1df4db883d0fbf981734815
7
+ data.tar.gz: ad09e35e5b0f5e974e302e4b021d6406ec987c6f98d135a55de75a4eca00018a9085b276b459332a37d1b0837ee7075f40294d8fa61edf0e13c8195ca95330b5
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ /.idea
2
+ .DS_Store
1
3
  /.bundle/
2
4
  /.yardoc
3
5
  /Gemfile.lock
@@ -7,3 +9,6 @@
7
9
  /pkg/
8
10
  /spec/reports/
9
11
  /tmp/
12
+ *.sublime-workspace
13
+ spec/examples.txt
14
+ .byebug_history
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.1
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
8
+ Max: 300
9
+
10
+ Metrics/AbcSize:
11
+ Max: 57
12
+
13
+ Metrics/ClassLength:
14
+ Max: 150
15
+
16
+ Metrics/MethodLength:
17
+ Max: 20
18
+
19
+ Style/NumericLiterals:
20
+ MinDigits: 7
21
+
22
+ Style/AsciiComments:
23
+ Enabled: false
24
+
25
+ Style/ParameterLists:
26
+ Max: 15
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
+ sudo: false
1
2
  language: ruby
2
3
  rvm:
3
- - 2.2.0
4
- before_install: gem install bundler -v 1.10.5
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.3
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 Liu Lantao
3
+ Copyright (c) 2016 guochunzhong
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Easemob
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/easemob`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Easemob is an unofficial easemob IM instant message gem, inspired from [huanxin](https://github.com/RobotJiang/ruby-for-huanxin) a lot.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,17 +20,26 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Config
24
+
25
+ Create `config/initializers/easemob.rb` and put following configurations into it.
26
+
27
+ ```ruby
28
+ Easemob.client_id = 'app_client_id' # 使用 APP 的 client_id
29
+ Easemob.client_secret = 'app_client_secret'
30
+ Easemob.org_name = 'bayetech' # 企业的唯一标识,开发者管理后台注册账号时填写的企业 ID
31
+ Easemob.app_name = 'landlord' # 同一“企业”下“APP”唯一标识,创建应用时填写的“应用名称”
32
+ ```
26
33
 
27
34
  ## Development
28
35
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
37
 
31
38
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
39
 
33
40
  ## Contributing
34
41
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easemob. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bayetech/easemob. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
43
 
37
44
 
38
45
  ## License
data/lib/easemob.rb CHANGED
@@ -1,5 +1,69 @@
1
- require "easemob/version"
1
+ require 'http'
2
+ require 'json'
3
+ require 'connection_pool'
4
+ require 'easemob/version'
5
+ require 'easemob/token'
6
+ require 'easemob/users'
2
7
 
3
8
  module Easemob
4
- # Your code goes here...
9
+ class << self
10
+ attr_accessor :client_id # 使用 APP 的 client_id
11
+ attr_accessor :client_secret # 和 client_secret 获取授权管理员 token
12
+
13
+ attr_writer :base_url
14
+ attr_writer :org_name # 企业的唯一标识,开发者管理后台注册账号时填写的企业 ID
15
+ attr_writer :app_name # 同一“企业”下“APP”唯一标识,在环信开发者管理后台创建应用时填写的“应用名称”
16
+
17
+ attr_writer :token_file_path
18
+ attr_writer :http_pool
19
+ attr_writer :http_timeout
20
+ end
21
+ @base_url = 'https://a1.easemob.com'
22
+ @random_generator = Random.new
23
+ @http_pool = 5
24
+ @http_timeout = 5
25
+
26
+ def self.head_url
27
+ "#{@base_url}/#{@org_name}/#{@app_name}"
28
+ end
29
+
30
+ def self.token_file_path
31
+ @token_file_path || "/tmp/easemob_#{@org_name}_#{@app_name}_token"
32
+ end
33
+
34
+ def self.httprbs
35
+ @httprbs ||= ConnectionPool.new(size: @http_pool, timeout: @http_timeout) { HTTP.persistent @base_url }
36
+ end
37
+
38
+ def self.do_post(resource, body_hash)
39
+ httprbs.with do |http|
40
+ res = do_post_request(http, resource, body_hash)
41
+ case res.code
42
+ # 401:(未授权)请求要求身份验证。对于需要 token 的接口,服务器可能返回此响应。
43
+ when 401, 408
44
+ Token.refresh
45
+ res = do_post_request(http, resource, body_hash)
46
+ # 408:(请求超时)服务器等候请求时发生超时。
47
+ when 408
48
+ res = do_post_request(http, resource, body_hash)
49
+ end
50
+ res
51
+ end
52
+ end
53
+
54
+ def self.do_post_request(http, resource, body_hash)
55
+ http.headers('Authorization' => "Bearer #{token}")
56
+ .post "#{head_url}/#{resource}", json: body_hash
57
+ end
58
+
59
+ def self.token
60
+ # Possible two worker running, one worker refresh token, other unaware, so must read every time
61
+ access_token, remain_life_seconds = Token.read_from_store
62
+ Token.refresh if remain_life_seconds < @random_generator.rand(30..3 * 60)
63
+ access_token
64
+ end
65
+
66
+ class << self
67
+ include Users
68
+ end
5
69
  end
@@ -0,0 +1,37 @@
1
+ module Easemob
2
+ module Token
3
+ def self.refresh
4
+ res = Easemob.httprbs.with do |http|
5
+ http.post "#{Easemob.head_url}/token", json: { grant_type: 'client_credentials', client_id: Easemob.client_id, client_secret: Easemob.client_secret }
6
+ end
7
+ raise "Failed to refresh easemob token: #{res}" unless res.code == 200
8
+ write_to_store(JSON.parse(res.to_s))
9
+ read_from_store
10
+ end
11
+
12
+ def self.read_from_store
13
+ td = read_token
14
+ token_life_in_seconds = td.fetch('token_expires_in').to_i
15
+ got_token_at = td.fetch('got_token_at').to_i
16
+ access_token = td.fetch('access_token')
17
+ remain_life_seconds = token_life_in_seconds - (Time.now.to_i - got_token_at)
18
+ return [access_token, remain_life_seconds]
19
+ rescue JSON::ParserError, Errno::ENOENT, KeyError, TypeError
20
+ refresh
21
+ end
22
+
23
+ def self.write_to_store(token_hash)
24
+ token_hash['got_token_at'.freeze] = Time.now.to_i
25
+ token_hash['token_expires_in'.freeze] = token_hash.delete('expires_in')
26
+ write_token(token_hash)
27
+ end
28
+
29
+ def self.read_token
30
+ JSON.parse(File.read(Easemob.token_file_path))
31
+ end
32
+
33
+ def self.write_token(token_hash)
34
+ File.write(Easemob.token_file_path, token_hash.to_json)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ module Easemob
2
+ module Users
3
+ def create_user(username, password, nickname = nil)
4
+ do_post('users', username: username, password: password, nickname: nickname)
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Easemob
2
- VERSION = "0.0.0"
2
+ VERSION = '0.0.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,78 +1,115 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easemob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
- - Liu Lantao
7
+ - Eric Guo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-18 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.3
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.3
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: connection_pool
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '3'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '2.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '3'
13
53
  - !ruby/object:Gem::Dependency
14
54
  name: bundler
15
55
  requirement: !ruby/object:Gem::Requirement
16
56
  requirements:
17
57
  - - "~>"
18
58
  - !ruby/object:Gem::Version
19
- version: '1.10'
59
+ version: '1.13'
20
60
  type: :development
21
61
  prerelease: false
22
62
  version_requirements: !ruby/object:Gem::Requirement
23
63
  requirements:
24
64
  - - "~>"
25
65
  - !ruby/object:Gem::Version
26
- version: '1.10'
66
+ version: '1.13'
27
67
  - !ruby/object:Gem::Dependency
28
68
  name: rake
29
69
  requirement: !ruby/object:Gem::Requirement
30
70
  requirements:
31
71
  - - "~>"
32
72
  - !ruby/object:Gem::Version
33
- version: '10.0'
73
+ version: '11.3'
34
74
  type: :development
35
75
  prerelease: false
36
76
  version_requirements: !ruby/object:Gem::Requirement
37
77
  requirements:
38
78
  - - "~>"
39
79
  - !ruby/object:Gem::Version
40
- version: '10.0'
80
+ version: '11.3'
41
81
  - !ruby/object:Gem::Dependency
42
82
  name: rspec
43
83
  requirement: !ruby/object:Gem::Requirement
44
84
  requirements:
45
- - - ">="
85
+ - - "~>"
46
86
  - !ruby/object:Gem::Version
47
- version: '0'
87
+ version: '3.5'
48
88
  type: :development
49
89
  prerelease: false
50
90
  version_requirements: !ruby/object:Gem::Requirement
51
91
  requirements:
52
- - - ">="
92
+ - - "~>"
53
93
  - !ruby/object:Gem::Version
54
- version: '0'
55
- description:
94
+ version: '3.5'
95
+ description: Helping rubyist integration with easemob IM service (环信 - 即时通讯云) easier.
56
96
  email:
57
- - liulantao@gmail.com
97
+ - eric.guocz@gmail.com
58
98
  executables: []
59
99
  extensions: []
60
100
  extra_rdoc_files: []
61
101
  files:
62
102
  - ".gitignore"
63
103
  - ".rspec"
104
+ - ".rubocop.yml"
64
105
  - ".travis.yml"
65
- - CODE_OF_CONDUCT.md
66
- - Gemfile
67
106
  - LICENSE.txt
68
107
  - README.md
69
- - Rakefile
70
- - bin/console
71
- - bin/setup
72
- - easemob.gemspec
73
108
  - lib/easemob.rb
109
+ - lib/easemob/token.rb
110
+ - lib/easemob/users.rb
74
111
  - lib/easemob/version.rb
75
- homepage: https://github.com/easemob/ruby-client
112
+ homepage: https://github.com/bayetech/easemob
76
113
  licenses:
77
114
  - MIT
78
115
  metadata: {}
@@ -82,9 +119,9 @@ require_paths:
82
119
  - lib
83
120
  required_ruby_version: !ruby/object:Gem::Requirement
84
121
  requirements:
85
- - - ">="
122
+ - - "~>"
86
123
  - !ruby/object:Gem::Version
87
- version: '0'
124
+ version: '2.2'
88
125
  required_rubygems_version: !ruby/object:Gem::Requirement
89
126
  requirements:
90
127
  - - ">="
@@ -92,8 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
129
  version: '0'
93
130
  requirements: []
94
131
  rubyforge_project:
95
- rubygems_version: 2.4.5
132
+ rubygems_version: 2.6.7
96
133
  signing_key:
97
134
  specification_version: 4
98
- summary: easemob api client
135
+ summary: An unofficial easemob IM instant message gem.
99
136
  test_files: []
data/CODE_OF_CONDUCT.md DELETED
@@ -1,13 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
-
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
-
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
-
9
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
-
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
-
13
- This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in easemob.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "easemob"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here
data/easemob.gemspec DELETED
@@ -1,24 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'easemob/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "easemob"
8
- spec.version = Easemob::VERSION
9
- spec.authors = ["Liu Lantao"]
10
- spec.email = ["liulantao@gmail.com"]
11
-
12
- spec.summary = %q{easemob api client}
13
- spec.homepage = "https://github.com/easemob/ruby-client"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
- spec.bindir = "exe"
18
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.10"
22
- spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "rspec"
24
- end