class PeopleHR::API

Constants

API_ROOT

Attributes

api_key[R]
connection[R]

Public Class Methods

new(api_key:, connection: nil) click to toggle source
# File lib/peoplehr/api.rb, line 10
def initialize(api_key:, connection: nil)
  @api_key = api_key
  @connection = connection ||
    Faraday.new(url: API_ROOT) do |faraday|
      faraday.request :url_encoded
      faraday.adapter Faraday.default_adapter
    end
end

Public Instance Methods

request(action, params = {}) click to toggle source
# File lib/peoplehr/api.rb, line 19
def request(action, params = {})
  request = {
    "APIKey" => api_key,
    "Action" => action,
  }

  payload = params.merge(request)
  payload = JSON.generate(payload)

  response = connection.post do |req|
    req.url "/"
    req.headers["Content-Type"] = "application/json"
    req.body = payload
  end

  response = JSON.parse(response.body)

  if !response || response["isError"]
    fail APIError
  end

  response
rescue JSON::ParserError
  fail BadResponse
end