Skip to main content
added 20 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Ruby; Fetch values from third party API within initialize method Class for Stripe transactions

I just wonder whether it is thisa common approach to hit the third api-party API and fetch values within initializethe initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end
     

Ruby; Fetch values from third party API within initialize method

I just wonder is this common approach to hit the third api and fetch values within initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end
     

Class for Stripe transactions

I just wonder whether it is a common approach to hit the third-party API and fetch values within the initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end
     
deleted 75 characters in body
Source Link
Peco
  • 125
  • 4

I just wonder is this common approach to hit the third api and fetch values within initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
          skip unless %w{ charge, adjustment, refund }.include?(type)
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end
     

I just wonder is this common approach to hit the third api and fetch values within initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
          skip unless %w{ charge, adjustment, refund }.include?(type)
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end
     

I just wonder is this common approach to hit the third api and fetch values within initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end
     
Source Link
Peco
  • 125
  • 4

Ruby; Fetch values from third party API within initialize method

I just wonder is this common approach to hit the third api and fetch values within initialize method?

Ex.

    class BalanceTransaction::Update
      include Service
    
      attr_reader :offset, :transactions
    
      def initialize(offset)
        @offset = offset
        @transactions = fetch_transactions
      end
    
      def call
        transactions.auto_paging_each do |txn|
          type = txn[:type]
          source = txn[:source]
          skip unless %w{ charge, adjustment, refund }.include?(type)
    
          case type
            when 'charge', 'adjustment' # NOTE: adjustment is something
              invoice = find_invoice(source)
              ac_id = invoice.ac.id
              update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
            when 'refund'
              refund = find_refund(source)
              ac_id = refund.invoice.account.id
              update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
          end
        end
      end

   private 
      def external_card_balance
        Stripe::BalanceTransaction
      end
     
      def fetch_transactions
       external_card_balance.all
      end