Sha256: 0d2161c1c399f159d9185e6bbe5a8af793617ed4a0fa7f0a6958c189c6b8e248

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

require 'hubspot-ruby'

namespace :hubspot do
  desc 'Dump properties to file'
  task :dump_properties, [:kind, :file, :hapikey, :include, :exclude] do |_, args|
    hapikey = args[:hapikey] || ENV['HUBSPOT_API_KEY']
    kind = args[:kind]
    unless %w(contact deal).include?(kind)
      raise ArgumentError, ':kind must be either "contact" or "deal"'
    end
    klass = kind == 'contact' ? Hubspot::ContactProperties : Hubspot::DealProperties
    props = Hubspot::Utils::dump_properties(klass, hapikey, build_filter(args))
    if args[:file].blank?
      puts JSON.pretty_generate(props)
    else
      File.open(args[:file], 'w') do |f|
        f.write(JSON.pretty_generate(props))
      end
    end
  end

  desc 'Restore properties from file'
  task :restore_properties, [:kind, :file, :hapikey, :dry_run] do |_, args|
    hapikey = args[:hapikey] || ENV['HUBSPOT_API_KEY']
    if args[:file].blank?
      raise ArgumentError, ':file is a required parameter'
    end
    kind = args[:kind]
    unless %w(contact deal).include?(kind)
      raise ArgumentError, ':kind must be either "contact" or "deal"'
    end
    klass = kind == 'contact' ? Hubspot::ContactProperties : Hubspot::DealProperties
    file = File.read(args[:file])
    props = JSON.parse(file)
    Hubspot::Utils.restore_properties(klass, hapikey, props, args[:dry_run] != 'false')
  end

  private

  def build_filter(args)
    { include: val_to_array(args[:include]),
      exclude: val_to_array(args[:exclude])
    }
  end

  def val_to_array(val)
    val.blank? ? val : val.split(/\W+/)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hubspot-ruby-0.6.0 lib/tasks/hubspot.rake
hubspot-ruby-0.5.0 lib/tasks/properties.rake
hubspot-ruby-0.4.0 lib/tasks/properties.rake
hubspot-ruby-0.3.0 lib/tasks/properties.rake
hubspot-ruby-0.2.1 lib/tasks/properties.rake