Sha256: 59842c8d059091d713782322e075d78f5a9a194dad46b07e5f4026b4a1e22ca5

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 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]
    requests.reject! { |r| @config[:ignore].include? r[:id].to_i  }
    teams = @config[:teams].map do |team|
      {
          name: team[:name],
          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[: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.8 lib/help_desk_dashboard/lib/data_file_generator.rb