class GTM::Pull

Attributes

client[RW]

Public Class Methods

new(client) click to toggle source
# File lib/gtm/pull.rb, line 12
def initialize(client)
  @client = client
end

Public Instance Methods

account_ids() click to toggle source
# File lib/gtm/pull.rb, line 23
def account_ids
  if @client.scope[:account_id]
    [@client.scope[:account_id]]
  else
    get_account_ids
  end
end
get_account_ids() click to toggle source
# File lib/gtm/pull.rb, line 31
def get_account_ids
  @client.run do |accounts|
    account_ids = accounts.map { |a| a['accountId'] }
    return account_ids
  end
  []
end
get_container(account_id) click to toggle source
# File lib/gtm/pull.rb, line 39
def get_container(account_id)
  result = @client.client.execute(
      api_method: @client.gtm.accounts.containers.list,
      parameters: {
          accountId: account_id
      }
  )
  result.data.containers
end
get_tags(account_id, container_id) click to toggle source
# File lib/gtm/pull.rb, line 49
def get_tags(account_id, container_id)
  result = @client.client.execute(
      api_method: @client.gtm.accounts.containers.tags.list,
      parameters: {
          accountId: account_id,
          containerId: container_id
      }
  )
  result.data.tags
end
pull_tags() click to toggle source
# File lib/gtm/pull.rb, line 60
def pull_tags
  container_scope = @client.scope[:container_name]
  @client.run do |accounts|
    account_ids = accounts.map { |a| a['accountId'] }
    if account_ids.include? @client.scope[:account_id]
      containers = get_container @client.scope[:account_id]
      containers.each do |container|
        container_name = container['name'].parameterize.underscore
        next if container_scope && !container_scope.include?(container_name)
        tags_path = "#{@client.scope[:path]}#{@client.scope[:account_id]}/#{container_name}/tags"
        meta_tags_path = "#{@client.scope[:path]}/.dump/meta/#{@client.scope[:account_id]}/#{container_name}/tags"
        FileUtils::mkdir_p tags_path
        FileUtils::mkdir_p meta_tags_path
        tags = get_tags @client.scope[:account_id], container['containerId']
        print "pulling tags from account #{container['accountId']} into #{tags_path} "
        tags.each do |tag|
          tag_name = tag['name'].parameterize.underscore
          if %w(ua html).include?(tag.type) # dumpable types
            suffix = ''
            if tag.type.eql?('ua')
              value = tag.parameter.to_json
              suffix = '.json'
            elsif tag.type.eql?('html')
              html_parameter = tag.parameter.select { |p| p.key.eql? 'html' }.first
              value = html_parameter.value
            end
            # EDITABLE CONTENT
            File.open("#{tags_path}/#{tag_name}.#{tag.type}#{suffix}", 'w') do |f|
              f.write value
            end
            # DUMP
            data = Marshal.dump(tag.to_hash)
            File.open("#{meta_tags_path}/#{tag_name}.#{tag.type}.dump", 'w') do |f|
              f.write data
            end
            print '.'
          end
        end
        puts '[OK]'
      end
    end
  end
end
run() click to toggle source
# File lib/gtm/pull.rb, line 16
def run
  account_ids.each do |account_id|
    @client.scope[:account_id] = account_id
    pull_tags
  end
end