Class: Hubspot::Utils
- Inherits:
-
Object
- Object
- Hubspot::Utils
- Defined in:
- lib/hubspot/utils.rb
Class Method Summary collapse
- .compare_property_lists(klass, source, target) ⇒ Object
- .create_groups(klass, groups, dry_run = false) ⇒ Object
- .create_properties(klass, props, dry_run = false) ⇒ Object
-
.hash_to_properties(hash, opts = {}) ⇒ Object
Turns a hash into the hubspot properties format.
-
.properties_array_to_hash(props) ⇒ Object
Converts an array of property objects into a hash with the property name as the key.
-
.properties_to_hash(props) ⇒ Object
Parses the hubspot properties format into a key-value hash.
- .update_properties(klass, props, dry_run = false) ⇒ Object
Class Method Details
.compare_property_lists(klass, source, target) ⇒ Object
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 89 90 |
# File 'lib/hubspot/utils.rb', line 58 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
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hubspot/utils.rb', line 25 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
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hubspot/utils.rb', line 36 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
20 21 22 23 |
# File 'lib/hubspot/utils.rb', line 20 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
15 16 17 |
# File 'lib/hubspot/utils.rb', line 15 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
7 8 9 10 11 |
# File 'lib/hubspot/utils.rb', line 7 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
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hubspot/utils.rb', line 47 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 |