Class: EpitechApi::PromoManager
- Inherits:
-
Object
- Object
- EpitechApi::PromoManager
- Defined in:
- lib/epitech_api/Managers/promo_manager.rb
Instance Method Summary collapse
- #extract_students(promo) ⇒ Object
- #get(name, location, year) ⇒ Object
-
#initialize(token) ⇒ PromoManager
constructor
A new instance of PromoManager.
-
#list(location, year) ⇒ Object
List of all promotions matching criteria.
Constructor Details
#initialize(token) ⇒ PromoManager
Returns a new instance of PromoManager.
6 7 8 |
# File 'lib/epitech_api/Managers/promo_manager.rb', line 6 def initialize(token) @token = token end |
Instance Method Details
#extract_students(promo) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/epitech_api/Managers/promo_manager.rb', line 46 def extract_students(promo) students = [] idx = 0 while idx < promo.size uri = URI("https://intra.epitech.eu/user/filter/user?format=json&location=#{promo.location}&year=#{promo.year}&active=true&promo=#{promo.name}&offset=#{idx}") req = Net::HTTP::Get.new uri req['Cookie'] = "#{@token}" http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true response = http.request req raise InvalidRights unless response.code.to_i == 200 response_body = JSON.parse response.body students.push *convert_users(response_body['items']) idx += response_body['items'].size end students end |
#get(name, location, year) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/epitech_api/Managers/promo_manager.rb', line 36 def get(name, location, year) promos = list(location, year) promos.each do |promo| return promo if promo.name == name && promo.location == location && promo.year == year end raise ResourceNotFound end |
#list(location, year) ⇒ Object
Returns List of all promotions matching criteria.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/epitech_api/Managers/promo_manager.rb', line 13 def list(location, year) uri = URI("https://intra.epitech.eu/user/filter/promo?format=json&location=#{location}&year=#{year}&active=true") req = Net::HTTP::Get.new uri req['Cookie'] = "#{@token}" http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true response = http.request req raise InvalidRights unless response.code.to_i == 200 response_body = JSON.parse response.body promos = [] response_body.each do |p| promos.push Promo.new(p['promo'], p['students'].to_i, year, location) end promos end |