Class: ForestLiana::StripeCardsGetter
- Inherits:
-
Object
- Object
- ForestLiana::StripeCardsGetter
- Defined in:
- app/services/forest_liana/stripe_cards_getter.rb
Instance Attribute Summary collapse
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
- #count ⇒ Object
- #fetch_cards(customer, params) ⇒ Object
-
#initialize(params, secret_key, reference) ⇒ StripeCardsGetter
constructor
A new instance of StripeCardsGetter.
- #limit ⇒ Object
- #offset ⇒ Object
- #pagination? ⇒ Boolean
- #perform ⇒ Object
- #user_collection ⇒ Object
- #user_field ⇒ Object
Constructor Details
#initialize(params, secret_key, reference) ⇒ StripeCardsGetter
Returns a new instance of StripeCardsGetter.
5 6 7 8 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 5 def initialize(params, secret_key, reference) @params = params Stripe.api_key = ForestLiana.integrations[:stripe][:api_key] end |
Instance Attribute Details
#records ⇒ Object
Returns the value of attribute records.
3 4 5 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 3 def records @records end |
Instance Method Details
#count ⇒ Object
10 11 12 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 10 def count @cards.try(:total_count) || 0 end |
#fetch_cards(customer, params) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 28 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 |
#limit ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 55 def limit return 10 unless pagination? if @params[:page][:size] @params[:page][:size].to_i else 10 end end |
#offset ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 44 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 |
#pagination? ⇒ Boolean
65 66 67 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 65 def pagination? @params[:page] && @params[:page][:number] end |
#perform ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 14 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 |
#user_collection ⇒ Object
69 70 71 72 73 74 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 69 def user_collection ForestLiana.integrations .try(:[], :stripe) .try(:[], :user_collection) .try(:constantize) end |
#user_field ⇒ Object
76 77 78 79 80 |
# File 'app/services/forest_liana/stripe_cards_getter.rb', line 76 def user_field ForestLiana.integrations .try(:[], :stripe) .try(:[], :user_field) end |