Sha256: eb82a3a24eb3f3d334cf96a1f6432c08a4dcd24f01fab9893e4074b4beb628e9

Contents?: true

Size: 1.8 KB

Versions: 19

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module ChartMogul
  class Subscription < APIResource
    set_resource_name 'Subscription'
    set_resource_path '/v1/import/customers/:customer_uuid/subscriptions'

    readonly_attr :uuid
    writeable_attr :external_id
    writeable_attr :subscription_set_external_id
    readonly_attr :cancellation_dates, default: []

    readonly_attr :plan_uuid
    writeable_attr :data_source_uuid
    readonly_attr :customer_uuid

    include API::Actions::Custom

    def update_cancellation_dates(cancellation_dates_array)
      cancellation_dates = parse_dates(cancellation_dates_array)
      custom!(:patch, "/v1/import/subscriptions/#{uuid}", cancellation_dates: cancellation_dates)
    end

    def cancel(cancelled_at)
      custom!(:patch, "/v1/import/subscriptions/#{uuid}", cancelled_at: cancelled_at)
    end

    def connect(customer_uuid, subscriptions)
      subscriptions.unshift(self)
      custom!(:post, "/v1/customers/#{customer_uuid}/connect_subscriptions", subscriptions: subscriptions.map(&:serialize_for_write))
    end

    def self.all(customer_uuid, options = {})
      Subscriptions.all(customer_uuid, options)
    end

    private

    def set_cancellation_dates(cancellation_dates_array)
      @cancellation_dates = parse_dates(cancellation_dates_array)
    end

    def parse_dates(dates)
      dates.map { |date| Time.parse(date.to_s) }
    end
  end

  class Subscriptions < APIResource
    readonly_attr :customer_uuid

    set_resource_name 'Subscriptions'
    set_resource_path '/v1/import/customers/:customer_uuid/subscriptions'

    set_resource_root_key :subscriptions

    include Concerns::Entries
    include Concerns::Pageable2

    set_entry_class Subscription

    def self.all(customer_uuid, options = {})
      super(options.merge(customer_uuid: customer_uuid))
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
chartmogul-ruby-3.3.0 lib/chartmogul/subscription.rb
chartmogul-ruby-3.2.0 lib/chartmogul/subscription.rb
chartmogul-ruby-3.1.0 lib/chartmogul/subscription.rb
chartmogul-ruby-3.0.2 lib/chartmogul/subscription.rb
chartmogul-ruby-3.0.0 lib/chartmogul/subscription.rb
chartmogul-ruby-2.9.0 lib/chartmogul/subscription.rb
chartmogul-ruby-2.1.0 lib/chartmogul/subscription.rb
chartmogul-ruby-2.0.0 lib/chartmogul/subscription.rb
chartmogul-ruby-1.7.3 lib/chartmogul/subscription.rb
chartmogul-ruby-1.7.2 lib/chartmogul/subscription.rb
chartmogul-ruby-1.7.1 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.9 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.8 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.7 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.6 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.4 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.3 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.2 lib/chartmogul/subscription.rb
chartmogul-ruby-1.6.1 lib/chartmogul/subscription.rb