zzlink 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zzlink.rb +26 -0
- data/lib/zzlink/auth.rb +83 -0
- data/lib/zzlink/client.rb +118 -0
- data/lib/zzlink/env.rb +44 -0
- data/lib/zzlink/service.rb +8 -0
- data/lib/zzlink/session.rb +32 -0
- data/lib/zzlink/users.rb +114 -0
- data/lib/zzlink/version.rb +3 -0
- data/zzlink.gemspec +25 -0
- metadata +63 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f41c9374bca1241a2fa77652a9e16e368af538d0801d74b83d3e8babe1851309
|
|
4
|
+
data.tar.gz: '09ee73adeab6018d730a6744846f6a665cfa53e5a8d931114f7677ed8509fdf7'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '0591c8a12ca2f75e4901102465ecdcf34194a8636968c70326a13976d44fd803d22e45a47549fd7a60e5569f846dcfd368519508cd882cd34fdc5683155360a0'
|
|
7
|
+
data.tar.gz: e77933c97c4d21c85e7a59f174de1bf038cddf27dc1ae284da79f7eb19b8cd0756c5175d7a3bf5a1f19f2c0864726b96638176b2c8bebea1f16ececa1df9662d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
zzlink (0.0.2)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
minitest (5.14.0)
|
|
10
|
+
rake (13.0.1)
|
|
11
|
+
|
|
12
|
+
PLATFORMS
|
|
13
|
+
ruby
|
|
14
|
+
|
|
15
|
+
DEPENDENCIES
|
|
16
|
+
minitest (~> 5.0)
|
|
17
|
+
rake (~> 13.0)
|
|
18
|
+
zzlink!
|
|
19
|
+
|
|
20
|
+
BUNDLED WITH
|
|
21
|
+
2.1.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Wu Zixing
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Zzlink
|
|
2
|
+
|
|
3
|
+
The ruby client of zzLink API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'zzlink'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install zzlink
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
TODO: Write usage instructions here
|
|
24
|
+
|
|
25
|
+
## Development
|
|
26
|
+
|
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
28
|
+
|
|
29
|
+
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).
|
|
30
|
+
|
|
31
|
+
## Contributing
|
|
32
|
+
|
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/zzlink/zzlink-ruby.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "zzlink"
|
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/zzlink.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require_relative 'zzlink/version'
|
|
2
|
+
require_relative 'zzlink/users'
|
|
3
|
+
require_relative 'zzlink/env'
|
|
4
|
+
require_relative 'zzlink/auth'
|
|
5
|
+
|
|
6
|
+
module Zzlink
|
|
7
|
+
class << self
|
|
8
|
+
# Get client of user service.
|
|
9
|
+
# @return [Zzlink::Users] client of zzUsers service.
|
|
10
|
+
def users
|
|
11
|
+
Zzlink::Users.instance
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Get client of Env service.
|
|
15
|
+
# @return [Zzlink::Env] client of zzEnv service.
|
|
16
|
+
def env
|
|
17
|
+
Zzlink::Env.instance
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Get helper of SSO service.
|
|
21
|
+
# @return [Zzlink::Auth] helper of SSO service.
|
|
22
|
+
def auth
|
|
23
|
+
Zzlink::Auth.instance
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/lib/zzlink/auth.rb
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
require_relative 'service'
|
|
3
|
+
require_relative 'session'
|
|
4
|
+
|
|
5
|
+
module Zzlink
|
|
6
|
+
# Helpers of visiting SSO service.
|
|
7
|
+
class Auth < Service
|
|
8
|
+
include Singleton
|
|
9
|
+
|
|
10
|
+
# Get login URL for the local redirect URL.
|
|
11
|
+
# @param redirect_url [String]
|
|
12
|
+
# @param client_id [String] client identification. Default is a web APP.
|
|
13
|
+
# @param ttl [Integer] TTL (in seconds) of the login procedure.
|
|
14
|
+
# @return [String] login URL of SSO.
|
|
15
|
+
def url_of_login(redirect_url, client_id = 'web', ttl = 600)
|
|
16
|
+
query = URI.encode_www_form(redirect_url: redirect_url, zt: zzlink_ticket(ttl), zc: client_id)
|
|
17
|
+
sso_url + '/login?' + query
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Get login URL of Github for the local redirect URL.
|
|
21
|
+
# @param redirect_url [String]
|
|
22
|
+
# @param client_id [String] client identification. Default is a web APP.
|
|
23
|
+
# @param ttl [Integer] TTL (in seconds) of the login procedure.
|
|
24
|
+
# @return [String] login URL of SSO.
|
|
25
|
+
def url_of_login_as_github(redirect_url, client_id = 'web', ttl = 600)
|
|
26
|
+
query = URI.encode_www_form(redirect_url: redirect_url, zt: zzlink_ticket(ttl), zc: client_id)
|
|
27
|
+
sso_url + '/oauth/github?' + query
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Check in with a ticket.
|
|
31
|
+
# @param ticket [String] the SSO ticket.
|
|
32
|
+
# @return [Session] a session object if there is a valid session.
|
|
33
|
+
# @raise if check-in failed.
|
|
34
|
+
def check_in_with_ticket(ticket)
|
|
35
|
+
check_in(ticket: ticket)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Check in with a token.
|
|
39
|
+
# @param token [String] the user token.
|
|
40
|
+
# @return [Session] a session object if there is a valid session.
|
|
41
|
+
# @raise if check-in failed.
|
|
42
|
+
def check_in_with_user_token(token)
|
|
43
|
+
check_in(user_token: token)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Get logout URL for the local redirect URL.
|
|
47
|
+
# @param redirect_url [String]
|
|
48
|
+
# @param ttl [Integer] TTL (in seconds) of the login procedure.
|
|
49
|
+
# @return [String] logout URL of SSO.
|
|
50
|
+
def url_of_logout(redirect_url, ttl = 60)
|
|
51
|
+
query = URI.encode_www_form(redirect_url: redirect_url, zt: zzlink_ticket(ttl))
|
|
52
|
+
sso_url + '/logout?' + query
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def sso_url
|
|
58
|
+
Zzlink.env.get_env('sso_url')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def zzlink_ticket(ttl)
|
|
62
|
+
Zzlink.env.new_ticket(ttl)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Check session status with ticket or user token.
|
|
66
|
+
# @param param [Hash] {ticket: 'xxxx'} or {user_token: 'xxxx'}.
|
|
67
|
+
# @return [Session] a session object in case of that a session is alive.
|
|
68
|
+
# @raise if check-in failed.
|
|
69
|
+
def check_in(param)
|
|
70
|
+
query = URI.encode_www_form(param)
|
|
71
|
+
uri = URI(sso_url + '/checkin?' + query)
|
|
72
|
+
resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, uss_ssl: uri.scheme == 'https') do |http|
|
|
73
|
+
req = Net::HTTP::Put.new(uri.request_uri)
|
|
74
|
+
set_request_headers(req)
|
|
75
|
+
req['Content-Type'] = 'application/json'
|
|
76
|
+
req.body = param.to_json
|
|
77
|
+
http.request(req)
|
|
78
|
+
end
|
|
79
|
+
value = check_and_return(resp)
|
|
80
|
+
Session.new(value)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
require 'base64'
|
|
2
|
+
require 'openssl'
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'json'
|
|
5
|
+
|
|
6
|
+
module Zzlink
|
|
7
|
+
# Error.
|
|
8
|
+
class Error < StandardError
|
|
9
|
+
def initialize(code, error)
|
|
10
|
+
super("#{code}: #{error}")
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# The HTTP client.
|
|
15
|
+
module Client
|
|
16
|
+
class << self
|
|
17
|
+
attr_accessor :access_key, :secret_key, :api_url
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Do a GET request.
|
|
21
|
+
def get(path, query_params = {})
|
|
22
|
+
uri = uri_of(path, query_params)
|
|
23
|
+
resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
|
|
24
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
|
25
|
+
set_request_headers(req)
|
|
26
|
+
http.request(req)
|
|
27
|
+
end
|
|
28
|
+
check_and_return(resp)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def post(path, data = {})
|
|
32
|
+
uri = uri_of(path)
|
|
33
|
+
resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
|
|
34
|
+
req = Net::HTTP::Post.new(uri.request_uri)
|
|
35
|
+
set_request_headers(req)
|
|
36
|
+
unless data.empty?
|
|
37
|
+
req['Content-Type'] = 'application/json'
|
|
38
|
+
req.body = data.to_json
|
|
39
|
+
end
|
|
40
|
+
http.request(req)
|
|
41
|
+
end
|
|
42
|
+
check_and_return(resp)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def put(path, data = {})
|
|
46
|
+
uri = uri_of(path)
|
|
47
|
+
resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
|
|
48
|
+
req = Net::HTTP::Put.new(uri.request_uri)
|
|
49
|
+
set_request_headers(req)
|
|
50
|
+
unless data.empty?
|
|
51
|
+
req['Content-Type'] = 'application/json'
|
|
52
|
+
req.body = data.to_json
|
|
53
|
+
end
|
|
54
|
+
http.request(req)
|
|
55
|
+
end
|
|
56
|
+
check_and_return(resp)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def delete(path, query_params = {})
|
|
60
|
+
uri = uri_of(path, query_params)
|
|
61
|
+
resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
|
|
62
|
+
req = Net::HTTP::Delete.new(uri.request_uri)
|
|
63
|
+
set_request_headers(req)
|
|
64
|
+
http.request(req)
|
|
65
|
+
end
|
|
66
|
+
check_and_return(resp)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
# Make URI object from request path and query parameters.
|
|
72
|
+
# @param path [String] request path (without scheme, host and port).
|
|
73
|
+
# @param query_params [Hash] optional query parameters.
|
|
74
|
+
# @return the URI object.
|
|
75
|
+
def uri_of(path, query_params = {})
|
|
76
|
+
url = Client.api_url + path
|
|
77
|
+
if query_params.empty?
|
|
78
|
+
URI(url)
|
|
79
|
+
else
|
|
80
|
+
queries = URI.encode_www_form(query_params)
|
|
81
|
+
url.include?('?') ? URI(url + '&' + queries) : URI(url + '?' + queries)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Set headers for the HTTP request.
|
|
86
|
+
# @param request [HttpRequest] the request object.
|
|
87
|
+
def set_request_headers(request)
|
|
88
|
+
host = self.name + '.' + self.version
|
|
89
|
+
date = Time.now().gmtime.strftime('%a, %d %b %Y %H:%M:%S %Z')
|
|
90
|
+
signing_string = "host: #{host}\n#{request.method} #{request.path} HTTP/1.1\ndate: #{date}"
|
|
91
|
+
signature = Base64.strict_encode64(OpenSSL::HMAC.digest("SHA256", Client.secret_key, signing_string))
|
|
92
|
+
request['Accept'] = 'application/json'
|
|
93
|
+
request['Host'] = host
|
|
94
|
+
request['Date'] = date
|
|
95
|
+
request['Authorization'] = "hmac username=\"#{Client.access_key}\", algorithm=\"hmac-sha256\", headers=\"host request-line date\", signature=\"#{signature}\""
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def check_and_return(response)
|
|
99
|
+
if response.is_a?(Net::HTTPSuccess)
|
|
100
|
+
response.body
|
|
101
|
+
else
|
|
102
|
+
raise Error.new(response.code, response.body)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
class << self
|
|
108
|
+
# Initialize the ZzLink client.
|
|
109
|
+
# @param access_key [String] developer's access key.
|
|
110
|
+
# @param secret_key [String] developer's secret key.
|
|
111
|
+
# @param api_url [String] URL of zzLink API service. Default to 'https://api.zhizhi.link'.
|
|
112
|
+
def init(access_key, secret_key, api_url = nil)
|
|
113
|
+
Client.access_key = access_key
|
|
114
|
+
Client.secret_key = secret_key
|
|
115
|
+
Client.api_url = (api_url.nil? || api_url.empty?) ? 'https://api.zhizhi.link' : api_url.delete_suffix('/')
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
data/lib/zzlink/env.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require_relative 'service'
|
|
2
|
+
require 'singleton'
|
|
3
|
+
|
|
4
|
+
module Zzlink
|
|
5
|
+
# Client of zzEnv service.
|
|
6
|
+
class Env < Service
|
|
7
|
+
include Singleton
|
|
8
|
+
|
|
9
|
+
def name
|
|
10
|
+
'zzEnv'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def version
|
|
14
|
+
'v1.0'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Get a environment variable's value.
|
|
18
|
+
# @param name [String] name of the environment variable.
|
|
19
|
+
# @return [String] value of the variable.
|
|
20
|
+
def get_env(name)
|
|
21
|
+
resp = get("/env/#{name}")
|
|
22
|
+
resp = JSON.parse(resp)
|
|
23
|
+
resp[name]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Get a zzLink ticket.
|
|
27
|
+
# @param ttl_in_sec [Integer] TTL (in seconds) of the new ticket.
|
|
28
|
+
# @return [String] a ticket which is URL-safe encoded.
|
|
29
|
+
def new_ticket(ttl_in_sec = 60)
|
|
30
|
+
resp = get('/ticket', ttl: ttl_in_sec)
|
|
31
|
+
resp = JSON.parse(resp)
|
|
32
|
+
resp['ticket']
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Check a zzLink ticket.
|
|
36
|
+
# @param ticket [String] the string representation of the ticket.
|
|
37
|
+
# @return [Boolean] whether the ticket is valid.
|
|
38
|
+
def check_ticket(ticket)
|
|
39
|
+
resp = put('/ticket', ticket: ticket);
|
|
40
|
+
resp = JSON.parse(resp)
|
|
41
|
+
resp['status']
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module Zzlink
|
|
2
|
+
# User session.
|
|
3
|
+
class Session
|
|
4
|
+
attr_accessor :token, :user_id, :client_id, :created_at, :updated_at
|
|
5
|
+
def initialize(attributes = {})
|
|
6
|
+
from_hash(attributes)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def from_hash(attributes)
|
|
10
|
+
attributes.each_key do |k|
|
|
11
|
+
setter = case k.to_sym
|
|
12
|
+
when :userId then :user_id=
|
|
13
|
+
when :clientId then :client_id=
|
|
14
|
+
when :createdAt then :created_at=
|
|
15
|
+
when :updatedAt then :updated_at=
|
|
16
|
+
else "#{k}=".to_sym
|
|
17
|
+
end
|
|
18
|
+
send(setter, attributes[k]) if respond_to?(setter)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_hash
|
|
23
|
+
hash = {}
|
|
24
|
+
hash[:userId] = user_id unless user_id.nil?
|
|
25
|
+
hash[:clientId] = client_id unless client_id.nil?
|
|
26
|
+
hash[:token] = token unless token.nil?
|
|
27
|
+
hash[:createdAt] = created_at unless created_at.nil?
|
|
28
|
+
hash[:updatedAt] = updated_at unless updated_at.nil?
|
|
29
|
+
hash
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/zzlink/users.rb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
require_relative 'service'
|
|
3
|
+
require_relative 'session'
|
|
4
|
+
|
|
5
|
+
module Zzlink
|
|
6
|
+
# A zzLink user.
|
|
7
|
+
class User
|
|
8
|
+
attr_accessor :id, :name, :avatar_url, :home_url, :email
|
|
9
|
+
attr_accessor :access_token, :token_type, :oauth_scope, :oauth_id, :oauth_provider
|
|
10
|
+
attr_accessor :created_at, :updated_at
|
|
11
|
+
|
|
12
|
+
# New a User object.
|
|
13
|
+
# @param attributes [Hash] attributes of the new user.
|
|
14
|
+
def initialize(attributes = {})
|
|
15
|
+
from_hash(attributes)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Assign attributes from a Hash.
|
|
19
|
+
# @param attributes [Hash] attributes.
|
|
20
|
+
def from_hash(attributes)
|
|
21
|
+
attributes.each_pair do |k, v|
|
|
22
|
+
setter = case k.to_sym
|
|
23
|
+
when :avatarURL then :avatar_url=
|
|
24
|
+
when :homeURL then :home_url=
|
|
25
|
+
when :accessToken then :access_token=
|
|
26
|
+
when :tokenType then :token_type=
|
|
27
|
+
when :oauthScope then :oauth_scope=
|
|
28
|
+
when :oauthId then :oauth_id=
|
|
29
|
+
when :oauthProvider then :oauth_provider=
|
|
30
|
+
when :createdAt then :created_at=
|
|
31
|
+
when :updatedAt then :created_at=
|
|
32
|
+
else "#{k}=".to_sym
|
|
33
|
+
end
|
|
34
|
+
send(setter, v) if respond_to?(setter)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def to_hash
|
|
39
|
+
hash = {}
|
|
40
|
+
hash['id'] = id unless id.nil?
|
|
41
|
+
hash['name'] = name unless name.nil?
|
|
42
|
+
hash['avatarURL'] = avatar_url unless avatar_url.nil?
|
|
43
|
+
hash['homeURL'] = home_url unless home_url.nil?
|
|
44
|
+
hash['email'] = email unless email.nil?
|
|
45
|
+
hash['accessToken'] = access_token unless access_token.nil?
|
|
46
|
+
hash['tokenType'] = token_type unless token_type.nil?
|
|
47
|
+
hash['oauthScope'] = oauth_scope unless oauth_scope.nil?
|
|
48
|
+
hash['oauthId'] = oauth_id unless oauth_id.nil?
|
|
49
|
+
hash['oauthProvider'] = oauth_provider unless oauth_provider.nil?
|
|
50
|
+
hash['createdAt'] = created_at unless created_at.nil?
|
|
51
|
+
hash['updatedAt'] = updated_at unless updated_at.nil?
|
|
52
|
+
hash
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The zzUser service.
|
|
57
|
+
class Users < Service
|
|
58
|
+
include Singleton
|
|
59
|
+
|
|
60
|
+
def name
|
|
61
|
+
'zzUsers'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def version
|
|
65
|
+
'v1.0'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Create a user. If :oauth_provider and :oauth_id already exist, the existing user will be updated.
|
|
69
|
+
# @param attributes [Hash] attributes of the new user.
|
|
70
|
+
# @return [User] the created user.
|
|
71
|
+
# @raise [Error] in case of failure.
|
|
72
|
+
def create_user(attributes)
|
|
73
|
+
user = User.new(attributes)
|
|
74
|
+
resp = post('/users', user.to_hash)
|
|
75
|
+
user.from_hash(JSON.parse(resp))
|
|
76
|
+
user
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Create or reset a session.
|
|
80
|
+
# @param attributes [Hash] attributes of the new session. If the combination of user_id and client_id confilicts, the existing session will be reset.
|
|
81
|
+
# @return [Session] the created or updated session.
|
|
82
|
+
# @raise [Error] in case of failure.
|
|
83
|
+
def create_session(attributes)
|
|
84
|
+
session = Session.new(attributes)
|
|
85
|
+
resp = post('/sessions', session.to_hash)
|
|
86
|
+
session.from_hash(JSON.parse(resp))
|
|
87
|
+
session
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Find a session by its token.
|
|
91
|
+
# @param token [String] the token.
|
|
92
|
+
# @return [Session, nil] the found session or nil.
|
|
93
|
+
def find_session_by_token(token)
|
|
94
|
+
begin
|
|
95
|
+
resp = get("/sessions/#{token}")
|
|
96
|
+
Session.new(JSON.parse(resp))
|
|
97
|
+
rescue
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Delete a session by its token.
|
|
103
|
+
# @param token [String] the token.
|
|
104
|
+
# @return [Boolean] the found session or nil.
|
|
105
|
+
def delete_session_by_token(token)
|
|
106
|
+
begin
|
|
107
|
+
delete("/sessions/#{token}")
|
|
108
|
+
true
|
|
109
|
+
rescue
|
|
110
|
+
false
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
data/zzlink.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require_relative 'lib/zzlink/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "zzlink"
|
|
5
|
+
spec.version = Zzlink::VERSION
|
|
6
|
+
spec.authors = ["Wu Zixing"]
|
|
7
|
+
spec.email = ["wzixing@gmail.com"]
|
|
8
|
+
|
|
9
|
+
spec.summary = %q{Ruby client of zzLink API.}
|
|
10
|
+
spec.homepage = "https://gitlab.com/zzlink/zzlink-ruby"
|
|
11
|
+
spec.license = "MIT"
|
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
13
|
+
|
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
15
|
+
spec.metadata["source_code_uri"] = "https://gitlab.com/zzlink/zzlink-ruby.git"
|
|
16
|
+
|
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
21
|
+
end
|
|
22
|
+
spec.bindir = "exe"
|
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
24
|
+
spec.require_paths = ["lib"]
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: zzlink
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wu Zixing
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-03-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- wzixing@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".gitignore"
|
|
21
|
+
- ".travis.yml"
|
|
22
|
+
- Gemfile
|
|
23
|
+
- Gemfile.lock
|
|
24
|
+
- LICENSE.txt
|
|
25
|
+
- README.md
|
|
26
|
+
- Rakefile
|
|
27
|
+
- bin/console
|
|
28
|
+
- bin/setup
|
|
29
|
+
- lib/zzlink.rb
|
|
30
|
+
- lib/zzlink/auth.rb
|
|
31
|
+
- lib/zzlink/client.rb
|
|
32
|
+
- lib/zzlink/env.rb
|
|
33
|
+
- lib/zzlink/service.rb
|
|
34
|
+
- lib/zzlink/session.rb
|
|
35
|
+
- lib/zzlink/users.rb
|
|
36
|
+
- lib/zzlink/version.rb
|
|
37
|
+
- zzlink.gemspec
|
|
38
|
+
homepage: https://gitlab.com/zzlink/zzlink-ruby
|
|
39
|
+
licenses:
|
|
40
|
+
- MIT
|
|
41
|
+
metadata:
|
|
42
|
+
homepage_uri: https://gitlab.com/zzlink/zzlink-ruby
|
|
43
|
+
source_code_uri: https://gitlab.com/zzlink/zzlink-ruby.git
|
|
44
|
+
post_install_message:
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: 2.3.0
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubygems_version: 3.1.2
|
|
60
|
+
signing_key:
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: Ruby client of zzLink API.
|
|
63
|
+
test_files: []
|