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.



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

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

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#bodyObject



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

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

#first_pageObject



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

def first_page
  get_page(:first)
end

#last_pageObject



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

def last_page
  get_page(:last)
end

#next_pageObject



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

def next_page
  get_page(:next)
end

#per_pageObject



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

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

#previous_pageObject



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

def previous_page
  get_page(:prev)
end

#success?Boolean

Returns:

  • (Boolean)


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

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



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

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