Sha256: 9e8c4c42227d07242d1e69beeff5c4937aa8685c40463833373c581bdf6408bc

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require_relative 'github_client'
module GithubStats
  # Retrieves issues from Github and upserts them into the database.
  class IssueIngester
    attr_accessor :search_string, :options

    def initialize(search_string, options)
      self.search_string = search_string
      self.options = options
    end

    def ingest
      return unless options[:ingest]
      github.search_issues(search_string).each(&method(:insert_or_update))
    end

    private def insert_or_update(result)
      issue = issues.first(github_id: result[:id])
      return insert(result) unless issue
      return update(issue, result) if issue[:closed_at] != result[:closed_at] ||
                                      issue[:created_at] != result[:created_at]
    end

    private def update(issue, result)
      issue.update(closed_at: result[:closed_at],
                   created_at: result[:created_at])
    end

    private def insert(result)
      issues.insert(search_string: search_string, github_id: result[:id],
                    closed_at: result[:closed_at],
                    created_at: result[:created_at],
                    url: result[:url])
    end

    private def issues
      db.issues.where(search_string: search_string)
    end

    private def db
      @db ||= Database.new(options)
    end

    private def github
      @github ||= GithubClient.new(options)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
github-stats-0.1.0 lib/github_stats/issue_ingester.rb