Sha256: 2e62c93c772228c7fdba8bc0b5ccc333ce01fbadb642f94d1d134033c1ef0c88

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'fileutils'
require_relative 'provider_factory'

class DataFileGenerator

  def initialize(config)
    @config = config
    @provider_factory = ProviderFactory.new
  end

  def generate
    provider = @provider_factory.create @config[:provider]
    requests = provider.get_requests @config.users_with_password, threads: @config[:threads]
    teams = @config[:teams].map do |team|
      {
          title: title(team),
          requests: team_requests(team, requests)
      }
    end
    requests.each { |r| replace_username_with_name r, :submitted_by, :assigned_to }
    FileUtils.mkdir_p output_dir
    teams.each { |t| create_data_file t }
  end

  private

  def title(team)
    team[:title] || "#{team[:name]} Help Desk Dashboard"
  end

  def team_requests(team, requests)
    members = team[:members].map { |m| username m }
    requests.select { |r| members.include? username(r[:submitted_by]) }
  end

  def replace_username_with_name(request, *keys)
    keys.each do |key|
      username = username(request[key])
      user = get_user username
      request[key] = user[:name] if user
    end
  end

  def get_user(username)
    users = users_by_username[username]
    users[0] if users
  end

  def users_by_username
    @users_by_username ||= @config[:users].group_by { |u| username u[:username] }
  end

  def username(username)
    username.downcase
  end

  def create_data_file(team)
    File.write data_file_path(team), team.to_json
  end

  def data_file_path(team)
    file_name = "#{team[:team_name]}.json".downcase.gsub(' ', '-')
    File.join output_dir, file_name
  end

  def output_dir
    File.join @config[:output_dir], 'data'
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
help_desk_dashboard-0.0.7 lib/help_desk_dashboard/lib/data_file_generator.rb