Sha256: 87dceebda192dd89ed93151e1baaa7f6d1d65e25b743e63f3e7d5dfd49d26409

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

require 'ghn/version'
require 'ghn/token'
require 'ghn/options'
require 'ghn/command'
require 'github_api'

class Ghn
  def initialize(token, command, options)
    @token = token
    @command = command
    @options = options
  end

  def run
    send(@command.command, *@command.args)
  end

  def run_print
    run.each do |notification|
      if @options.open_browser?
        system "open #{notification}"
      else
        puts notification
      end
    end
  end

  def client
    @client ||= Github.new(oauth_token: @token.token)
  end

  def list(target = nil)
    params = {}
    unless target.nil?
      params['user'], params['repo'] = target.split('/')
    end

    client.activity.notifications.list(params).map { |notification|
      repo = notification.repository.full_name
      type, number = if notification.subject.url.match(/pulls/)
                       ['pull', notification.subject.url.match(/[^\/]+\z/).to_a.first]
                     else
                       ['issues', notification.subject.url.match(/[^\/]+\z/).to_a.first]
                     end
      if @options.mark_as_read?
        self.mark(notification.id)
        "[x] https://github.com/#{repo}/#{type}/#{number}"
      else
        "https://github.com/#{repo}/#{type}/#{number}"
      end
    }
  end

  def mark(id, read = true)
    client.activity.notifications.mark(thread_id: id, read: read)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ghn-0.0.5 lib/ghn.rb
ghn-0.0.4 lib/ghn.rb
ghn-0.0.3 lib/ghn.rb
ghn-0.0.2 lib/ghn.rb