Class: Spaceship::ConnectAPI::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
spaceship/lib/spaceship/connect_api/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body: nil, status: nil, client: nil) ⇒ Response

Returns a new instance of Response.



11
12
13
14
15
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 11

def initialize(body: nil, status: nil, client: nil)
  @body = body
  @status = status
  @client = client
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 7

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 9

def client
  @client
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 8

def status
  @status
end

Instance Method Details

#all_pagesObject



50
51
52
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 50

def all_pages
  return next_pages(count: nil)
end

#each(&block) ⇒ Object



60
61
62
63
64
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 60

def each(&block)
  to_models.each do |model|
    yield(model)
  end
end

#next_pageObject



23
24
25
26
27
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 23

def next_page
  url = next_url
  return nil if url.nil?
  return client.get(url)
end

#next_pages(count: 1) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 29

def next_pages(count: 1)
  if !count.nil? && count < 0
    count = 0
  end

  responses = [self]
  counter = 0

  resp = self
  loop do
    resp = resp.next_page
    break if resp.nil?
    responses << resp
    counter += 1

    break if !count.nil? && counter >= count
  end

  return responses
end

#next_urlObject



17
18
19
20
21
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 17

def next_url
  return nil if body.nil?
  links = body["links"] || {}
  return links["next"]
end

#to_modelsObject



54
55
56
57
58
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 54

def to_models
  return [] if body.nil?
  model_or_models = Spaceship::ConnectAPI::Models.parse(body)
  return [model_or_models].flatten
end