Class: GTM::Pull

Inherits:
Object
  • Object
show all
Defined in:
lib/gtm/pull.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Pull

Returns a new instance of Pull.



12
13
14
# File 'lib/gtm/pull.rb', line 12

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/gtm/pull.rb', line 10

def client
  @client
end

Instance Method Details

#account_idsObject



23
24
25
26
27
28
29
# File 'lib/gtm/pull.rb', line 23

def 
  if @client.scope[:account_id]
    [@client.scope[:account_id]]
  else
    
  end
end

#get_account_idsObject



31
32
33
34
35
36
37
# File 'lib/gtm/pull.rb', line 31

def 
  @client.run do |accounts|
     = accounts.map { |a| a['accountId'] }
    return 
  end
  []
end

#get_container(account_id) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/gtm/pull.rb', line 39

def get_container()
  result = @client.client.execute(
      api_method: @client.gtm.accounts.containers.list,
      parameters: {
          accountId: 
      }
  )
  result.data.containers
end

#get_tags(account_id, container_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/gtm/pull.rb', line 49

def get_tags(, container_id)
  result = @client.client.execute(
      api_method: @client.gtm.accounts.containers.tags.list,
      parameters: {
          accountId: ,
          containerId: container_id
      }
  )
  result.data.tags
end

#pull_tagsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gtm/pull.rb', line 60

def pull_tags
  container_scope = @client.scope[:container_name]
  @client.run do |accounts|
     = accounts.map { |a| a['accountId'] }
    if .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

#runObject



16
17
18
19
20
21
# File 'lib/gtm/pull.rb', line 16

def run
  .each do ||
    @client.scope[:account_id] = 
    pull_tags
  end
end