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
- .dump_properties(klass, hapikey = , filter = {}) ⇒ Object
-
.hash_to_properties(hash, opts = {}) ⇒ Object
Turns a hash into the hubspot properties format.
-
.properties_to_hash(props) ⇒ Object
Parses the hubspot properties format into a key-value hash.
- .restore_properties(klass, hapikey = , properties = {}, dry_run = false) ⇒ Object
- .update_properties(klass, props, dry_run = false) ⇒ Object
- .with_hapikey(hapikey) ⇒ Object
Class Method Details
.compare_property_lists(klass, source, target) ⇒ Object
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 103 104 105 106 107 |
# File 'lib/hubspot/utils.rb', line 75 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
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/hubspot/utils.rb', line 42 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
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hubspot/utils.rb', line 53 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 |
.dump_properties(klass, hapikey = , filter = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/hubspot/utils.rb', line 17 def dump_properties(klass, hapikey=ENV['HUBSPOT_API_KEY'], filter={}) Hubspot::Deprecator.build.deprecation_warning("Hubspot::Utils.dump_properties") with_hapikey(hapikey) do { 'groups' => klass.groups({}, filter), 'properties' => klass.all({}, filter).select { |p| !p['hubspotDefined'] } } end end |
.hash_to_properties(hash, opts = {}) ⇒ Object
Turns a hash into the hubspot properties format
12 13 14 15 |
# File 'lib/hubspot/utils.rb', line 12 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_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 |
.restore_properties(klass, hapikey = , properties = {}, dry_run = false) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hubspot/utils.rb', line 27 def restore_properties(klass, hapikey=ENV['HUPSPOT_API_KEY'], properties={}, dry_run=false) Hubspot::Deprecator.build.deprecation_warning("Hubspot::Utils.restore_properties") existing_properties = dump_properties(klass, hapikey) skip, new_groups, new_props, update_props = compare_property_lists(klass, properties, existing_properties) puts '', 'Dry Run - Changes will not be applied' if dry_run puts '','Skipping' skip.each { |h| puts "#{h[:reason]} - #{h[:prop]['groupName']}:#{h[:prop]['name']}" } with_hapikey(hapikey) do create_groups(klass, new_groups, dry_run) create_properties(klass, new_props, dry_run) update_properties(klass, update_props, dry_run) end end |
.update_properties(klass, props, dry_run = false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hubspot/utils.rb', line 64 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 |
.with_hapikey(hapikey) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/hubspot/utils.rb', line 109 def with_hapikey(hapikey) begin Hubspot.configure(hapikey: hapikey) unless hapikey.blank? yield if block_given? ensure Hubspot.configure(hapikey: ENV['HUBSPOT_API_KEY']) unless hapikey.blank? end end |