Class: Meibo::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/meibo/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zipfile:, profile:) ⇒ Reader

Returns a new instance of Reader.



22
23
24
25
# File 'lib/meibo/reader.rb', line 22

def initialize(zipfile:, profile:)
  @profile = profile
  @zipfile = zipfile
end

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



20
21
22
# File 'lib/meibo/reader.rb', line 20

def profile
  @profile
end

#zipfileObject (readonly)

Returns the value of attribute zipfile.



20
21
22
# File 'lib/meibo/reader.rb', line 20

def zipfile
  @zipfile
end

Class Method Details

.open(file_path, profile: Meibo.current_profile) ⇒ Object



8
9
10
11
12
# File 'lib/meibo/reader.rb', line 8

def self.open(file_path, profile: Meibo.current_profile)
  Zip::File.open(file_path) do |zipfile|
    yield new(zipfile: zipfile, profile: profile)
  end
end

.open_buffer(io, profile: Meibo.current_profile) ⇒ Object



14
15
16
17
18
# File 'lib/meibo/reader.rb', line 14

def self.open_buffer(io, profile: Meibo.current_profile)
  Zip::File.open_buffer(io) do |zipfile|
    yield new(zipfile: zipfile, profile: profile)
  end
end

Instance Method Details

#academic_sessionsObject



27
28
29
# File 'lib/meibo/reader.rb', line 27

def academic_sessions
  each_academic_session.to_a
end

#classesObject



31
32
33
# File 'lib/meibo/reader.rb', line 31

def classes
  each_class.to_a
end

#coursesObject



35
36
37
# File 'lib/meibo/reader.rb', line 35

def courses
  each_course.to_a
end

#demographicsObject



39
40
41
# File 'lib/meibo/reader.rb', line 39

def demographics
  each_demographic.to_a
end

#each_academic_session(&block) ⇒ Object



63
64
65
# File 'lib/meibo/reader.rb', line 63

def each_academic_session(&block)
  read_data(:file_academic_sessions, &block)
end

#each_class(&block) ⇒ Object



67
68
69
# File 'lib/meibo/reader.rb', line 67

def each_class(&block)
  read_data(:file_classes, &block)
end

#each_course(&block) ⇒ Object



71
72
73
# File 'lib/meibo/reader.rb', line 71

def each_course(&block)
  read_data(:file_courses, &block)
end

#each_demographic(&block) ⇒ Object



75
76
77
# File 'lib/meibo/reader.rb', line 75

def each_demographic(&block)
  read_data(:file_demographics, &block)
end

#each_enrollment(&block) ⇒ Object



79
80
81
# File 'lib/meibo/reader.rb', line 79

def each_enrollment(&block)
  read_data(:file_enrollments, &block)
end

#each_organization(&block) ⇒ Object



83
84
85
# File 'lib/meibo/reader.rb', line 83

def each_organization(&block)
  read_data(:file_orgs, &block)
end

#each_role(&block) ⇒ Object



87
88
89
# File 'lib/meibo/reader.rb', line 87

def each_role(&block)
  read_data(:file_roles, &block)
end

#each_user(&block) ⇒ Object



95
96
97
# File 'lib/meibo/reader.rb', line 95

def each_user(&block)
  read_data(:file_users, &block)
end

#each_user_profile(&block) ⇒ Object



91
92
93
# File 'lib/meibo/reader.rb', line 91

def (&block)
  read_data(:file_user_profiles, &block)
end

#enrollmentsObject



43
44
45
# File 'lib/meibo/reader.rb', line 43

def enrollments
  each_enrollment.to_a
end

#file_entry?(filename) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/meibo/reader.rb', line 126

def file_entry?(filename)
  @zipfile.get_entry(filename).file?
rescue Errno::ENOENT
  false
end

#load_bulk_filesObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/meibo/reader.rb', line 109

def load_bulk_files
  bulk_file_attributes = manifest.file_attributes(processing_mode: Meibo::Manifest::PROCESSING_MODES[:bulk])
  {
    file_academic_sessions: :academic_sessions,
    file_classes: :classes,
    file_courses: :courses,
    file_demographics: :demographics,
    file_enrollments: :enrollments,
    file_orgs: :organizations,
    file_roles: :roles,
    file_users: :users,
    file_user_profiles: :user_profiles
  }.filter_map do |file_attribute, data_method|
    [data_method, public_send(data_method).to_a] if bulk_file_attributes.include?(file_attribute)
  end.to_h
end

#manifestObject



99
100
101
102
103
104
105
106
107
# File 'lib/meibo/reader.rb', line 99

def manifest
  @manifest ||= begin
    filename = Meibo::Manifest.filename
    raise CsvFileNotFoundError, "#{filename} not found" unless file_entry?(filename)

    csv = @zipfile.read(filename)
    Meibo::Manifest.parse(csv)
  end
end

#organizationsObject



47
48
49
# File 'lib/meibo/reader.rb', line 47

def organizations
  each_organization.to_a
end

#rolesObject



51
52
53
# File 'lib/meibo/reader.rb', line 51

def roles
  each_role.to_a
end

#user_profilesObject



59
60
61
# File 'lib/meibo/reader.rb', line 59

def user_profiles
  .to_a
end

#usersObject



55
56
57
# File 'lib/meibo/reader.rb', line 55

def users
  each_user.to_a
end