Class: Bearcat::ApiArray
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/bearcat/api_array.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(response, api_client, array_key = nil) ⇒ ApiArray
Returns a new instance of ApiArray.
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/bearcat/api_array.rb', line 29
def initialize(response, api_client, array_key = nil)
@api_client = api_client
@raw_response = response
@array_key = array_key
@page_count = nil
case response.status
when 200..206
@members = process_body(response)
init_pages
end
end
|
Instance Attribute Details
#members ⇒ Object
Returns the value of attribute members.
7
8
9
|
# File 'lib/bearcat/api_array.rb', line 7
def members
@members
end
|
#raw_response ⇒ Object
Returns the value of attribute raw_response.
7
8
9
|
# File 'lib/bearcat/api_array.rb', line 7
def raw_response
@raw_response
end
|
Class Method Details
.make_indifferent(thing) ⇒ Object
19
20
21
22
23
24
25
26
27
|
# File 'lib/bearcat/api_array.rb', line 19
def self.make_indifferent(thing)
if thing.is_a?(Array)
thing.map { |v| make_indifferent(v) }
elsif thing.is_a?(Hash)
thing.with_indifferent_access
else
thing
end
end
|
.process_response(response, api_client) ⇒ Object
9
10
11
12
13
14
15
16
17
|
# File 'lib/bearcat/api_array.rb', line 9
def self.process_response(response, api_client)
if response.body.is_a?(Array)
self.new(response, api_client)
elsif key = array_key(response)
self.new(response, api_client, key)
else
make_indifferent(response.body)
end
end
|
Instance Method Details
#[](key) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/bearcat/api_array.rb', line 48
def [](key)
if key.is_a?(Numeric)
@members[key]
else
@raw_response.body[key]
end
end
|
#all_pages!(page_count = nil) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/bearcat/api_array.rb', line 104
def all_pages!(page_count = nil)
if pages?
@members = []
iterate_pages(page_count) do |page_response|
page_members = process_body(page_response)
break unless page_members.present?
@members.concat(page_members)
end
@link_hash = {}
end
self
end
|
#all_pages_each(page_count = nil, &block) ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/bearcat/api_array.rb', line 96
def all_pages_each(page_count = nil, &block)
return to_enum(:all_pages_each, page_count) unless block_given?
each_page(page_count) do |page|
page.each &block
end
end
|
#each(&block) ⇒ Object
60
61
62
|
# File 'lib/bearcat/api_array.rb', line 60
def each(&block)
@members.each { |member| block.call(member) }
end
|
#each_page(page_count = nil, &block) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/bearcat/api_array.rb', line 82
def each_page(page_count = nil, &block)
return to_enum(:each_page, page_count) unless block_given?
if pages?
iterate_pages(page_count) do |page_response|
@members = process_body(page_response)
break unless @members.present?
block.call(@members)
end
else
block.call(@members)
end
end
|
#last ⇒ Object
56
57
58
|
# File 'lib/bearcat/api_array.rb', line 56
def last
@members.last
end
|
#method ⇒ Object
44
45
46
|
# File 'lib/bearcat/api_array.rb', line 44
def method
raw_response.env[:method]
end
|
#page_count ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/bearcat/api_array.rb', line 68
def page_count
return nil unless @link_hash['last']
uri = URI.parse(@link_hash['last'])
params = CGI.parse(uri.query)
params.dig('page', 0)&.to_i || 1
end
|
#pages? ⇒ Boolean
64
65
66
|
# File 'lib/bearcat/api_array.rb', line 64
def pages?
@link_hash['next'] || @link_hash['prev']
end
|