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.28 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.27 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.26 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.25 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.24 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.23 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.22 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.21 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.20 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.19 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.18 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.17 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.16 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.15 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.14 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.13 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.12 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.11 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.10 app/services/forest_liana/stripe_cards_getter.rb
forest_liana-1.3.9 app/services/forest_liana/stripe_cards_getter.rb