huanxin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6830d7a4854d536f51f7688f1fbfb97f2a18211d
4
+ data.tar.gz: 0e05fc2065187813660ea05a40c309f80c7c5591
5
+ SHA512:
6
+ metadata.gz: 0dcae61318d872ccd0ddbb9ddde8834a26490ced86520f137e66e7f99463290fb4e01fee5f203e41697125d8ea4d65af674ca918d779dad21d010ed6f6651fc0
7
+ data.tar.gz: 32b6ce92226c53815e5635e6bd12c44278467d13afe575845a0935d7d66c987cde1e225c19d4cf5cf4b4943703d43a9599f8051acdcebb83d3a60cb4265b7af2
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
@@ -0,0 +1,13 @@
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, 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 ADDED
@@ -0,0 +1,5 @@
1
+ #source 'https://rubygems.org'
2
+ source 'http://ruby.taobao.org'
3
+
4
+ # Specify your gem's dependencies in huanxin.gemspec
5
+ gemspec
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ huanxin (0.1.0)
5
+
6
+ GEM
7
+ remote: http://ruby.taobao.org/
8
+ specs:
9
+ activesupport (4.2.3)
10
+ i18n (~> 0.7)
11
+ json (~> 1.7, >= 1.7.7)
12
+ minitest (~> 5.1)
13
+ thread_safe (~> 0.3, >= 0.3.4)
14
+ tzinfo (~> 1.1)
15
+ diff-lcs (1.2.5)
16
+ httparty (0.13.5)
17
+ json (~> 1.8)
18
+ multi_xml (>= 0.5.2)
19
+ i18n (0.7.0)
20
+ json (1.8.3)
21
+ minitest (5.7.0)
22
+ multi_xml (0.5.5)
23
+ rake (10.4.2)
24
+ rspec (2.99.0)
25
+ rspec-core (~> 2.99.0)
26
+ rspec-expectations (~> 2.99.0)
27
+ rspec-mocks (~> 2.99.0)
28
+ rspec-core (2.99.2)
29
+ rspec-expectations (2.99.2)
30
+ diff-lcs (>= 1.1.3, < 2.0)
31
+ rspec-mocks (2.99.4)
32
+ thread_safe (0.3.5)
33
+ tzinfo (1.2.2)
34
+ thread_safe (~> 0.1)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ activesupport
41
+ bundler (~> 1.8)
42
+ httparty (~> 0.13.3)
43
+ huanxin!
44
+ rake (~> 10.0)
45
+ rspec (~> 2.6)
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Robot Jiang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Huanxin
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/huanxin`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'huanxin'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install huanxin
22
+
23
+ ## Usage
24
+
25
+ Huanxin.configure do |config|
26
+ config[:client_id] = Your client id
27
+ config[:client_secret] = Your client secret
28
+ #config[:host] = 'https://a1.easemob.com' 可选配置,默认是 https://a1.easemob.com
29
+ end
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/[my-github-username]/huanxin/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "huanxin"
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
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'huanxin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "huanxin"
8
+ spec.version = Huanxin::VERSION
9
+ spec.authors = ["Robot Jiang"]
10
+ spec.email = ["robot.z.jiang@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
14
+ end
15
+
16
+ spec.summary = %q{Huanxin Rest API for Ruby Client}
17
+ spec.description = %q{环信 Rest API 的 Ruby Client }
18
+ spec.homepage = "https://github.com/RobotJiang/ruby-for-huanxin"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.8"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 2.6"
29
+ spec.add_development_dependency "httparty", "~> 0.13.3"
30
+ spec.add_development_dependency "activesupport"
31
+ end
@@ -0,0 +1,7 @@
1
+ require "huanxin/version"
2
+ require "huanxin/base"
3
+ require "huanxin/client"
4
+
5
+ module Huanxin
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,46 @@
1
+ module Huanxin
2
+ @huanxin_config = {}
3
+
4
+ class << self
5
+ def configure
6
+ yield @huanxin_config if block_given?
7
+ end
8
+ def config
9
+ @huanxin_config
10
+ end
11
+ end
12
+
13
+ class Base
14
+ RAND_WORDS = [('a'..'z'), ('A'..'Z'), (0..9), ['_']].map { |i| i.to_a }.flatten
15
+ #cache_client 所有实现了 ActiveSupport::Cache::Store 的对象实例
16
+ #如果是在rails里面使用,可以直接传递 Rails.cache
17
+ #
18
+ def initialize(cache_client, org_name, app_name)
19
+ raise 'invalid cache client' if cache_client.nil?
20
+
21
+ @client_id = Huanxin.config[:client_id]
22
+ @client_secret = Huanxin.config[:client_secret]
23
+ @base_url = Huanxin.config[:host] || 'https://a1.easemob.com'
24
+ @username_len = Huanxin.config[:username_len] || 8
25
+ @password_len = Huanxin.config[:password_len] || 10
26
+
27
+ @org_name = org_name
28
+ @app_name = app_name
29
+ @cache = cache_client
30
+
31
+ @head_url = "#{@base_url}/#{org_name}/#{app_name}"
32
+ @token_cache_key = "_huanxin_auth_#{org_name}_#{app_name}_"
33
+ end
34
+
35
+ protected
36
+
37
+ def generate_username(user_id)
38
+ "#{(0...@username_len).map { RAND_WORDS[rand(RAND_WORDS.length)] }.join}n#{user_id}"
39
+ end
40
+ def generate_password
41
+ "#{(0...@password_len).map { RAND_WORDS[rand(RAND_WORDS.length)] }.join}"
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,110 @@
1
+ require 'httparty'
2
+
3
+ module Huanxin
4
+
5
+ class Client < Huanxin::Base
6
+
7
+ def initialize(cache_client, org_name, app_name)
8
+ super(cache_client, org_name, app_name)
9
+ end
10
+ #注册用户到环信
11
+ def register(user_id, nickname = nil)
12
+ self.register_by_name_pwd(self.generate_username(user_id), self.generate_password(), nickname)
13
+ end
14
+ #注册用户到环信,使用外部的用户名和密码
15
+ def register_by_name_pwd(username, password, nickname = nil)
16
+ token = self.auth_token()
17
+
18
+ result = HTTParty.post("#{@head_url}/users",
19
+ :body => { :username => username,
20
+ :password => password,
21
+ :nickname => nickname
22
+ }.to_json,
23
+ :headers => { 'Content-Type' => 'application/json', 'Authorization'=>"Bearer #{token}" } )
24
+ if result.response.code.to_i == 200
25
+ return [username, password]
26
+ else
27
+ nil
28
+ end
29
+ end
30
+ #获取环信上的用户
31
+ def get_user(username)
32
+ token = self.auth_token()
33
+ result = HTTParty.get("#{@head_url}/users/#{username}", :headers => { 'Content-Type' => 'application/json', 'Authorization'=>"Bearer #{token}" } )
34
+
35
+ if result.response.code.to_i == 200
36
+ return result["entities"][0]
37
+ else
38
+ nil
39
+ end
40
+
41
+ end
42
+ #重置用户密码
43
+ def reset_user_password(username, password = nil)
44
+ new_password = password || self.generate_password()
45
+ token = self.auth_token()
46
+ result = HTTParty.put("#{@head_url}/users/#{username}/password",
47
+ :body => {newpassword: new_password}.to_json ,
48
+ :headers => { 'Content-Type' => 'application/json', 'Authorization'=>"Bearer #{token}" } )
49
+
50
+ if result.response.code.to_i == 200
51
+ return new_password
52
+ else
53
+ nil
54
+ end
55
+
56
+ end
57
+ #修改用户昵称
58
+ def modify_user_nickname(username, nickname)
59
+ token = self.auth_token()
60
+ result = HTTParty.put("#{@head_url}/users/#{username}",
61
+ :body => {:nickname => nickname}.to_json ,
62
+ :headers => { 'Content-Type' => 'application/json', 'Authorization'=>"Bearer #{token}" } )
63
+
64
+ if result.response.code.to_i == 200
65
+ return result["entities"][0]
66
+ else
67
+ nil
68
+ end
69
+ end
70
+
71
+ #删除环信上的用户
72
+ def delete_user(username)
73
+ token = self.auth_token()
74
+ result = HTTParty.delete("#{@head_url}/users/#{username}", :headers => { 'Content-Type' => 'application/json', 'Authorization'=>"Bearer #{token}" } )
75
+ if result.response.code.to_i == 200
76
+ return result["entities"][0]
77
+ else
78
+ nil
79
+ end
80
+
81
+ end
82
+
83
+ protected
84
+
85
+ def auth_token
86
+ @cache.fetch( @token_cache_key, expires_in: 6.days ) do
87
+ cache_val = nil
88
+ begin
89
+ result = HTTParty.post("#{@head_url}/token",
90
+ :body => { :grant_type => 'client_credentials',
91
+ :client_id => @client_id,
92
+ :client_secret => @client_secret
93
+ }.to_json,
94
+ :headers => { 'Content-Type' => 'application/json' } )
95
+
96
+ if !result.nil? and !result["access_token"].blank?
97
+ cache_val = result["access_token"]
98
+ end
99
+ rescue => e
100
+ puts e
101
+ end
102
+
103
+ cache_val
104
+
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,3 @@
1
+ module Huanxin
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huanxin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robot Jiang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: "环信 Rest API 的 Ruby Client "
84
+ email:
85
+ - robot.z.jiang@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rspec"
91
+ - ".travis.yml"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - huanxin.gemspec
101
+ - lib/huanxin.rb
102
+ - lib/huanxin/base.rb
103
+ - lib/huanxin/client.rb
104
+ - lib/huanxin/version.rb
105
+ homepage: https://github.com/RobotJiang/ruby-for-huanxin
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Huanxin Rest API for Ruby Client
130
+ test_files: []