class SoarCustomer::HapiProvider

Directory Provider to HAPI for all things related to Customer Data

@author Dane-Garrin Balia

Attributes

configuration[R]

Passed in at initialization with connection details, username and password etc

connection[R]

Faraday initialized

credentials[R]

Credentials is both the username and password

password[R]

Password for HAPI

server_url[R]

URL for HAPI

username[R]

Username for HAPI

Public Class Methods

new(configuration) click to toggle source

Constructor @param configuration [Hash] contains the configuration details for connecting to HAPI @note Cannot bootstrap without these details

# File lib/soar_customer.rb, line 31
def initialize(configuration)
  bootstrap(configuration)
end

Public Instance Methods

authenticate(credentials) click to toggle source

Used to authenticate against HAPI @return [Boolean] Returns true if authentication details are passed in @note Must be called first prior to any use

# File lib/soar_customer.rb, line 38
def authenticate(credentials)
  @credentials = credentials
  validate_credentials(credentials)
  remember_credentials(credentials)
end
bootstrap(configuration) click to toggle source

Bootstrapping the application via configuration

# File lib/soar_customer.rb, line 45
def bootstrap(configuration)
  @configuration = configuration
  validate_configuration(@configuration)
  remember_configuration(@configuration)
end
bootstrapped?() click to toggle source

Confirms if the class is instantiated with some form of configuration information

# File lib/soar_customer.rb, line 59
def bootstrapped?
  not @configuration.nil?
end
create_profile(customer) click to toggle source

Used to create a profile through HAPI @return [String] Returns body of message (JSON string) from HAPI response @note This is how you create a customer profile (not an account)

# File lib/soar_customer.rb, line 54
def create_profile(customer)
  post('/profile', customer)
end

Protected Instance Methods

post(endpoint, data) click to toggle source

Generic Method for Posting to HAPI @param endpoint [String] used for what resource to access on HAPI @param data [JSON] customer details

@note Since this is a “driver” it should bubble up messages

# File lib/soar_customer.rb, line 69
def post(endpoint, data)
  raise SoarHapiError.new('Missing endpoint') if endpoint.nil? or endpoint.empty?
  raise SoarHapiError.new('Missing data') if data.nil? or data.empty?
  setup_connection
  response = submit_to_hapi(endpoint, data)
  raise SoarHapiError.new("Invalid - HTTP status: #{response.status}") if response.status != 200
  response.body
rescue Faraday::Error::ConnectionFailed => e
  raise SoarHapiError.new('Connection failed to HAPI')
end

Private Instance Methods

remember_configuration(configuration) click to toggle source

Ensures configuration is bound for re-use

# File lib/soar_customer.rb, line 134
def remember_configuration(configuration)
  @configuration = configuration
  @server_url = @configuration['server_url']
end
remember_credentials(credentials) click to toggle source

Ensure credential is bound for re-use

# File lib/soar_customer.rb, line 140
def remember_credentials(credentials)
  @credentials = credentials
  @username = @credentials['username']
  @password = @credentials['password']
  true
end
setup_connection() click to toggle source

Sets up the connection by instantiating Faraday

@note Raises exception to Method post(endpoint, data)

# File lib/soar_customer.rb, line 99
def setup_connection
  @connection = Faraday.new(:url => @server_url)
  @connection.basic_auth(@username, @password)
rescue Exception => e
  raise 
end
submit_to_hapi(endpoint, data) click to toggle source

Submit information to HAPI (only post)

@note Raises exception to Method post(endpoint, data)

# File lib/soar_customer.rb, line 85
def submit_to_hapi(endpoint, data)
  output = @connection.post do |req|
    req.url endpoint
    req.headers['Content-Type'] = 'application/json'
    req.body = data
  end
  output
rescue Exception => e
  raise 
end
validate_configuration(configuration) click to toggle source

Validation for configuration hash

@note Returns a SoarHapiError if validation fails

# File lib/soar_customer.rb, line 109
def validate_configuration(configuration)
  raise SoarHapiError.new('No configuration') if configuration.nil?
  raise SoarHapiError.new('Empty configuration') if configuration == {}
  raise SoarHapiError.new('Invalid configuration') if not configuration.is_a?(Hash)
  raise SoarHapiError.new('Missing server url') if configuration['server_url'].nil?
  raise SoarHapiError.new('Invalid server url') unless validate_server_url?(configuration['server_url'])
end
validate_credentials(credentials) click to toggle source

Validation for credentials

@note Returns a SoarHapiError if validation fails

# File lib/soar_customer.rb, line 125
def validate_credentials(credentials)
  raise SoarHapiError.new('Missing credentials') if credentials.nil?
  raise SoarHapiError.new('Empty credentials') if credentials == {}
  raise SoarHapiError.new('Invalid credentials') if not credentials.is_a?(Hash)
  raise SoarHapiError.new('Missing username') if credentials['username'].nil? or credentials['username'].empty?
  raise SoarHapiError.new('Missing password') if credentials['password'].nil? or credentials['password'].empty? 
end
validate_server_url?(server_url) click to toggle source

Validation for Service Url

# File lib/soar_customer.rb, line 118
def validate_server_url?(server_url)
  server_url =~ URI::regexp
end