class GitHubBub::Response

Public Class Methods

create(response) click to toggle source
# File lib/git_hub_bub/response.rb, line 7
def self.create(response)
  self.new(response.data)
end

Public Instance Methods

first_page?() click to toggle source
# File lib/git_hub_bub/response.rb, line 82
def first_page?
  return true if first_url.nil?
  return false
end
first_url() click to toggle source
# File lib/git_hub_bub/response.rb, line 71
def first_url
  pagination['first_url']
end
json_body() click to toggle source
# File lib/git_hub_bub/response.rb, line 42
def json_body
 ::JSON.parse(self.body)
end
last_page?() click to toggle source
# 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() click to toggle source
# File lib/git_hub_bub/response.rb, line 67
def last_url
  pagination['last_url']
end
next_url() click to toggle source
# File lib/git_hub_bub/response.rb, line 58
def next_url
  pagination['next_url']
end
page_number_from_url(url) click to toggle source
# 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() click to toggle source
# File lib/git_hub_bub/response.rb, line 50
def pagination
  @pagination ||= parse_pagination
end
parse_pagination() click to toggle source
# 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() click to toggle source
# File lib/git_hub_bub/response.rb, line 54
def parsed_response
 response.body.inspect
end
prev_url() click to toggle source
# File lib/git_hub_bub/response.rb, line 62
def prev_url
  pagination['prev_url']
end
Also aliased as: previous_url
previous_url()
Alias for: prev_url
rate_limit_remaining() click to toggle source
# 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() click to toggle source
# 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) click to toggle source

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

# 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?() click to toggle source
# File lib/git_hub_bub/response.rb, line 46
def success?
 status.to_s =~ /^2.*/
end