Sha256: 86f77969122dfef91b05bb507e1dfef75e043c2e48243060d48a1d81c1f7303f

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
github-archive-0.0.1 lib/github/archive/event.rb