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



12
13
14
15
16
# File 'lib/khl/http/response.rb', line 12

def initialize(raw_response)
  @raw_response = raw_response
  @http_code = raw_response.code.to_i
  @body = ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(@raw_response.body))
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#http_codeObject (readonly)

Returns the value of attribute http_code.



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

def http_code
  @http_code
end

Instance Method Details

#codeObject

The code in the response body



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

def code
  @body[:code]
end

#dataObject



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

def data
  @body[:data]
end

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  !success?
end

#limit?Boolean

Returns:

  • (Boolean)


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

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

#limit_bucketObject



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

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

#max_request_limitObject



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

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

#messageObject



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

def message
  @body[:message]
end

#pageObject



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

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

#page_sizeObject



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

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

#page_totalObject



67
68
69
# File 'lib/khl/http/response.rb', line 67

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

#remain_request_limitObject



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

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

#reset_limit_timeObject



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

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

#success?Boolean

Returns:

  • (Boolean)


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

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