class PeopleDoc::V2::Client

PeopleDoc REST API v2 Client

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

encoded_credentials() click to toggle source
# File lib/people_doc.rb, line 204
def encoded_credentials
  EncodedCredentials.new(@application_id, @application_secret).call
end
get(resource) click to toggle source

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

@param [String] the resource endpoint @return [Hash] response data

# 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) click to toggle source

POST a file …

@param [String] the resource endpoint @param […] file @param [Hash] payload data @return [Hash] response data

# 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) click to toggle source

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

@param [String] the resource endpoint @param [Hash] payload data @return [Hash] response data

# File lib/people_doc.rb, line 277
def put(resource, payload)
  @request.put(
    base_headers,
    "api/v2/client/#{resource}",
    payload.to_json
  )
end
token() click to toggle source

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

@return [Token] token details

# 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

Protected Instance Methods

base_headers() click to toggle source
# File lib/people_doc.rb, line 287
def base_headers
  {
    'Accept' => 'application/json',
    'Authorization' => "Bearer #{token.access_token}",
    'Content-Type' => 'application/json',
    'Host' => uri.host,
    'User-Agent' => 'PeopleDoc::V2::Client'
  }
end
default_options() click to toggle source

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

@return [Hash] containing the default options

# File lib/people_doc.rb, line 303
def default_options
  {
    application_id: PeopleDoc::V2.application_id,
    application_secret: PeopleDoc::V2.application_secret,
    base_url: PeopleDoc::V2.base_url,
    client_id: PeopleDoc::V2.client_id,
    logger: PeopleDoc::V2.logger
  }
end
response_handlers() click to toggle source
# File lib/people_doc.rb, line 313
def response_handlers
  RESPONSE_HANDLERS.merge(
    bad_request: PeopleDoc::ResponseHandlers::V2::HandleBadRequest,
    unauthorized: PeopleDoc::ResponseHandlers::V2::HandleUnauthorized
  )
end
uri() click to toggle source
# File lib/people_doc.rb, line 320
def uri
  @uri ||= URI.parse(@base_url)
end