Class: Qtc::Eds::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/qtc/eds/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    api_url: 'https://api.engin.io/v1'
}

Instance Method Summary collapse

Constructor Details

#initialize(backend_id, options = {}) ⇒ Client

Initialize

Parameters:

  • backend_id (String)
  • options (Hash) (defaults to: {})


18
19
20
21
22
23
# File 'lib/qtc/eds/client.rb', line 18

def initialize(backend_id, options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @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

Parameters:

  • access_token (String)


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

Parameters:

  • name (String)

Returns:



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

Parameters:

  • username (String)
  • password (String)


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_userObject



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_clientQtc::Client

Get Qtc::Client instance

Returns:



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

Parameters:

  • token (String)


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

#usergroupsQtc::Eds::UsergroupCollection

Get usergroup collection



54
55
56
# File 'lib/qtc/eds/client.rb', line 54

def usergroups
  Qtc::Eds::UsergroupCollection.new(@client)
end

#usersQtc::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

Parameters:

  • access_token (String)
  • []


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