Sha256: 6ff59608e33eeb9b6f12972b569852993b23db923d8b364591bad48014828f81

Contents?: true

Size: 1.64 KB

Versions: 46

Compression:

Stored size: 1.64 KB

Contents

module ForestLiana
  class StripeCardsGetter
    attr_accessor :records

    def initialize(params, secret_key, reference)
      @params = params
      Stripe.api_key = ForestLiana.integrations[:stripe][:api_key]
    end

    def count
      @cards.try(:total_count) || 0
    end

    def perform
      params = { limit: limit, offset: offset, object: 'card' }
      params['include[]'] = 'total_count'

      resource = user_collection.find(@params[:id])
      customer = resource[user_field]

      if customer.blank?
        @records = []
      else
        fetch_cards(customer, params)
      end
    end

    def fetch_cards(customer, params)
      @cards = Stripe::Customer.retrieve(customer).sources.all(params)
      if @cards.blank?
        @records = []
        return
      end

      @records = @cards.data.map do |d|
        query = {}
        query[user_field] = d.customer
        d.customer = user_collection.find_by(query)

        d
      end
    end

    def offset
      return 0 unless pagination?

      number = @params[:page][:number]
      if number && number.to_i > 0
        (number.to_i - 1) * limit
      else
        0
      end
    end

    def limit
      return 10 unless pagination?

      if @params[:page][:size]
        @params[:page][:size].to_i
      else
        10
      end
    end

    def pagination?
      @params[:page] && @params[:page][:number]
    end

    def user_collection
      ForestLiana.integrations
        .try(:[], :stripe)
        .try(:[], :user_collection)
        .try(:constantize)
    end

    def user_field
      ForestLiana.integrations
        .try(:[], :stripe)
        .try(:[], :user_field)
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
forest_liana-1.3.8 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.7 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.6 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.5 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.4 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.3 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.2 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.1 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.0 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.2.6 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.2.5 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.2.3 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.2.2 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.2.1 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.1.35 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.1.34 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.1.33 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.1.32 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.1.31 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.1.30 app/services/forest_liana/stripe_cards_getter.rb