Class: Hubscreen::Utils
- Inherits:
-
Object
- Object
- Hubscreen::Utils
- Defined in:
- lib/hubscreen/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
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 103 104 105 106 |
# File 'lib/hubscreen/utils.rb', line 74 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
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hubscreen/utils.rb', line 41 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
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hubscreen/utils.rb', line 52 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
20 21 22 23 24 25 26 |
# File 'lib/hubscreen/utils.rb', line 20 def dump_properties(klass, hapikey=ENV['HUBSPOT_API_KEY'], filter={}) 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
15 16 17 18 |
# File 'lib/hubscreen/utils.rb', line 15 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
8 9 10 11 12 |
# File 'lib/hubscreen/utils.rb', line 8 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
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hubscreen/utils.rb', line 28 def restore_properties(klass, hapikey=ENV['HUPSPOT_API_KEY'], properties={}, dry_run=false) 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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hubscreen/utils.rb', line 63 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
108 109 110 111 112 113 114 115 |
# File 'lib/hubscreen/utils.rb', line 108 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 |