Class: Qtc::Eds::Client
- Inherits:
-
Object
- Object
- Qtc::Eds::Client
- Defined in:
- lib/qtc/eds/client.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ api_url: 'https://api.engin.io/v1' }
Instance Method Summary collapse
-
#access_token=(access_token) ⇒ Object
Set access token.
-
#collection(name) ⇒ Qtc::Eds::Collection
Get collection.
-
#create_user_token(username, password) ⇒ Object
Create user access token.
- #current_user ⇒ Object
-
#http_client ⇒ Qtc::Client
Get Qtc::Client instance.
-
#initialize(backend_id, options = {}) ⇒ Client
constructor
Initialize.
-
#revoke_user_token(token) ⇒ Object
Revoke user access token.
-
#usergroups ⇒ Qtc::Eds::UsergroupCollection
Get usergroup collection.
-
#users ⇒ Qtc::Eds::UserCollection
Get user collection.
-
#with_access_token(access_token, &block) ⇒ Object
Call block with given access token.
Constructor Details
#initialize(backend_id, options = {}) ⇒ Client
Initialize
18 19 20 21 22 23 |
# File 'lib/qtc/eds/client.rb', line 18 def initialize(backend_id, = {}) @options = DEFAULT_OPTIONS.merge() @backend_id = backend_id headers = {'Enginio-Backend-Id' => @backend_id} @client = Qtc::Client.new(@options[:api_url], headers) end |
Instance Method Details
#access_token=(access_token) ⇒ Object
Set access token
68 69 70 71 72 73 74 |
# File 'lib/qtc/eds/client.rb', line 68 def access_token=(access_token) if !access_token.nil? @client.default_headers['Authorization'] = "Bearer #{access_token}" else @client.default_headers.delete('Authorization') end end |
#collection(name) ⇒ Qtc::Eds::Collection
Get collection
38 39 40 |
# File 'lib/qtc/eds/client.rb', line 38 def collection(name) Qtc::Eds::Collection.new(@client, "/objects/#{name}") end |
#create_user_token(username, password) ⇒ Object
Create user access token
96 97 98 99 100 101 102 103 |
# File 'lib/qtc/eds/client.rb', line 96 def create_user_token(username, password) body = { grant_type: 'password', username: username, password: password } @client.post('/auth/oauth2/token', body, {}, {'Content-Type' => 'application/x-www-form-urlencoded'}) end |
#current_user ⇒ Object
58 59 60 61 62 |
# File 'lib/qtc/eds/client.rb', line 58 def current_user if @client.default_headers['Authorization'] @client.get('/user') end end |
#http_client ⇒ Qtc::Client
Get Qtc::Client instance
29 30 31 |
# File 'lib/qtc/eds/client.rb', line 29 def http_client @client end |
#revoke_user_token(token) ⇒ Object
Revoke user access token
109 110 111 112 113 114 |
# File 'lib/qtc/eds/client.rb', line 109 def revoke_user_token(token) body = { token: token } @client.post('/auth/oauth2/revoke', body, {}, {'Content-Type' => 'application/x-www-form-urlencoded'}) end |
#usergroups ⇒ Qtc::Eds::UsergroupCollection
Get usergroup collection
54 55 56 |
# File 'lib/qtc/eds/client.rb', line 54 def usergroups Qtc::Eds::UsergroupCollection.new(@client) end |
#users ⇒ Qtc::Eds::UserCollection
Get user collection
46 47 48 |
# File 'lib/qtc/eds/client.rb', line 46 def users Qtc::Eds::UserCollection.new(@client) end |
#with_access_token(access_token, &block) ⇒ Object
Call block with given access token
81 82 83 84 85 86 87 88 89 |
# File 'lib/qtc/eds/client.rb', line 81 def with_access_token(access_token, &block) prev_auth = @client.default_headers['Authorization'].dup @client.default_headers['Authorization'] = "Bearer #{access_token}" result = call(&block) @client.default_headers['Authorization'] = prev_auth result ensure @client.default_headers['Authorization'] = prev_auth if prev_auth end |