Class: Runa::Products

Inherits:
Response show all
Defined in:
lib/runa/models/products.rb

Constant Summary collapse

PATH =
'/product'

Constants inherited from Response

Response::STATUS

Instance Attribute Summary collapse

Attributes inherited from Response

#error_code, #error_details, #error_string, #payload, #status

Instance Method Summary collapse

Methods inherited from Response

#is_successful?

Methods included from Initializable

#initialize

Instance Attribute Details

#allObject

Returns the value of attribute all.



6
7
8
# File 'lib/runa/models/products.rb', line 6

def all
  @all
end

Instance Method Details

#find(name, value) ⇒ Object

Find all products by fieldname.



32
33
34
# File 'lib/runa/models/products.rb', line 32

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

#get(ctx) ⇒ Object

Product Details List GET /v2/product



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/runa/models/products.rb', line 10

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

#parse(response) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/runa/models/products.rb', line 36

def parse(response)
  super(response)

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