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_pages(&block) ⇒ Object



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

def all_pages(&block)
  return next_pages(count: nil, &block)
end

#all_pages_each(&block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 72

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

  resp = self
  loop do
    resp = resp.next_page
    break if resp.nil?
    resp.each(&block)
  end
end

#each(&block) ⇒ Object



66
67
68
69
70
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 66

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

#next_page(&block) ⇒ Object



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

def next_page(&block)
  url = next_url
  return nil if url.nil?
  if block_given?
    return yield(url)
  else
    return client.get(url)
  end
end

#next_pages(count: 1, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'spaceship/lib/spaceship/connect_api/response.rb', line 35

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

  responses = [self]
  counter = 0

  resp = self
  loop do
    resp = resp.next_page(&block)
    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



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

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