Class: KHL::HTTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/khl/http/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • raw_response (Net::HTTPResponse)

    The raw HTTP response



9
10
11
12
13
# File 'lib/khl/http/response.rb', line 9

def initialize(raw_response)
  @raw_response = raw_response
  @http_code = raw_response.code.to_i
  @body = JSON.parse(@raw_response.body)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/khl/http/response.rb', line 6

def body
  @body
end

#http_codeObject (readonly)

Returns the value of attribute http_code.



6
7
8
# File 'lib/khl/http/response.rb', line 6

def http_code
  @http_code
end

Instance Method Details

#codeObject

The code in the response body



20
21
22
# File 'lib/khl/http/response.rb', line 20

def code
  @body["code"]
end

#dataObject



15
16
17
# File 'lib/khl/http/response.rb', line 15

def data
  @body["data"]
end

#error?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/khl/http/response.rb', line 48

def error?
  !success?
end

#limit?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/khl/http/response.rb', line 40

def limit?
  @http_code == 429 || !@raw_response["X-Rate-Limit-Global"].nil?
end

#limit_bucketObject



36
37
38
# File 'lib/khl/http/response.rb', line 36

def limit_bucket
  @raw_response["X-Rate-Limit-Bucket"]
end

#max_request_limitObject



24
25
26
# File 'lib/khl/http/response.rb', line 24

def max_request_limit
  @raw_response["X-Rate-Limit-Limit"].to_i
end

#messageObject



52
53
54
# File 'lib/khl/http/response.rb', line 52

def message
  @body["message"]
end

#pageObject



56
57
58
# File 'lib/khl/http/response.rb', line 56

def page
  data.dig("meta", "page") || 0
end

#page_sizeObject



60
61
62
# File 'lib/khl/http/response.rb', line 60

def page_size
  data.dig("meta", "page_size") || 0
end

#page_totalObject



64
65
66
# File 'lib/khl/http/response.rb', line 64

def page_total
  data.dig("meta", "total") || 0
end

#remain_request_limitObject



28
29
30
# File 'lib/khl/http/response.rb', line 28

def remain_request_limit
  @raw_response["X-Rate-Limit-Remaining"].to_i
end

#reset_limit_timeObject



32
33
34
# File 'lib/khl/http/response.rb', line 32

def reset_limit_time
  @raw_response["X-Rate-Limit-Reset"].to_i
end

#success?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/khl/http/response.rb', line 44

def success?
  http_code == 200 && code.zero?
end