module Github module Archive class Event < ::ActiveRecord::Base scope :in_time_range, ->(range) { where(:gh_created_at => range) } scope :with_event_type, ->(type) { where(:event_type => type) } class << self def create_with_json(raw_json) json = raw_json.with_indifferent_access if related_event?(json[:type]) url = "UNKNOWN" if json[:repo] url = json[:repo][:url] elsif json[:repository] url = json[:repository][:url] end self.create( url: url, event_type: json[:type], gh_created_at: json[:created_at] ) end end def results_for_range_and_type(range, type, limit) self.in_time_range(range) .with_event_type(type) .group(:url) .limit(limit) .order('count(*) DESC') .count(:url) end private def related_event?(event_type) event_type != "GistEvent" && event_type != "FollowEvent" && event_type != "TeamAddEvent" end end end end end