module ZendeskAppsTools::Theming::Common

Public Instance Methods

assets() click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 19
def assets
  assets = Dir.glob(theme_package_path('assets', '*'))
  assets.each_with_object({}) do |asset, asset_payload|
    asset_payload[File.basename(asset)] = url_for(asset)
  end
end
assets_hash() click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 26
def assets_hash
  assets.each_with_object({}) do |(k,v), h|
    parametrized = k.gsub(/[^a-z0-9\-_]+/, '-')
    h["assets-#{parametrized}"] = v
  end
end
manifest() click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 33
def manifest
  full_manifest_path = theme_package_path('manifest.json')
  JSON.parse(File.read(full_manifest_path))
rescue Errno::ENOENT
  say_error_and_exit "There is no manifest file in #{full_manifest_path}"
rescue JSON::ParserError
  say_error_and_exit "The manifest file is invalid at #{full_manifest_path}"
end
metadata_hash() click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 48
def metadata_hash
  { 'api_version' => manifest['api_version'] }
end
recursive_pathname_split(relative_path) click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 60
def recursive_pathname_split(relative_path)
  split_path = relative_path.split
  joined_directories = split_path[0]
  return split_path if split_path[0] == joined_directories.split[0]
  [*recursive_pathname_split(joined_directories), split_path[1]]
end
relative_path_for(filename) click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 15
def relative_path_for(filename)
  Pathname.new(filename).relative_path_from(Pathname.new(File.expand_path(app_dir))).cleanpath
end
settings_hash() click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 42
def settings_hash
  manifest['settings'].flat_map { |setting_group| setting_group['variables'] }.each_with_object({}) do |variable, result|
    result[variable.fetch('identifier')] = value_for_setting(variable)
  end
end
theme_package_path(*file) click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 4
def theme_package_path(*file)
  File.expand_path(File.join(app_dir, *file))
end
url_for(package_file) click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 8
def url_for(package_file)
  relative_path = relative_path_for(package_file)
  path_parts = recursive_pathname_split(relative_path)
  path_parts.shift
  "http://localhost:4567/guide/#{path_parts.join('/')}"
end
value_for_setting(variable) click to toggle source
# File lib/zendesk_apps_tools/theming/common.rb, line 52
def value_for_setting(variable)
  return variable.fetch('value') unless variable.fetch('type') == 'file'

  files = Dir.glob(theme_package_path('settings', '*.*'))
  file = files.find { |f| File.basename(f, '.*') == variable.fetch('identifier') }
  url_for(file)
end