Class: PeopleDoc::V2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/people_doc.rb

Overview

PeopleDoc REST API v2 Client

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



193
194
195
196
197
198
199
200
201
202
# File 'lib/people_doc.rb', line 193

def initialize(options = {})
  options = default_options.merge(options)

  @application_id = options.fetch(:application_id)
  @application_secret = options.fetch(:application_secret)
  @base_url = options.fetch(:base_url)
  @client_id = options.fetch(:client_id)
  @logger = options.fetch(:logger, Logger.new(STDOUT))
  @request = HTTPartyRequest.new(@base_url, @logger, response_handlers)
end

Instance Method Details

#encoded_credentialsObject



204
205
206
# File 'lib/people_doc.rb', line 204

def encoded_credentials
  EncodedCredentials.new(@application_id, @application_secret).call
end

#get(resource) ⇒ Hash

Get a resource Makes a request for a resource from PeopleDoc and returns the response as a raw Hash.

Parameters:

  • the (String)

    resource endpoint

Returns:

  • (Hash)

    response data



244
245
246
247
248
249
# File 'lib/people_doc.rb', line 244

def get(resource)
  @request
    .get(base_headers, "api/v2/client/#{resource}")
rescue NotFound
  nil
end

#post_file(resource, file, payload = nil) ⇒ Hash

POST a file …

Parameters:

  • the (String)

    resource endpoint

  • file (...)
  • payload (Hash) (defaults to: nil)

    data

Returns:

  • (Hash)

    response data



259
260
261
262
263
264
265
266
267
268
# File 'lib/people_doc.rb', line 259

def post_file(resource, file, payload = nil)
  @request.post_file(
    base_headers.merge(
      'Content-Type' => 'multipart/form-data'
    ),
    "api/v2/#{resource}",
    file: file,
    data: payload ? payload.to_json : nil
  )
end

#put(resource, payload) ⇒ Hash

PUT a resource Makes a request to PUT new or existing resource details to PeopleDoc.

Parameters:

  • the (String)

    resource endpoint

  • payload (Hash)

    data

Returns:

  • (Hash)

    response data



277
278
279
280
281
282
283
# File 'lib/people_doc.rb', line 277

def put(resource, payload)
  @request.put(
    base_headers,
    "api/v2/client/#{resource}",
    payload.to_json
  )
end

#tokenToken

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

Returns:

  • (Token)

    token details



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/people_doc.rb', line 214

def token
  return @token if @token

  payload = {
    client_id: @client_id,
    grant_type: 'client_credentials',
    scope: 'client'
  }
  headers = {
    'Accept' => 'application/json',
    'Authorization' => "Basic #{encoded_credentials}",
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Host' => uri.host,
    'User-Agent' => 'PeopleDoc::V2::Client'
  }

  response = @request.post(headers, 'api/v2/client/tokens', payload)

  @token = Token.new(
    *response.values_at('access_token', 'token_type', 'expires_in')
  )
end