class ChargebeeRails::SyncPlans

Attributes

messages[RW]

Public Class Methods

sync(create:true, update:true, delete:false) click to toggle source
# File lib/chargebee_rails/sync_plans.rb, line 5
def self.sync(create:true, update:true, delete:false)
        syncer = SyncPlans.new
        return syncer.do_sync(create:create, update:update, delete:delete)
end

Public Instance Methods

do_sync(create:true, update:true, delete:false) click to toggle source
# File lib/chargebee_rails/sync_plans.rb, line 10
  def do_sync(create:true, update:true, delete:false)
          self.get_plans
          self.sync_plans(create:create, update:update, delete:delete)

return messages
  end

Protected Instance Methods

cb_plans() click to toggle source
# File lib/chargebee_rails/sync_plans.rb, line 35
def cb_plans
  @cb_plans ||= []
end
create_new_plans() click to toggle source

Create new plans that are not present in app but are available in chargebee

# File lib/chargebee_rails/sync_plans.rb, line 60
def create_new_plans
  plan_ids = Plan.all.map(&:plan_id)
  cb_plans.reject { |cb_plan| plan_ids.include?(cb_plan.id) }
          .each   { |new_plan| output "Creating Plan - #{new_plan.id}"; Plan.create(plan_params(new_plan)) }
end
get_plans() click to toggle source
# File lib/chargebee_rails/sync_plans.rb, line 25
def get_plans
    loop do
      plan_list = retrieve_plan_list
      @offset = plan_list.next_offset
      cb_plans << plan_list.flat_map(&:plan)
      break unless @offset.present?
    end
    @cb_plans = cb_plans.flatten
end
output(message) click to toggle source
# File lib/chargebee_rails/sync_plans.rb, line 19
def output(message)
  puts(message)
  self.messages ||= []
  self.messages << message
end
plan_params(plan) click to toggle source

Build the plan params to be created or updated in the application

# File lib/chargebee_rails/sync_plans.rb, line 74
def plan_params plan
  {
    name: plan.name,
    plan_id: plan.id,
    status: plan.status,
    chargebee_data: {
      price: plan.price,
      period: plan.period,
      period_unit: plan.period_unit,
      trial_period: plan.trial_period,
      trial_period_unit: plan.trial_period_unit,
      charge_model: plan.charge_model,
      free_quantity: plan.free_quantity
    }
  }
end
remove_plans() click to toggle source

Remove plans from application that do not exist in chargebee

# File lib/chargebee_rails/sync_plans.rb, line 53
def remove_plans
  cb_plan_ids = cb_plans.flat_map(&:id)
  Plan.all.reject { |plan| cb_plan_ids.include?(plan.plan_id) }
          .each   { |plan| output "Deleting Plan - #{plan.plan_id}"; plan.destroy }
end
retrieve_plan_list() click to toggle source

Retrieve the plan list from chargebee

# File lib/chargebee_rails/sync_plans.rb, line 46
def retrieve_plan_list
  options = { limit: 100 }
  options[:offset] = @offset if @offset.present?
  ChargeBee::Plan.list(options)
end
sync_plans(create:true, update:true, delete:false) click to toggle source
# File lib/chargebee_rails/sync_plans.rb, line 39
def sync_plans(create:true, update:true, delete:false)
  output "Removed #{remove_plans.count} plan(s)" if (delete)
  output "Created #{create_new_plans.count} plan(s)" if (create)
  output "Updated all #{update_all_plans.count} plan(s)" if (update)
end
update_all_plans() click to toggle source

Update all existing plans in the application

# File lib/chargebee_rails/sync_plans.rb, line 67
def update_all_plans
  cb_plans.map do |cb_plan|
    Plan.find_by(plan_id: cb_plan.id).update(plan_params(cb_plan))
  end
end