Class: FetchStudents

Inherits:
KOSapiInteractor show all
Defined in:
app/interactors/fetch_students.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize

Class Attribute Details

.event_typesObject

Returns the value of attribute event_types


44
45
46
# File 'app/interactors/fetch_students.rb', line 44

def event_types
  @event_types
end

.resourceObject

Returns the value of attribute resource


44
45
46
# File 'app/interactors/fetch_students.rb', line 44

def resource
  @resource
end

.source_keyObject

Returns the value of attribute source_key


44
45
46
# File 'app/interactors/fetch_students.rb', line 44

def source_key
  @source_key
end

Class Method Details

.[](resource, source_key:, event_types:) ⇒ Object


46
47
48
49
50
51
52
# File 'app/interactors/fetch_students.rb', line 46

def [](resource, source_key:, event_types:)
  Class.new(self).tap do |cls|
    cls.resource = resource
    cls.source_key = source_key
    cls.event_types = event_types
  end
end

Instance Method Details

#event_typesObject


63
64
65
# File 'app/interactors/fetch_students.rb', line 63

def event_types
  self.class.event_types
end

#fetch_event_students(client, event) ⇒ Object


31
32
33
# File 'app/interactors/fetch_students.rb', line 31

def fetch_event_students(client, event)
  client.send(resource_name).find(event.source_id.to_i).limit(100).order_by('id').attendees
end

#load_events(faculty_semester, future_only) ⇒ Object


25
26
27
28
29
# File 'app/interactors/fetch_students.rb', line 25

def load_events(faculty_semester, future_only)
  query = Event.where(event_type: event_types, faculty: faculty_semester.faculty, semester: faculty_semester.code)
  query = query.where('starts_at > ?'.lit(Time.new)) if future_only
  query
end

#perform(faculty_semester:, future_events_only: true) ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'app/interactors/fetch_students.rb', line 11

def perform(faculty_semester:, future_events_only: true)
  client = kosapi_client(faculty_semester)

  @events = load_events(faculty_semester, future_events_only).map do |event|
    students = fetch_event_students(client, event)
    update_event_students(event, students)
    event
  end
end

#resource_nameObject


55
56
57
# File 'app/interactors/fetch_students.rb', line 55

def resource_name
  self.class.resource
end

#resultsObject


21
22
23
# File 'app/interactors/fetch_students.rb', line 21

def results
  { events: @events, people: @people.values }
end

#setup(*args) ⇒ Object


6
7
8
9
# File 'app/interactors/fetch_students.rb', line 6

def setup(*args)
  super
  @people = {}
end

#source_keyObject


59
60
61
# File 'app/interactors/fetch_students.rb', line 59

def source_key
  self.class.source_key
end

#update_event_students(event, students) ⇒ Object


35
36
37
38
39
40
41
# File 'app/interactors/fetch_students.rb', line 35

def update_event_students(event, students)
  student_ids = students.map do |student|
    @people[student.username] ||= Person.new(full_name: student.full_name) { |p| p.id = student.username }
    student.username
  end
  event.student_ids = student_ids
end