Class: GTM::Pull
- Inherits:
-
Object
- Object
- GTM::Pull
- Defined in:
- lib/gtm/pull.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
- #account_ids ⇒ Object
- #get_account_ids ⇒ Object
- #get_container(account_id) ⇒ Object
- #get_tags(account_id, container_id) ⇒ Object
-
#initialize(client) ⇒ Pull
constructor
A new instance of Pull.
- #pull_tags ⇒ Object
- #run ⇒ Object
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
#client ⇒ Object
Returns the value of attribute client.
10 11 12 |
# File 'lib/gtm/pull.rb', line 10 def client @client end |
Instance Method Details
#account_ids ⇒ Object
23 24 25 26 27 28 29 |
# 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 ⇒ Object
31 32 33 34 35 36 37 |
# 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) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gtm/pull.rb', line 49 def (account_id, container_id) result = @client.client.execute( api_method: @client.gtm.accounts.containers..list, parameters: { accountId: account_id, containerId: container_id } ) result.data. end |
#pull_tags ⇒ Object
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 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) = "#{@client.scope[:path]}#{@client.scope[:account_id]}/#{container_name}/tags" = "#{@client.scope[:path]}/.dump/meta/#{@client.scope[:account_id]}/#{container_name}/tags" FileUtils::mkdir_p FileUtils::mkdir_p = @client.scope[:account_id], container['containerId'] print "pulling tags from account #{container['accountId']} into #{} " .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("#{}/#{tag_name}.#{tag.type}#{suffix}", 'w') do |f| f.write value end # DUMP data = Marshal.dump(tag.to_hash) File.open("#{}/#{tag_name}.#{tag.type}.dump", 'w') do |f| f.write data end print '.' end end puts '[OK]' end end end end |
#run ⇒ Object
16 17 18 19 20 21 |
# File 'lib/gtm/pull.rb', line 16 def run account_ids.each do |account_id| @client.scope[:account_id] = account_id end end |