class Osm::Myscout::Template

Constants

SORT_BY
TEMPLATES
VALID_TEMPLATE_IDS

Public Class Methods

get_template(api, section, id, options={}) click to toggle source

Get a template @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get login history for @param [String] id The ID of the template to get @!macro options_get @return [String, nil]

# File lib/osm/myscout.rb, line 243
def self.get_template(api, section, id, options={})
  fail ArgumentError, "Invalid template ID: #{id.inspect}" unless VALID_TEMPLATE_IDS.include?(id)

  section_id = section.to_i
  require_ability_to(api, :read, :user, section, options)
  cache_key = ['myscout', 'template', section_id, id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("ext/settings/parents/?action=getTemplate&key=#{id}&section_id=#{section_id}")
  content = data.is_a?(Hash) ? data['data'] : ''
  return nil if content.empty?

  cache_write(api, cache_key, content)
  return content
end
restore_template(api, section, id) click to toggle source

Restore a template to OSM's default for it @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get login history for @param [String] id The ID of the template to get @param [String] content The new content of the template @return [String, nil] The content of the template (nil if not restored)

# File lib/osm/myscout.rb, line 305
def self.restore_template(api, section, id)
  fail ArgumentError, "Invalid template ID: #{id.inspect}" unless VALID_TEMPLATE_IDS.include?(id)

  section_id = section.to_i
  require_ability_to(api, :write, :user, section)

  data = api.perform_query('ext/settings/parents/?action=restoreTemplate', {
    'section_id' => section_id,
    'key' =>        id,
  })

  if data.is_a?(Hash) && data['status']
    content = data['data']
    cache_key = ['myscout', 'template', section_id, id]
    cache_write(api, cache_key, content)
    return content
  end

  return nil
end
update_template(api, section, id, content) click to toggle source

Update a template in OSM @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get login history for @param [String] id The ID of the template to get @param [String] content The new content of the template @return [Boolean] Wheter OSM reported the template as updated

# File lib/osm/myscout.rb, line 268
def self.update_template(api, section, id, content)
  fail ArgumentError, "Invalid template ID: #{id.inspect}" unless VALID_TEMPLATE_IDS.include?(id)

  section_id = section.to_i
  require_ability_to(api, :write, :user, section)

  # Make sure required tags are present
  tags = Osm::Myscout::Template::TEMPLATES.find{ |t| t[:id].eql?(id) }[:tags]
  fail Osm::Error, "Couldn't find tags for template" if tags.nil?
  tags.select{ |tag| tag[:required] }.each do |tag|
    unless content.include?("[#{tag[:id]}]")
      message = "Required tag [#{tag[:id]}] not found in template content."
      fail ArgumentError, message
    end
  end

  data = api.perform_query('ext/settings/parents/?action=updateTemplate', {
    'section_id' => section_id,
    'key' =>        id,
    'value' =>      content
  })

  if data.is_a?(Hash) && data['status'] && data['data']
    cache_key = ['myscout', 'template', section_id, id]
    cache_write(api, cache_key, content)
    return true
  end

  return false
end