Module: Haipa::Client::Common::Configurable

Defined in:
lib/haipa_rest/common/configurable.rb,
lib/haipa_rest.rb

Overview

The Haipa::Common::Configurable module provides basic configuration for Haipa activities.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_idString

Returns client id.

Returns:

  • (String)

    client id.



15
16
17
# File 'lib/haipa_rest/common/configurable.rb', line 15

def client_id
  @client_id
end

#client_keyString

Returns client key.

Returns:

  • (String)

    client key



21
22
23
# File 'lib/haipa_rest/common/configurable.rb', line 21

def client_key
  @client_key
end

#client_key_fileString

Returns path to client key file.

Returns:

  • (String)

    path to client key file



18
19
20
# File 'lib/haipa_rest/common/configurable.rb', line 18

def client_key_file
  @client_key_file
end

#credentialsMsRest::ServiceClientCredentials

Returns credentials to authorize HTTP requests made by the service client.

Returns:

  • (MsRest::ServiceClientCredentials)

    credentials to authorize HTTP requests made by the service client.



12
13
14
# File 'lib/haipa_rest/common/configurable.rb', line 12

def credentials
  @credentials
end

#identity_endpointString

Returns url to identity endpoint.

Returns:

  • (String)

    url to identity endpoint.



24
25
26
# File 'lib/haipa_rest/common/configurable.rb', line 24

def identity_endpoint
  @identity_endpoint
end

Class Method Details

.keysArray

List of configurable keys for Haipa::Client::Common::Client.

Returns:

  • (Array)

    of option keys.



31
32
33
# File 'lib/haipa_rest/common/configurable.rb', line 31

def keys
  @keys ||= [:client_id, :client_key_file, :identity_endpoint ]
end

Instance Method Details

#configObject



75
76
77
# File 'lib/haipa_rest/common/configurable.rb', line 75

def config
  self
end

#configure {|_self| ... } ⇒ Object

Set configuration options using a block.

Yields:

  • (_self)

Yield Parameters:



39
40
41
# File 'lib/haipa_rest/common/configurable.rb', line 39

def configure
  yield self
end

#reset!(options = {}) ⇒ Object

Resets the configurable options to provided options or defaults. This will also creates MsRest::TokenCredentials to be used for subsequent clients.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/haipa_rest/common/configurable.rb', line 47

def reset!(options = {})
  Haipa::Client::Common::Configurable.keys.each do |key|
    default_value = Haipa::Client::Common::Default.options[key]
    instance_variable_set(:"@#{key}", options.fetch(key, default_value))
  end

  if(options[:client_key].nil?)
    # The user has not passed in the client key. try to read it from client_key_file
    self.client_key = OpenSSL::PKey::RSA.new File.read self.client_key_file unless self.client_key_file.nil?       
  end

  if(options[:credentials].nil?)
    # The user has not passed in the credentials. So, the api has to
    # build the credentials itself.
    fail ArgumentError, 'client_id is nil' if self.client_id.nil?
    fail ArgumentError, 'client_key is nil' if self.client_key.nil?
    fail ArgumentError, 'identity_endpoint is nil' if self.identity_endpoint.nil?

    self.credentials = MsRest::TokenCredentials.new(
        Haipa::Client::ApplicationTokenProvider.new(
            self.client_id, self.client_key, self.identity_endpoint))
  else
    self.credentials = options[:credentials]
  end

  self
end