Class: Hubspot::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/utils.rb

Class Method Summary collapse

Class Method Details

.compare_property_lists(klass, source, target) ⇒ Object



56
57
58
59
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
# File 'lib/hubspot/utils.rb', line 56

def compare_property_lists(klass, source, target)
  skip         = [] # Array of skipped properties and the reason
  new_groups   = Set.new # Array of groups to create
  new_props    = [] # Array of properties to add
  update_props = [] # Array of properties to update
  src_groups   = source['groups']
  dst_groups   = target['groups']
  src_props    = source['properties']
  dst_props    = target['properties']

  src_props.each do |src|
    group = find_by_name(src['groupName'], src_groups)
    if src['createdUserId'].blank? && src['updatedUserId'].blank? then
      skip << { prop: src, reason: 'Not user created' }
    else
      dst = find_by_name(src['name'], dst_props)
      if dst
        if dst['readOnlyDefinition']
          skip << { prop: src, reason: 'Definition is read-only' }
        elsif klass.same?(src, dst)
          skip << { prop: src, reason: 'No change' }
        else
          new_groups << group unless group.blank? || find_by_name(group['name'], dst_groups)
          update_props << src
        end
      else
        new_groups << group unless group.blank? || find_by_name(group['name'], dst_groups)
        new_props << src
      end
    end
  end
  [skip, new_groups.to_a, new_props, update_props]
end

.create_groups(klass, groups, dry_run = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/hubspot/utils.rb', line 23

def create_groups(klass, groups, dry_run=false)
  puts '','Creating new groups'
  groups.each do |g|
    if dry_run || klass.create_group!(g)
      puts "Created: #{g['name']}"
    else
      puts "Failed: #{g['name']}"
    end
  end
end

.create_properties(klass, props, dry_run = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/hubspot/utils.rb', line 34

def create_properties(klass, props, dry_run=false)
  puts '','Creating new properties'
  props.each do |p|
    if dry_run || klass.create!(p)
      puts "Created: #{p['groupName']}:#{p['name']}"
    else
      puts "Failed: #{p['groupName']}:#{p['name']}"
    end
  end
end

.hash_to_properties(hash, opts = {}) ⇒ Object

Turns a hash into the hubspot properties format



18
19
20
21
# File 'lib/hubspot/utils.rb', line 18

def hash_to_properties(hash, opts = {})
  key_name = opts[:key_name] || "property"
  hash.map { |k, v| { key_name => k.to_s, "value" => v } }
end

.properties_array_to_hash(props) ⇒ Object

Converts an array of property objects into a hash with the property name as the key



13
14
15
# File 'lib/hubspot/utils.rb', line 13

def properties_array_to_hash(props)
  props.inject({}) { |h, x| h[x["name"]] = x; h }
end

.properties_to_hash(props) ⇒ Object

Parses the hubspot properties format into a key-value hash



5
6
7
8
9
# File 'lib/hubspot/utils.rb', line 5

def properties_to_hash(props)
  newprops = HashWithIndifferentAccess.new
  props.each { |k, v| newprops[k] = v["value"] }
  newprops
end

.update_properties(klass, props, dry_run = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/hubspot/utils.rb', line 45

def update_properties(klass, props, dry_run=false)
  puts '','Updating existing properties'
  props.each do |p|
    if dry_run || klass.update!(p['name'], p)
      puts "Updated: #{p['groupName']}:#{p['name']}"
    else
      puts "Failed: #{p['groupName']}:#{p['name']}"
    end
  end
end