Class: Breathe::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/breathe/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:, type:) ⇒ Response

Returns a new instance of Response.



8
9
10
11
# File 'lib/breathe/response.rb', line 8

def initialize(response:, type:)
  @response = response
  @type = type
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/breathe/response.rb', line 4

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/breathe/response.rb', line 4

def type
  @type
end

Instance Method Details

#bodyObject



13
14
15
# File 'lib/breathe/response.rb', line 13

def body
  @body ||= response.data[type]
end

#first_pageObject



25
26
27
# File 'lib/breathe/response.rb', line 25

def first_page
  get_page(:first)
end

#last_pageObject



37
38
39
# File 'lib/breathe/response.rb', line 37

def last_page
  get_page(:last)
end

#next_pageObject



29
30
31
# File 'lib/breathe/response.rb', line 29

def next_page
  get_page(:next)
end

#per_pageObject



21
22
23
# File 'lib/breathe/response.rb', line 21

def per_page
  response.headers["per-page"].to_i
end

#previous_pageObject



33
34
35
# File 'lib/breathe/response.rb', line 33

def previous_page
  get_page(:prev)
end

#success?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/breathe/response.rb', line 41

def success?
  return true if /^2+/.match?(response.status.to_s)

  case response.status
  when 401
    raise Breathe::AuthenticationError, "The BreatheHR API returned a 401 error - are you sure you've set the correct API key?"
  else
    puts("Unknown Error")
    puts(response.status)
    puts(response.body)
    raise Breathe::UnknownError, "The BreatheHR API returned an unknown error"
  end
end

#totalObject



17
18
19
# File 'lib/breathe/response.rb', line 17

def total
  response.headers["total"].to_i
end