Sha256: 4e4e2b3016c409fdeadf2be17c98527e0810a1c28c11b882176a62c37b301ef3

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

class Runa::Products < Runa::Response
  PATH = '/product'

  attr_accessor :all

  # Product Details List
  # GET /v2/product
  def get(ctx)
    @all = []
    @after_key = nil

    loop do
      options = {}
      if !@after_key.nil?
        options = {
          after: @after_key
        }
      end
      response = ctx.request(:get, PATH, options, '')
      parse(response)
      if @after_key.nil?
        break
      end
    end

    self
  end

  # Find all products by fieldname.
  def find(name, value)
    Runa::Products.new(all: all.select! { |p| p.send(name).eql?(value) })
  end

  def parse(response)
    super(response)

    if is_successful?
      # TODO: separate?
      if @payload['catalog']
          products = @payload['catalog']
          products.delete_if { |product| !product['payout_type'].eql? 'gift_card'.freeze }
          @all.concat(@payload['catalog'].map { |p| Runa::Product.new(p) })
      end
      @after_key = @payload['pagination']['cursors']['after']
    else
      @all = []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
runa-ruby-client-0.3.1 lib/runa/models/products.rb