Sha256: f4e5ca175b6cb759b9cbccb551b2ed0fec7f3fbca2de481fb7655d73b3b1cb97

Contents?: true

Size: 1.67 KB

Versions: 11

Compression:

Stored size: 1.67 KB

Contents

module PagarMe
  class BulkAnticipation < NestedModel
    def confirm
      update self.class.confirm(parent_id, id).attributes
      self
    end

    def delete
      self.class.delete parent_id, id
    end

    def cancel
      update self.class.cancel(parent_id, id).attributes
      self
    end

    class << self
      def parent_resource_name
        'recipients'
      end

      def confirm(recipient_id, id)
        PagarMe::Request.post( url(recipient_id, id, 'confirm') ).call
      end

      def delete(recipient_id, id)
        PagarMe::Request.delete( url(recipient_id, id) ).call
      end

      def cancel(recipient_id, id)
        PagarMe::Request.post( url(recipient_id, id, 'cancel') ).call
      end

      def create(params = Hash.new)
        super normalize_params(params)
      end

      def bulk_anticipations_limits(recipient_id, params = Hash.new)
        PagarMe::Request.get(url(recipient_id, 'limits'), params: normalize_params(params) ).call
      end

      protected
      def normalize_params(params = Hash.new)
        normalized_params = Hash[ params.map{ |key, value| [key.to_sym, value] } ]
        normalized_params[:timeframe] = :start if normalized_params[:timeframe].blank?

        raise RequestError.new('Missing payment_date param') if normalized_params[:payment_date].blank?
        normalized_params[:payment_date] = date_to_milliseconds_since_epoch normalized_params[:payment_date]

        normalized_params
      end

      def date_to_milliseconds_since_epoch(date)
        case date
        when Date
          date.to_time.to_i * 1000
        when Time
          date.to_i * 1000
        else
          date
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pagarme-2.4.0 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.3.0 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.2.3 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.2.2 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.2.1 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.2.0 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.1.4 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.1.3 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.1.2 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.1.1 lib/pagarme/resources/bulk_anticipation.rb
pagarme-2.1.0 lib/pagarme/resources/bulk_anticipation.rb