makers_toolbelt 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5c7bcc459271d34a0af77e7190c313db9a5ee33
4
- data.tar.gz: 0d93bea6d24743f08bd144c33f3fa60b90468f83
3
+ metadata.gz: 902d2b913cb0794b925d99ef188e07dc4b8ca93c
4
+ data.tar.gz: 2a520dd7cbed0a78d81b10a2a4d1f170a922c07a
5
5
  SHA512:
6
- metadata.gz: c9b0f4268dfdda1c4fe98a09d2770e9be6616dc7efaf2ccbd926a6547ae1f713c43c3f6408f6d9760ee4eaf2dc4d87f74dcff83304472842522e8751247c246d
7
- data.tar.gz: 0faccf941161b48a6ef31b31f8ff2df3724365170f74a6f427d76ef5fb2bb36a8c7014a5bf08c0fec05c0c4d943a22a8550bbc3aeb37b597afc6cc1af6e68cd1
6
+ metadata.gz: 15855dab65d239cb3af54845b93ec40dac4480efdb2468e77bb6be58862a85398e31f602668147570dccf7e30f2bb130b1282d8cc86cb69c2cf3efe6c3459b28
7
+ data.tar.gz: 3baaf1de081d49efd4c0c3e900be64b8eab0e2794f596aec284a72235dd119018e07f9b6c8bd412148882ba6feb405df4e3416d5010889380613b7dd49ebb3fb
data/command_map.yml ADDED
@@ -0,0 +1,9 @@
1
+ fetch_pull_requests:
2
+ class: FetchPullRequests
3
+ args: [filter]
4
+ generate_pairs:
5
+ class: GeneratePairs
6
+ args: [source]
7
+ randomize_bytes:
8
+ class: RandomizeBytes
9
+ args: []
@@ -0,0 +1,8 @@
1
+ require 'yaml'
2
+ require_relative 'makers_toolbelt/fetch_pull_requests'
3
+ require_relative 'makers_toolbelt/generate_pairs'
4
+ require_relative 'makers_toolbelt/randomize_bytes'
5
+
6
+ module MakersToolbelt
7
+ class NotFoundError < StandardError; end
8
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'questions/get_uri'
2
+ require_relative 'questions/get_positive_number'
3
+ require_relative '../../makers_toolbelt'
4
+
5
+ module MakersToolbelt
6
+ module CommandLine
7
+ class Interface
8
+
9
+ attr_reader :answers
10
+ private :answers
11
+
12
+ def self.ask_questions(question_set)
13
+ new.send(question_set)
14
+ end
15
+
16
+ def initialize
17
+ @answers = {}
18
+ end
19
+
20
+ def randomize_bytes
21
+ answers[:cohort_id] = GetPositiveNumber.call(question: "Enter cohort id: ")
22
+ answers[:number_of_bytes] = GetPositiveNumber.call(question: "Enter required number of bytes: ")
23
+ answers[:base_uri] = GetURI.call(question: "Enter base uri (press enter for #{HubClient::HUB_URL}): ")
24
+ answers
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ require_relative './question'
2
+
3
+ module MakersToolbelt
4
+ module CommandLine
5
+ class GetPositiveNumber < Question
6
+
7
+ def call
8
+ super.to_i
9
+ end
10
+
11
+ private
12
+
13
+ def validate(input)
14
+ unless is_number?(input) && is_greater_than_zero?(input)
15
+ raise "You must enter a valid number"
16
+ end
17
+ input
18
+ end
19
+
20
+ def is_number?(input)
21
+ input.to_i.to_s == input
22
+ end
23
+
24
+ def is_greater_than_zero?(input)
25
+ input.to_i > 0
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ require_relative './question'
2
+ require_relative '../../../makers_toolbelt'
3
+
4
+ module MakersToolbelt
5
+ module CommandLine
6
+ class GetURI < Question
7
+
8
+ private
9
+
10
+ def validate(input)
11
+ return input if input.empty?
12
+ raise "You must enter a valid uri e.g. https://example.com" unless valid_uri?(input)
13
+ input
14
+ end
15
+
16
+ def valid_uri?(input)
17
+ input =~ /\A#{URI::regexp(['http', 'https'])}\z/
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module MakersToolbelt
2
+ module CommandLine
3
+ class Question
4
+ attr_reader :question, :instream, :outstream
5
+ private :question, :instream, :outstream
6
+
7
+ def self.call(question:, instream: $stdin, outstream: $stdout)
8
+ new(question, instream, outstream).call
9
+ end
10
+
11
+ def initialize(question, instream, outstream)
12
+ @question = question
13
+ @instream = instream
14
+ @outstream = outstream
15
+ end
16
+
17
+ def call
18
+ outstream.print(question)
19
+ validate(instream.gets.chomp)
20
+ end
21
+
22
+ private
23
+
24
+ def validate(input)
25
+ raise "Not Implemented: #{__method__}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,108 @@
1
+ require 'octokit'
2
+
3
+ module MakersToolbelt
4
+ class FetchPullRequests
5
+ OPEN_SWITCHES = ['-a', '--all', '-o', '--open']
6
+ CLOSED_SWITCHES = ['-a', '--all', '-c', '--closed']
7
+
8
+ GITHUB_REMOTE_REGEX = /github\.com(?::|\/)?(.+\/[\w\-]+)(?:\.git|\/)?\n/
9
+
10
+ attr_reader :options, :filter
11
+
12
+ def initialize(options = {})
13
+ @options = options
14
+ @filter = options[:filter] || '--open'
15
+ end
16
+
17
+ def run()
18
+ pulls = []
19
+ pulls << open_pull_requests if include_open?
20
+ pulls << closed_pull_requests if include_closed?
21
+
22
+ pulls.flatten!.select! {|p| p.head.repo }
23
+
24
+ pulls.map { |p| [p.head.repo.owner.login, p.head.repo.html_url] }.each do |name, url|
25
+ puts "adding #{name}"
26
+ `git remote add #{name} #{url}`
27
+ puts "fetching #{url}"
28
+ `git fetch #{name}`
29
+ end
30
+ end
31
+
32
+ def repo
33
+ @repo ||= repo_from_remote_path(origin_path)
34
+ end
35
+
36
+ def repo_from_remote_path(path)
37
+ @regex ||= GITHUB_REMOTE_REGEX
38
+ @regex.match(path).captures.first
39
+ rescue
40
+ fail NotFoundError.new('Could not find remote origin')
41
+ end
42
+
43
+ def include_open?
44
+ OPEN_SWITCHES.include? filter
45
+ end
46
+
47
+ def include_closed?
48
+ CLOSED_SWITCHES.include? filter
49
+ end
50
+
51
+ private
52
+
53
+ def open_pull_requests
54
+ puts "fetching open pull requests from #{repo}..."
55
+ fetch_pull_requests('open')
56
+ end
57
+
58
+ def closed_pull_requests
59
+ puts "fetching closed pull requests from #{repo}..."
60
+ fetch_pull_requests('closed')
61
+ end
62
+
63
+ def fetch_pull_requests(state)
64
+ pulls = client.pull_requests repo, state: state, per_page: 100
65
+ fetch_more_pages(pulls)
66
+ rescue StandardError => e
67
+ puts "Error occurred while fetching pull requests: #{e.message}"
68
+ ensure
69
+ pulls
70
+ end
71
+
72
+ def fetch_more_pages(into_array)
73
+ next_page = client.last_response.rels[:next]
74
+ while next_page do
75
+ response = next_page.get
76
+ into_array << response.data
77
+ next_page = response.rels[:next]
78
+ end
79
+ into_array
80
+ end
81
+
82
+ def origin_path
83
+ @origin_path ||= `git remote show origin | grep 'Fetch URL:'`
84
+ end
85
+
86
+ def client
87
+ @client ||= create_client
88
+ end
89
+
90
+ def create_client
91
+ token = ENV['MAKERS_TOOLBELT_GITHUB_TOKEN']
92
+ if token
93
+ Octokit::Client.new access_token: token
94
+ else
95
+ create_client_from_config
96
+ end
97
+ end
98
+
99
+ def create_client_from_config
100
+ print 'Enter your GitHub username: '
101
+ username = STDIN.gets.chomp
102
+ print "Enter GitHub password for #{username}: "
103
+ password = STDIN.noecho(&:gets).chomp
104
+ Octokit::Client.new login: username, password: password
105
+ end
106
+ end
107
+
108
+ end
@@ -0,0 +1,38 @@
1
+ require 'one_factorization'
2
+ require 'json'
3
+
4
+ class GeneratePairs
5
+ FILE_EXTENSION = 'pairs'
6
+
7
+ attr_reader :source
8
+
9
+ def self.load_names(file_path)
10
+ File.readlines(file_path).map(&:strip)
11
+ end
12
+
13
+ def initialize(options = {})
14
+ @source = options[:source]
15
+ end
16
+
17
+ def run
18
+ names = GeneratePairs.load_names(source)
19
+ File.open(path, 'w') do |file|
20
+ puts "Pair assignments created in file #{path}"
21
+ file.write names.one_factorize.shuffle.to_json
22
+ end
23
+ end
24
+
25
+ def path
26
+ File.join(source_directory, filename)
27
+ end
28
+
29
+ private
30
+
31
+ def source_directory
32
+ File.dirname(source)
33
+ end
34
+
35
+ def filename
36
+ "#{File.basename(source)}.#{FILE_EXTENSION}"
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ class HubClient
5
+ include HTTParty
6
+
7
+ attr_reader :auth_token
8
+ private :auth_token
9
+
10
+ HUB_URL = "https://hub.makersacademy.com"
11
+
12
+ format :json
13
+
14
+ def initialize(auth_token: nil)
15
+ @auth_token = auth_token || ENV['HUB_AUTH_TOKEN']
16
+ end
17
+
18
+ def post(path:, params:, base_uri: nil)
19
+ query = params.merge({"auth_token" => auth_token})
20
+ headers = {'Content-Type' => 'application/json' }
21
+ self.class.post(uri(base_uri, path), query: query, headers: headers)
22
+ end
23
+
24
+ private
25
+
26
+ def uri(base_uri, path)
27
+ if base_uri.nil? || base_uri.empty?
28
+ base_uri = HUB_URL
29
+ end
30
+ base_uri + path
31
+ end
32
+
33
+ end
@@ -0,0 +1,58 @@
1
+ require_relative 'hub_client'
2
+ require_relative 'command_line/interface'
3
+
4
+ module MakersToolbelt
5
+ class RandomizeBytes
6
+
7
+ attr_reader :client, :interface
8
+ private :client, :interface
9
+
10
+ RANDOMIZE_BYTES_PATH = -> (cohort_id) do
11
+ "/api/v1/cohorts/#{cohort_id}/randomize_bytes"
12
+ end
13
+
14
+ def initialize(client: nil, interface: nil)
15
+ @interface = interface || CommandLine::Interface
16
+ @client = client || HubClient.new
17
+ end
18
+
19
+ def run
20
+ @options = interface.ask_questions(:randomize_bytes)
21
+ response = client.post(**options)
22
+ output(response)
23
+ end
24
+
25
+ private
26
+
27
+ def output(response)
28
+ return successful_output(response) if response.success?
29
+ unsuccessful_output(response)
30
+ end
31
+
32
+ def successful_output(response)
33
+ bytes = JSON.parse(response.body)
34
+ output = prettify(bytes)
35
+ print output
36
+ end
37
+
38
+ def prettify(bytes)
39
+ bytes.each_with_index.inject("") do |aggregate, (byte, byte_number)|
40
+ string = "#{aggregate}Byte #{byte_number}\n======\n\n"
41
+ byte.each{|student| string << "#{student["name"]}\n"}
42
+ string << "\n"
43
+ end
44
+ end
45
+
46
+ def unsuccessful_output(response)
47
+ print "\n#{response.code_type} #{response.code} #{response.msg}\n"
48
+ end
49
+
50
+ def options
51
+ {
52
+ path: RANDOMIZE_BYTES_PATH.call(@options[:cohort_id]),
53
+ params: {number_of_bytes: @options[:number_of_bytes]},
54
+ base_uri: @options[:base_uri]
55
+ }
56
+ end
57
+ end
58
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module MakersToolBelt
2
+ VERSION = '0.2.1'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makers_toolbelt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Forrest
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-07-25 00:00:00.000000000 Z
13
+ date: 2017-10-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: octokit
@@ -147,6 +147,17 @@ extensions: []
147
147
  extra_rdoc_files: []
148
148
  files:
149
149
  - bin/makers
150
+ - command_map.yml
151
+ - lib/makers_toolbelt.rb
152
+ - lib/makers_toolbelt/command_line/interface.rb
153
+ - lib/makers_toolbelt/command_line/questions/get_positive_number.rb
154
+ - lib/makers_toolbelt/command_line/questions/get_uri.rb
155
+ - lib/makers_toolbelt/command_line/questions/question.rb
156
+ - lib/makers_toolbelt/fetch_pull_requests.rb
157
+ - lib/makers_toolbelt/generate_pairs.rb
158
+ - lib/makers_toolbelt/hub_client.rb
159
+ - lib/makers_toolbelt/randomize_bytes.rb
160
+ - lib/version.rb
150
161
  homepage: http://www.makersacademy.com
151
162
  licenses:
152
163
  - MIT