Class: Breathe::Client

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

Constant Summary collapse

BASE_URL =
ENV.fetch("BREATHE_API_URL", "https://api.breathehr.com/v1/")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, auto_paginate: false) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/breathe/client.rb', line 7

def initialize(api_key:, auto_paginate: false)
  @api_key = api_key
  @auto_paginate = auto_paginate
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/breathe/client.rb', line 3

def api_key
  @api_key
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



3
4
5
# File 'lib/breathe/client.rb', line 3

def last_response
  @last_response
end

Instance Method Details

#absencesObject



12
13
14
# File 'lib/breathe/client.rb', line 12

def absences
  @absences ||= Absences.new(self)
end

#agentObject



37
38
39
40
41
42
# File 'lib/breathe/client.rb', line 37

def agent
  Sawyer::Agent.new(BASE_URL, links_parser: Sawyer::LinkParsers::Simple.new) do |http|
    http.headers["Content-Type"] = "application/json"
    http.headers["X-Api-Key"] = api_key
  end
end

#employee_training_coursesObject



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

def employee_training_courses
  @employee_training_courses ||= EmployeeTrainingCourses.new(self)
end

#employeesObject



20
21
22
# File 'lib/breathe/client.rb', line 20

def employees
  @employees ||= Employees.new(self)
end

#request(method:, path:, data: {}, options: {}) ⇒ Object



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

def request(method:, path:, data: {}, options: {})
  @last_response = agent.call(method, path, data, options)
  @last_response
end

#response(method:, path:, args: {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/breathe/client.rb', line 28

def response(method:, path:, args: {})
  response = request(method: method, path: path, options: {query: args})
  parsed_response = Response.new(response: response, type: path.split("/").first)

  if parsed_response.success?
    @auto_paginate ? auto_paginated_response(parsed_response) : parsed_response
  end
end

#sicknessesObject



16
17
18
# File 'lib/breathe/client.rb', line 16

def sicknesses
  @sicknesses ||= Sicknesses.new(self)
end