Class: AdpClient
- Inherits:
-
Object
- Object
- AdpClient
- Defined in:
- lib/adp_client.rb,
lib/adp_client/version.rb
Overview
Basic ADP Api Client Basic Api client that uses client credentials for authentication. The PEM certificate details must contain include the private key appended.
Defined Under Namespace
Classes: BadRequest, BadRequestHandler, BaseErrorHandler, Error, ErrorHandler, InvalidRequest, InvalidRequestHandler, ResourceNotFound, ResourceNotFoundHandler, Token, Unauthorized, UnauthorizedHandler, UnknownErrorHandler
Constant Summary collapse
- VERSION =
'1.0.0'
Class Attribute Summary collapse
-
.base_url ⇒ Object
Returns the value of attribute base_url.
-
.client_id ⇒ Object
Returns the value of attribute client_id.
-
.client_secret ⇒ Object
Returns the value of attribute client_secret.
-
.logger ⇒ Object
Returns the value of attribute logger.
-
.pem ⇒ Object
Returns the value of attribute pem.
Class Method Summary collapse
-
.configure {|_self| ... } ⇒ Object
Configures default AdpClient settings.
Instance Method Summary collapse
-
#default_options ⇒ Hash
Default options A Hash of default options populate by attributes set during configuration.
-
#delete(resource) ⇒ HTTParty::Response
Make a delete request.
-
#get(resource) ⇒ HTTParty::Response
Make a get request Makes a request for a resource from ADP and returns the full response.
-
#get_resource(resource) ⇒ Hash
Get a resource Makes a request for a resource from ADP and returns the response as a raw Hash.
-
#initialize(options = {}) ⇒ AdpClient
constructor
A new instance of AdpClient.
-
#post_resource(resource, data) ⇒ Hash
Post a resource Makes a request to post new resource details to ADP amd returns the response as a raw Hash.
-
#token ⇒ Token
OAuth token Performs authentication using client credentials against the ADP Api.
Constructor Details
#initialize(options = {}) ⇒ AdpClient
Returns a new instance of AdpClient.
65 66 67 68 69 70 71 72 73 |
# File 'lib/adp_client.rb', line 65 def initialize( = {}) = .merge() @client_id = [:client_id] @client_secret = [:client_secret] @base_url = [:base_url] = { pem: [:pem] } @logger = .fetch(:logger, Logger.new(STDOUT)) end |
Class Attribute Details
.base_url ⇒ Object
Returns the value of attribute base_url.
24 25 26 |
# File 'lib/adp_client.rb', line 24 def base_url @base_url end |
.client_id ⇒ Object
Returns the value of attribute client_id.
24 25 26 |
# File 'lib/adp_client.rb', line 24 def client_id @client_id end |
.client_secret ⇒ Object
Returns the value of attribute client_secret.
24 25 26 |
# File 'lib/adp_client.rb', line 24 def client_secret @client_secret end |
.logger ⇒ Object
Returns the value of attribute logger.
24 25 26 |
# File 'lib/adp_client.rb', line 24 def logger @logger end |
.pem ⇒ Object
Returns the value of attribute pem.
24 25 26 |
# File 'lib/adp_client.rb', line 24 def pem @pem end |
Class Method Details
.configure {|_self| ... } ⇒ Object
Configures default AdpClient settings.
44 45 46 47 |
# File 'lib/adp_client.rb', line 44 def configure yield self true end |
Instance Method Details
#default_options ⇒ Hash
Default options A Hash of default options populate by attributes set during configuration.
80 81 82 83 84 85 86 87 88 |
# File 'lib/adp_client.rb', line 80 def { base_url: AdpClient.base_url, client_id: AdpClient.client_id, client_secret: AdpClient.client_secret, logger: AdpClient.logger, pem: AdpClient.pem } end |
#delete(resource) ⇒ HTTParty::Response
Make a delete request. Makes a request to delete an existing resource from ADP.
128 129 130 131 132 133 134 135 136 137 138 |
# 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 |
#get(resource) ⇒ HTTParty::Response
Make a get request Makes a request for a resource from ADP and returns the full response.
146 147 148 149 150 151 152 153 |
# 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_resource(resource) ⇒ Hash
Get a resource Makes a request for a resource from ADP and returns the response as a raw Hash.
162 163 164 165 166 |
# File 'lib/adp_client.rb', line 162 def get_resource(resource) raises_unless_success do get(resource) end.parsed_response end |
#post_resource(resource, data) ⇒ Hash
Post a resource Makes a request to post new resource details to ADP amd returns the response as a raw Hash.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# 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 |
#token ⇒ Token
OAuth token Performs authentication using client credentials against the ADP Api.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/adp_client.rb', line 95 def token return @token if @token = .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, ) end.parsed_response @token = Token.new( *response.values_at('access_token', 'token_type', 'expires_in', 'scope') ) end |