Class: SisRuby::Client

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

Defined Under Namespace

Classes: AuthenticationError

Constant Summary collapse

DEFAULT_API_VERSION =
'1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.

Parameters:

  • url

    the base URL of the service (excluding ”/api/v…”)

  • options (can include :version, :auth_token) (defaults to: {})


15
16
17
18
19
20
21
22
23
24
# File 'lib/sis_ruby/client.rb', line 15

def initialize(url, options = {})
  @base_url = url
  @api_version = options[:api_version] || DEFAULT_API_VERSION
  @auth_token = options[:auth_token]

  @entity_endpoints = {}
  @hooks   = create_endpoint('hooks',   'name')
  @schemas = create_endpoint('schemas', 'name')
  @hiera   = create_endpoint('hiera',   'name')
end

Instance Attribute Details

#api_versionObject (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_tokenObject (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_urlObject (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_endpointsObject (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

#hieraObject (readonly)

Returns the value of attribute hiera.



9
10
11
# File 'lib/sis_ruby/client.rb', line 9

def hiera
  @hiera
end

#hooksObject (readonly)

Returns the value of attribute hooks.



9
10
11
# File 'lib/sis_ruby/client.rb', line 9

def hooks
  @hooks
end

#hostsObject (readonly)

Returns the value of attribute hosts.



9
10
11
# File 'lib/sis_ruby/client.rb', line 9

def hosts
  @hosts
end

#schemasObject (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.

Returns:

  • self for chaining this method after the constructor



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"
  options = {
      userpwd: username + ':' + password,
      headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
  }
  response = Typhoeus.post(dest, options)
  unless response.options[: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_sObject



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