Class: AdpClient

Inherits:
Object
  • Object
show all
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.

Examples:


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'])
)

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

Class Method Summary collapse

Instance Method Summary collapse

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(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

Class Attribute Details

.base_urlObject

Returns the value of attribute base_url.



24
25
26
# File 'lib/adp_client.rb', line 24

def base_url
  @base_url
end

.client_idObject

Returns the value of attribute client_id.



24
25
26
# File 'lib/adp_client.rb', line 24

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



24
25
26
# File 'lib/adp_client.rb', line 24

def client_secret
  @client_secret
end

.loggerObject

Returns the value of attribute logger.



24
25
26
# File 'lib/adp_client.rb', line 24

def logger
  @logger
end

.pemObject

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.

Examples:

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

using the client

client = AdpClient.new

Yields:

  • (_self)

Yield Parameters:

  • _self (AdpClient)

    the object that the method was called on



44
45
46
47
# File 'lib/adp_client.rb', line 44

def configure
  yield self
  true
end

Instance Method Details

#default_optionsHash

Default options A Hash of default options populate by attributes set during configuration.

Returns:

  • (Hash)

    containing the default options



80
81
82
83
84
85
86
87
88
# 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

#delete(resource) ⇒ HTTParty::Response

Make a delete request. Makes a request to delete an existing resource from ADP.

Parameters:

  • the (String)

    resource endpoint

Returns:

  • (HTTParty::Response)

    the response



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.

Parameters:

  • the (String)

    resource endpoint

Returns:

  • (HTTParty::Response)

    the 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.

Parameters:

  • the (String)

    resource endpoint

Returns:

  • (Hash)

    response data



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.

Parameters:

  • the (String)

    resource endpoint

  • the (Hash)

    resource data

Returns:

  • (Hash)

    response data



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

#tokenToken

OAuth token Performs authentication using client credentials against the ADP Api.

Returns:

  • (Token)

    token details



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

  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