Class: TeachersPet::Actions::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/teachers_pet/actions/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/teachers_pet/actions/base.rb', line 10

def initialize(opts={})
  @options = opts.symbolize_keys
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/teachers_pet/actions/base.rb', line 8

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/teachers_pet/actions/base.rb', line 8

def options
  @options
end

Instance Method Details

#init_clientObject



34
35
36
37
38
39
# File 'lib/teachers_pet/actions/base.rb', line 34

def init_client
  puts "=" * 50
  puts "Authenticating to GitHub..."
  octokit = Octokit::Client.new(self.octokit_config)
  @client = TeachersPet::ClientDecorator.new(octokit)
end

#octokit_configObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/teachers_pet/actions/base.rb', line 14

def octokit_config
  opts = {
    api_endpoint: self.options[:api],
    web_endpoint: self.options[:web],
    login: self.options[:username],
    # Organizations can get big, pull in all pages
    auto_paginate: true
  }

  if self.options[:token]
    opts[:access_token] = self.options[:token]
  elsif self.options[:password]
    opts[:password] = self.options[:password]
  else
    raise Thor::RequiredArgumentMissingError.new("No value provided for option --password or --token")
  end

  opts
end

#read_file(filename) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/teachers_pet/actions/base.rb', line 41

def read_file(filename)
  map = Hash.new
  File.open(filename).each_line do |team|
    # Team can be a single user, or a team name and multiple users
    # Trim whitespace, otherwise issues occur
    team.strip!
    items = team.split(' ')
    items.each do |item|
      abort("No users can be named 'owners' (in any case)") if 'owners'.eql?(item.downcase)
    end

    if map[items[0]].nil?
      map[items[0]] = Array.new
      puts " -> #{items[0]}"
      if (items.size > 1)
        print "  \\-> members: "
        1.upto(items.size - 1) do |i|
          print "#{items[i]} "
          map[items[0]] << items[i]
        end
        print "\n"
      else
        map[items[0]] << items[0]
      end
    end
  end

  map
end

#read_students_fileObject



71
72
73
74
75
# File 'lib/teachers_pet/actions/base.rb', line 71

def read_students_file
  student_file = self.options[:students]
  puts "Loading students:"
  read_file(student_file)
end