class Eco::API::UseCases::DefaultCases::ToCsvCase

Attributes

options[R]
people[R]
session[R]

Public Instance Methods

main(people, session, options, usecase) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 7
def main(people, session, options, usecase)
  options[:end_get] = false
  @session = session; @options = options; @people = people

  unless people && !people.empty?
    session.logger.warn("No source people to create the file... aborting!")
    return false
  end

  if options.dig(:export, :options, :split_schemas)
    by_schema.each do |id, people|
      sch_name = schemas.to_name(id)
      prefix = sch_name ? sch_name.gsub(" ", "_") : "No_Schema"
      create_file!("#{prefix}_#{file}", people)
    end
  else
    create_file!(file, people)
  end
end

Private Instance Methods

by_schema() click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 90
def by_schema
  people.group_by do |person|
    if details = person.details
      details.schema_id
    end
  end.transform_values do |persons|
    people.newFrom persons
  end
end
create_file!(filename = file, data = people) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 29
def create_file!(filename = file, data = people)
  session.logger.info("going to create file: #{filename}")

  CSV.open(filename, "w") do |csv|
    csv << spot_header(data.first)
    data.each do |person|
      csv << to_row(person)
    end
  end
end
deps() click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 77
def deps
  @deps ||= {"supervisor_id" => {people: people}}
end
file() click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 81
def file
  @file ||= (options[:file] || options.dig(:export, :file, :name)).tap do |filename|
    unless filename
      session.logger.error("Destination file not specified")
      return false
    end
  end
end
keys(entry) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 55
def keys(entry)
  entry.keys - ["freemium", "send_invites"]
end
nice_header_maps() click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 117
def nice_header_maps
  @nice_header_maps ||= {
    "policy_group_ids"   => "User Group(s)",
    "email"              => "Email",
    "name"               => "Name",
    "supervisor_id"      => "Manager ID",
    "filter_tags"        => "Locations",
    "default_tag"        => "Default Location",
    "id"                 => "ecoPortal ID",
    "external_id"        => "Reference ID (ext_id)",
    "login_provider_ids" => "Login Methods",
    "landing_page_id"    => "Landing Page ID"
  }
end
nice_header_names(header, schema: nil) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 63
def nice_header_names(header, schema: nil)
  schema ||= session.schema
  name_maps = schema.fields_by_alt_id.each_with_object({}) do |(alt_id, fld), mappings|
    mappings[alt_id] = fld.name
  end.merge(nice_header_maps)
  header.map {|name| name_maps[name] ? name_maps[name] : name}
end
nice_header_names?() click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 59
def nice_header_names?
   options.dig(:nice_header) || options.dig(:export, :options, :nice_header)
end
schema(value) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 100
def schema(value)
  case value
  when Ecoportal::API::V1::Person
    schema(value.details&.schema_id)
  when String
    schemas[value]
  when Ecoportal::API::V1::PersonDetails
    schema(value.schema_id)
  when Ecoportal::API::V1::PersonSchema
    value
  end
end
schemas() click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 113
def schemas
  session.schemas
end
spot_header(person = people.first) { |header| ... } click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 47
def spot_header(person = people.first)
  header = keys(to_entry_type(person))
  header << "Schema"
  header = yield(header) if block_given?
  header = nice_header_names(header, schema: schema(person.details)) if nice_header_names?
  header
end
to_entry_type(person) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 71
def to_entry_type(person)
  session.new_entry(person, dependencies: deps).yield_self do |person_entry|
    options.dig(:export, :options, :internal_names) ? person_entry.mapped_entry : person_entry.external_entry
  end
end
to_row(person) click to toggle source
# File lib/eco/api/usecases/default_cases/to_csv_case.rb, line 40
def to_row(person)
  entry = to_entry_type(person)
  entry.values_at(*keys(entry)).tap do |row|
    row << schemas.to_name(person.details&.schema_id) || "No Schema"
  end
end