ft_42 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ft_42.rb +175 -20
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1de92e1b2395f0aca367f9a9824ff379b22d5168
4
- data.tar.gz: adbaba8cf515a66415e99fc90766991922a3e4ba
3
+ metadata.gz: 1045636a9b4a0cbd599f49f692e94e4f01df6c6f
4
+ data.tar.gz: cd2bff5d6d35f831c306afc0cfa22c67e908e12c
5
5
  SHA512:
6
- metadata.gz: 315726ad581ebbfd9fc6fbc16ad097910b332c6221d9d7eabb61fb41b2d47734b1a6df643c19222a25872f2706e4346707b9d786c3be4d059465a7215cd88650
7
- data.tar.gz: 6cbdac9a0515aea813b3509707703dac8737693adcb19419679524697ed72a1c3933a16a7b54a95ab63d91a2246027c52c50edc0629f6a8686f313ef386aed57
6
+ metadata.gz: d956c086a08bbcfbe1106b6d47ad4b95adaa605d996ea67bc0ab11090b2ccb32c0b711659feb33726bfa067fe2880d9828ce148116caa2453e0313c8779cddae
7
+ data.tar.gz: c5d4b8db1e543f0ab9be74cdc5256a632e2c2ea4f2659773d56d19dc8e7ea53e83b2ca4614e14fe82d0a2c10d0776165dc62de971ffe73c2294bfd0802476dc8
data/lib/ft_42.rb CHANGED
@@ -3,6 +3,7 @@ require "action_view"
3
3
  require "pastel"
4
4
  require "oauth2"
5
5
  require "ruby-progressbar"
6
+ require "pp"
6
7
 
7
8
  module Constants
8
9
  URL_42 = "https://api.intra.42.fr"
@@ -16,49 +17,93 @@ end
16
17
  class FT_42
17
18
  def initialize(*args)
18
19
  if (args.size > 2)
19
- puts "This could take a while..."
20
- ft_42 = Client.new(args.first, args.third)
20
+ if (args.first == "project")
21
+ ft_42 = Client.new(args.second, args.last)
22
+ else
23
+ ft_42 = Client.new(args.first, args.third)
24
+ end
21
25
  else
22
26
  ft_42 = Client.new(args.first)
23
27
  end
24
- user = User.new(ft_42.user)
25
- user_sessions = UserSessions.new(ft_42.user_sessions)
26
- user_print = UserPrinter.new(user)
27
- user_sessions_print = UserSessionsPrinter.new(user_sessions)
28
- if args.size == 1
29
- user_print.all
30
- user_sessions_print.all
31
- elsif args.second == "sessions"
32
- user_sessions_print.sessions
28
+ if (args.first == "project")
29
+ project = Project.new(ft_42.project)
30
+ project_users = ProjectUsers.new(ft_42.project_users)
31
+ project_print = ProjectPrinter.new(project)
32
+ project_users_print = ProjectUsersPrinter.new(project_users)
33
+ project_print.all
34
+ project_users_print.all
33
35
  else
34
- puts"Wrong arguments. Usage ft_42 [USER_LOGIN] [OPTIONAL CMD]"
36
+ user = User.new(ft_42.user)
37
+ user_sessions = UserSessions.new(ft_42.user_sessions)
38
+ user_print = UserPrinter.new(user)
39
+ user_sessions_print = UserSessionsPrinter.new(user_sessions)
40
+ if args.size == 1
41
+ user_print.all
42
+ user_sessions_print.all
43
+ elsif args.second == "sessions"
44
+ user_sessions_print.sessions
45
+ else
46
+ puts"Wrong arguments. Usage ft_42 [USER_LOGIN] [OPTIONAL CMD]"
47
+ end
35
48
  end
36
49
  end
37
50
  end
38
51
 
39
52
 
40
53
  class Client
41
- attr_reader :username, :token, :weeks_ago
54
+ attr_reader :input_1, :input_2, :token
42
55
 
43
- def initialize(username, weeks_ago = nil)
44
- @username = username
45
- @weeks_ago = weeks_ago
56
+ def initialize(input_1, input_2 = nil)
57
+ @input_1 = input_1
58
+ @input_2 = input_2
46
59
  @token = Token.new.token
47
60
  end
48
61
 
49
62
  def user
50
- token.get("/v2/users/#{username}", params: { per_page: 100 }).parsed
63
+ token.get("/v2/users/#{input_1}", params: { per_page: 100 }).parsed
51
64
  end
52
65
 
53
66
  def user_sessions
54
- token.get("/v2/users/#{username}/locations?range[end_at]=#{time_ago},#{right_now}", params: { per_page: 100 }).parsed
67
+ token.get("/v2/users/#{input_1}/locations?range[end_at]=#{time_ago},#{right_now}", params: { per_page: 100 }).parsed
68
+ end
69
+
70
+ def project
71
+ token.get("/v2/projects/#{input_1}").parsed
72
+ end
73
+
74
+ def campus
75
+ token.get("/v2/campus/#{input_2}").parsed
76
+ end
77
+
78
+ def project_users
79
+ user_projects = []
80
+ i = 1
81
+ loop do
82
+ begin
83
+ tries ||= 3
84
+ response = token.get("/v2/projects_users?filter[campus]=#{campus['id']}&filter[project_id]=#{project['id']}", params: { page: i, per_page: 100 }).parsed
85
+ rescue
86
+ puts "Something went wrong..."
87
+ puts "REFRESHING API TOKEN... wait 8 sec"
88
+ sleep 8
89
+ client = OAuth2::Client.new(ENV.fetch("UID42"), ENV.fetch("SECRET42"), site: ENV.fetch("API42"))
90
+ token = client.client_credentials.get_token
91
+ puts "Retrying request..."
92
+ retry unless (tries -= 1).zero?
93
+ else
94
+ break if response.empty?
95
+ user_projects << response
96
+ i += 1
97
+ end
98
+ end
99
+ user_projects
55
100
  end
56
101
 
57
102
  private
58
103
 
59
104
  def time_ago
60
- if weeks_ago
61
- time = Time.current - (weeks_ago.to_i * 7).days
105
+ if input_2
106
+ time = Time.current - (input_2.to_i * 7).days
62
107
  return time.to_s.split(" ")[0...-1].join("T")
63
108
  else
64
109
  return Time.current.beginning_of_week.to_s.split(" ")[0...-1].join("T")
@@ -83,6 +128,69 @@ class Token
83
128
  end
84
129
 
85
130
 
131
+ class Project
132
+ attr_reader :project
133
+
134
+ def initialize(project_response)
135
+ @project = project_response
136
+ end
137
+
138
+ def id
139
+ project["id"]
140
+ end
141
+
142
+ def name
143
+ project["name"]
144
+ end
145
+
146
+ def slug
147
+ project["slug"]
148
+ end
149
+
150
+ def tier
151
+ project["tier"]
152
+ end
153
+
154
+ def exam
155
+ project["exam"]
156
+ end
157
+ end
158
+
159
+
160
+ class Campus
161
+ attr_reader :campus
162
+
163
+ def initialize(campus_response)
164
+ @campus = campus_response
165
+ end
166
+
167
+ def id
168
+ campus["id"]
169
+ end
170
+
171
+ def name
172
+ campus["name"]
173
+ end
174
+
175
+ def student_count
176
+ campus["users_count"]
177
+ end
178
+ end
179
+
180
+
181
+ class ProjectUsers
182
+ attr_reader :project_users
183
+
184
+ def initialize(project_users_response)
185
+ @project_users = project_users_response
186
+ @project_users.flatten!
187
+ end
188
+
189
+ def logins
190
+ logins = project_users.map { |user_project| user_project["user"]["login"] }
191
+ end
192
+ end
193
+
86
194
 
87
195
  class User
88
196
  attr_reader :user
@@ -201,6 +309,53 @@ class UserSessions
201
309
  end
202
310
 
203
311
 
312
+ class ProjectPrinter
313
+ attr_reader :pastel, :project
314
+
315
+ def initialize(project)
316
+ @pastel = Pastel.new
317
+ @project = project
318
+ end
319
+
320
+ def all
321
+ name
322
+ tier
323
+ end
324
+
325
+ def name
326
+ puts highlight(project.name)
327
+ end
328
+
329
+ def tier
330
+ puts "Tier: #{project.tier}"
331
+ end
332
+
333
+ private
334
+
335
+ def highlight(string)
336
+ pastel.bright_green.bold(string)
337
+ end
338
+ end
339
+
340
+ class ProjectUsersPrinter
341
+ attr_reader :pastel, :project_users
342
+
343
+ def initialize(project_users)
344
+ @pastel = Pastel.new
345
+ @project_users = project_users
346
+ end
347
+
348
+ def all
349
+ usernames
350
+ end
351
+
352
+ def usernames
353
+ project_users.logins.each_with_index do |login, i|
354
+ puts "#{i + 1}. #{login}"
355
+ end
356
+ end
357
+ end
358
+
204
359
 
205
360
  class UserPrinter
206
361
  attr_reader :pastel, :user
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ft_42
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matias Fernandez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-15 00:00:00.000000000 Z
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack