Class: EpitechApi::PromoManager

Inherits:
Object
  • Object
show all
Defined in:
lib/epitech_api/Managers/promo_manager.rb

Instance Method Summary collapse

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

Parameters:

  • promo (Promo)

    Promotion to extract students information



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

Parameters:

  • name (string)

    Promo name

  • location (string)

    location (ex FR/LIL)

  • year (number)

    year of selected promotion

Raises:



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.

Parameters:

  • location (string)

    base location for the promotion

  • year (number)

    of activity of this promotion

Returns:

  • List of all promotions matching criteria

Raises:



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