Class: SisRuby::Client
- Inherits:
-
Object
- Object
- SisRuby::Client
- Defined in:
- lib/sis_ruby/client.rb
Defined Under Namespace
Classes: AuthenticationError
Constant Summary collapse
- DEFAULT_API_VERSION =
'1.1'
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#entity_endpoints ⇒ Object
readonly
Returns the value of attribute entity_endpoints.
-
#hiera ⇒ Object
readonly
Returns the value of attribute hiera.
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
Instance Method Summary collapse
-
#authenticate(username, password) ⇒ Object
Authenticates the username and password.
- #create_endpoint(endpoint_suffix, id_fieldname = :default) ⇒ Object
- #entities(name, id_fieldname = DEFAULT_ID_FIELDNAME) ⇒ Object
-
#initialize(url, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#schema_for(collection_name) ⇒ Object
Returns the schema for the specified collection_name, or nil if it’s not found.
- #to_s ⇒ Object
- #tokens(username) ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sis_ruby/client.rb', line 15 def initialize(url, = {}) @base_url = url @api_version = [:api_version] || DEFAULT_API_VERSION @auth_token = [:auth_token] @entity_endpoints = {} @hooks = create_endpoint('hooks', 'name') @schemas = create_endpoint('schemas', 'name') @hiera = create_endpoint('hiera', 'name') end |
Instance Attribute Details
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def api_version @api_version end |
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def auth_token @auth_token end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def base_url @base_url end |
#entity_endpoints ⇒ Object (readonly)
Returns the value of attribute entity_endpoints.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def entity_endpoints @entity_endpoints end |
#hiera ⇒ Object (readonly)
Returns the value of attribute hiera.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def hiera @hiera end |
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def hooks @hooks end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def hosts @hosts end |
#schemas ⇒ Object (readonly)
Returns the value of attribute schemas.
9 10 11 |
# File 'lib/sis_ruby/client.rb', line 9 def schemas @schemas end |
Instance Method Details
#authenticate(username, password) ⇒ Object
Authenticates the username and password. Get the token by calling client.auth_token.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sis_ruby/client.rb', line 51 def authenticate(username, password) dest = "#{base_url}/api/v#{api_version}/users/auth_token" = { userpwd: username + ':' + password, headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } } response = Typhoeus.post(dest, ) unless response.[:response_code] == 201 raise AuthenticationError.new(response) end @auth_token = JSON.parse(response.response_body)['name'] self end |
#create_endpoint(endpoint_suffix, id_fieldname = :default) ⇒ Object
27 28 29 |
# File 'lib/sis_ruby/client.rb', line 27 def create_endpoint(endpoint_suffix, id_fieldname = :default) Endpoint.new(self, endpoint_suffix, id_fieldname) end |
#entities(name, id_fieldname = DEFAULT_ID_FIELDNAME) ⇒ Object
32 33 34 |
# File 'lib/sis_ruby/client.rb', line 32 def entities(name, id_fieldname = DEFAULT_ID_FIELDNAME) @entity_endpoints[name] ||= create_endpoint("entities/#{name}", id_fieldname) end |
#schema_for(collection_name) ⇒ Object
Returns the schema for the specified collection_name, or nil if it’s not found.
43 44 45 46 |
# File 'lib/sis_ruby/client.rb', line 43 def schema_for(collection_name) params = Params.new.limit(1).filter('name' => collection_name) schemas.list(params).first end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/sis_ruby/client.rb', line 67 def to_s self.class.name + ": base_url = #{@base_url}" end |
#tokens(username) ⇒ Object
37 38 39 |
# File 'lib/sis_ruby/client.rb', line 37 def tokens(username) create_endpoint("users/#{username}/tokens", 'name') end |