Class: GitHubBub::Response
- Inherits:
-
Excon::Response
- Object
- Excon::Response
- GitHubBub::Response
- Defined in:
- lib/git_hub_bub/response.rb
Class Method Summary collapse
Instance Method Summary collapse
- #first_page? ⇒ Boolean
- #first_url ⇒ Object
- #header_links ⇒ Object
- #json_body ⇒ Object
- #last_page? ⇒ Boolean
- #last_url ⇒ Object
- #next_url ⇒ Object
- #page_number_from_url(url) ⇒ Object
- #pagination ⇒ Object
- #parse_pagination ⇒ Object
- #parsed_response ⇒ Object
- #prev_url ⇒ Object (also: #previous_url)
- #rate_limit_remaining ⇒ Object
-
#rate_limit_reset_time_left ⇒ Object
in seconds.
-
#rate_limit_sleep!(bypass_sleep: false) ⇒ Object
When no time is left we want to sleep until our limit is reset i.e.
- #success? ⇒ Boolean
Class Method Details
.create(response) ⇒ Object
7 8 9 |
# File 'lib/git_hub_bub/response.rb', line 7 def self.create(response) self.new(response.data) end |
Instance Method Details
#first_page? ⇒ Boolean
82 83 84 85 |
# File 'lib/git_hub_bub/response.rb', line 82 def first_page? return true if first_url.nil? return false end |
#first_url ⇒ Object
71 72 73 |
# File 'lib/git_hub_bub/response.rb', line 71 def first_url pagination['first_url'] end |
#header_links ⇒ Object
92 93 94 |
# File 'lib/git_hub_bub/response.rb', line 92 def header_links (headers['link'] || headers['Link'] || "").split(',') end |
#json_body ⇒ Object
42 43 44 |
# File 'lib/git_hub_bub/response.rb', line 42 def json_body ::JSON.parse(self.body) end |
#last_page? ⇒ Boolean
75 76 77 78 79 80 |
# File 'lib/git_hub_bub/response.rb', line 75 def last_page? return true if next_url.nil? last_page_number = page_number_from_url(last_url) next_page_number = page_number_from_url(next_url) return next_page_number > last_page_number end |
#last_url ⇒ Object
67 68 69 |
# File 'lib/git_hub_bub/response.rb', line 67 def last_url pagination['last_url'] end |
#next_url ⇒ Object
58 59 60 |
# File 'lib/git_hub_bub/response.rb', line 58 def next_url pagination['next_url'] end |
#page_number_from_url(url) ⇒ Object
87 88 89 90 |
# File 'lib/git_hub_bub/response.rb', line 87 def page_number_from_url(url) query = ::URI.parse(url).query ::CGI.parse(query)["page"].first.to_i end |
#pagination ⇒ Object
50 51 52 |
# File 'lib/git_hub_bub/response.rb', line 50 def pagination @pagination ||= parse_pagination end |
#parse_pagination ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/git_hub_bub/response.rb', line 96 def parse_pagination header_links.each_with_object({}) do |element, hash| key = element[/rel=["'](.*)['"]/, 1] value = element[/<(.*)>/, 1] hash["#{key}_url"] = value end end |
#parsed_response ⇒ Object
54 55 56 |
# File 'lib/git_hub_bub/response.rb', line 54 def parsed_response response.body.inspect end |
#prev_url ⇒ Object Also known as: previous_url
62 63 64 |
# File 'lib/git_hub_bub/response.rb', line 62 def prev_url pagination['prev_url'] end |
#rate_limit_remaining ⇒ Object
11 12 13 14 |
# File 'lib/git_hub_bub/response.rb', line 11 def rate_limit_remaining limit_remaining = headers["X-RateLimit-Limit"] Integer(limit_remaining) end |
#rate_limit_reset_time_left ⇒ Object
in seconds
16 17 18 19 20 |
# File 'lib/git_hub_bub/response.rb', line 16 def rate_limit_reset_time_left # in seconds utc_epoch_seconds = headers["X-RateLimit-Reset"] utc_epoch_seconds = Integer(utc_epoch_seconds) return utc_epoch_seconds - Time.now.utc.to_i end |
#rate_limit_sleep!(bypass_sleep: false) ⇒ Object
When no time is left we want to sleep until our limit is reset i.e. remaining is 1 so time/1 => time
When we have plenty of requests left then we want to sleep for too long i.e. time / 1000 => smaller amount of time
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/git_hub_bub/response.rb', line 27 def rate_limit_sleep!(bypass_sleep: false) remaining = rate_limit_remaining time_left = rate_limit_reset_time_left return 0 if time_left <= 0 return 0 if remaining > 1000 if remaining > 0 val = time_left / remaining.to_f else val = time_left end sleep(val) unless bypass_sleep return val end |
#success? ⇒ Boolean
46 47 48 |
# File 'lib/git_hub_bub/response.rb', line 46 def success? status.to_s =~ /^2.*/ end |