class GemStream::Follower

Constants

MAX_RUBY_GEMS_QUERY_RANGE_IN_SECONDS
RUBYGEMS_ENDPOINT

Attributes

synced_up_to_time[R]

Public Class Methods

follow_from(start_time) click to toggle source
# File lib/gem_stream/follower.rb, line 12
def self.follow_from(start_time)
  self.new(start_time).tap(&:follow)
end
new(start_time) click to toggle source
# File lib/gem_stream/follower.rb, line 16
def initialize(start_time)
  @synced_up_to_time = start_time
  @on_version = GemStream.configuration.on_version.dup
  @on_synced = GemStream.configuration.on_synced.dup
  @api_call_interval = GemStream.configuration.api_call_interval.dup
  @keep_streaming = true
end

Public Instance Methods

follow() click to toggle source
# File lib/gem_stream/follower.rb, line 24
def follow
  while keep_streaming?
    query_rubygems
    increment_query_window
  end

  synced
end

Private Instance Methods

increment_query_window() click to toggle source
# File lib/gem_stream/follower.rb, line 69
def increment_query_window
  @synced_up_to_time = [
    (@synced_up_to_time + MAX_RUBY_GEMS_QUERY_RANGE_IN_SECONDS),
    Time.now
  ].min
end
keep_streaming?() click to toggle source
# File lib/gem_stream/follower.rb, line 35
def keep_streaming?
  @keep_streaming
end
query_params(page) click to toggle source
# File lib/gem_stream/follower.rb, line 76
def query_params(page)
  {
    from: @synced_up_to_time.iso8601,
    to: (@synced_up_to_time + MAX_RUBY_GEMS_QUERY_RANGE_IN_SECONDS).iso8601,
    page: page,
  }
end
query_rubygems(page: 1) click to toggle source
# File lib/gem_stream/follower.rb, line 43
def query_rubygems(page: 1)
  uri = URI(RUBYGEMS_ENDPOINT)
  params = query_params(page)
  uri.query = URI.encode_www_form(params)
  response = Net::HTTP.get_response(uri)

  if response.code != '200'
    msg = "Got status #{response.code} from rubygems for #{RUBYGEMS_ENDPOINT} with options: #{params.inspect}"
    raise GemStream::ApiError, msg
  end

  versions = JSON.parse(response.body)

  if versions.size == 0
    @keep_streaming = page == 1
    return
  end

  versions.each do |version|
    @on_version.call(version)
  end

  sleep @api_call_interval
  query_rubygems(page: page + 1)
end
synced() click to toggle source
# File lib/gem_stream/follower.rb, line 39
def synced
  @on_synced.call()
end