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, headers: nil, client: nil) ⇒ Response

Returns a new instance of Response.



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

def initialize(body: nil, status: nil, headers: nil, client: nil)
  @body = body
  @status = status
  @headers = headers
  @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.



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

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
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



52
53
54
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 52

def all_pages
  return next_pages(count: nil)
end

#each(&block) ⇒ Object



62
63
64
65
66
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 62

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

#next_pageObject



25
26
27
28
29
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 25

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

#next_pages(count: 1) ⇒ Object



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

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



19
20
21
22
23
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 19

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

#to_modelsObject



56
57
58
59
60
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 56

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