class EpitechApi::PromoManager

Public Class Methods

new(token) click to toggle source
# File lib/epitech_api/Managers/promo_manager.rb, line 6
def initialize(token)
  @token = token
end

Public Instance Methods

extract_students(promo) click to toggle source

@param [Promo] promo Promotion to extract students information @return Array<BasicUser>

# 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) click to toggle source

@param [string] name Promo name @param [string] location location (ex FR/LIL) @param [number] year year of selected promotion

# 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) click to toggle source

@param [string] location base location for the promotion @param [number] year of activity of this promotion @return List of all promotions matching criteria

# 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

Private Instance Methods

convert_users(users) click to toggle source
# File lib/epitech_api/Managers/promo_manager.rb, line 70
def convert_users(users)
  students = []
  users.each do |u|
    students.push BasicStudent.new(u['login'], u['prenom'], u['nom'], u['picture'], u['location'])
  end
  students
end