class AdpClient
Basic ADP Api Client Basic Api client that uses client credentials for authentication. The PEM certificate details must contain include the private key appended.
@example
client = AdpClient.new( client_id: ENV['ADP_CLIENT_ID'], client_secret: ENV['ADP_CLIENT_SECRET'], base_ur: ENV['ADP_API_HOST'], pem: File.read(ENV['ADP_SSL_CERT_PATH']) )
Constants
- Token
- VERSION
Attributes
Public Class Methods
Configures default AdpClient
settings.
@example configuring the client defaults
AdpClient.configure do |config| config.base_url = 'https://api.adp.com' config.client_id = 'client_id' config.client_secret = 'client_secret' config.pem = '{cert and key data}' config.logger = Logger.new(STDOUT) end
@example using the client
client = AdpClient.new
# File lib/adp_client.rb, line 44 def configure yield self true end
# File lib/adp_client.rb, line 65 def initialize(options = {}) options = default_options.merge(options) @client_id = options[:client_id] @client_secret = options[:client_secret] @base_url = options[:base_url] @options = { pem: options[:pem] } @logger = options.fetch(:logger, Logger.new(STDOUT)) end
Public Instance Methods
Default options A {Hash} of default options populate by attributes set during configuration.
@return [Hash] containing the default options
# File lib/adp_client.rb, line 80 def default_options { base_url: AdpClient.base_url, client_id: AdpClient.client_id, client_secret: AdpClient.client_secret, logger: AdpClient.logger, pem: AdpClient.pem } end
Make a delete request. Makes a request to delete an existing resource from ADP.
@param [String] the resource endpoint @return [HTTParty::Response] the response
# File lib/adp_client.rb, line 128 def delete(resource) headers = base_headers.merge('Content-Type' => 'application/json') url = "#{@base_url}/#{resource}" @logger.debug("DELETE request Url: #{url}") @logger.debug("-- Headers: #{headers}") x = HTTParty.delete(url, headers: headers) puts x.inspect x end
Make a get request Makes a request for a resource from ADP and returns the full response.
@param [String] the resource endpoint @return [HTTParty::Response] the response
# File lib/adp_client.rb, line 146 def get(resource) url = "#{@base_url}/#{resource}" @logger.debug("GET request Url: #{url}") @logger.debug("-- Headers: #{base_headers}") HTTParty.get(url, headers: base_headers) end
Get a resource Makes a request for a resource from ADP and returns the response as a raw {Hash}.
@param [String] the resource endpoint @return [Hash] response data
# File lib/adp_client.rb, line 162 def get_resource(resource) raises_unless_success do get(resource) end.parsed_response end
Post a resource Makes a request to post new resource details to ADP amd returns the response as a raw {Hash}.
@param [String] the resource endpoint @param [Hash] the resource data @return [Hash] response data
# File lib/adp_client.rb, line 176 def post_resource(resource, data) headers = base_headers .merge('Content-Type' => 'application/json') url = "#{@base_url}/#{resource}" @logger.debug("POST request Url: #{url}") @logger.debug("-- Headers: #{headers}") @logger.debug("-- JSON #{data.to_json}") raises_unless_success do HTTParty .post(url, body: data.to_json, headers: headers) end.parsed_response end
OAuth token Performs authentication using client credentials against the ADP Api.
@return [Token] token details
# File lib/adp_client.rb, line 95 def token return @token if @token options = @options.merge( body: { client_id: @client_id, client_secret: @client_secret, grant_type: 'client_credentials' }, headers: { 'Accept' => 'application/json', 'Host' => uri.host } ) url = "#{@base_url}/auth/oauth/v2/token" @logger.debug("Request token from #{url}") response = raises_unless_success do HTTParty.post(url, options) end.parsed_response @token = Token.new( *response.values_at('access_token', 'token_type', 'expires_in', 'scope') ) end
Protected Instance Methods
# File lib/adp_client.rb, line 193 def base_headers { 'Accept' => 'application/json', 'Authorization' => "Bearer #{token.access_token}", 'Connection' => 'Keep-Alive', 'Host' => uri.host, 'User-Agent' => 'AdpClient' } end
# File lib/adp_client.rb, line 203 def raises_unless_success httparty = yield [ ErrorHandler, InvalidRequestHandler, ResourceNotFoundHandler, UnauthorizedHandler, BadRequestHandler, UnknownErrorHandler ].each do |response_handler_type| response_handler_type.new(httparty).call end httparty end
# File lib/adp_client.rb, line 316 def uri @uri ||= URI.parse(@base_url) end