Class: Bearcat::ApiArray
- Inherits:
-
Object
- Object
- Bearcat::ApiArray
- Includes:
- Enumerable
- Defined in:
- lib/bearcat/api_array.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
Class Method Summary collapse
Instance Method Summary collapse
- #[](i) ⇒ Object
- #all_pages!(page_count = nil) ⇒ Object
- #all_pages_each(page_count = nil, &block) ⇒ Object
- #each(&block) ⇒ Object
- #each_page(page_count = nil, &block) ⇒ Object
-
#initialize(response, api_client, array_key = nil) ⇒ ApiArray
constructor
A new instance of ApiArray.
- #last ⇒ Object
- #method ⇒ Object
- #page_count ⇒ Object
- #pages? ⇒ Boolean
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(headers[:link]) end end |
Instance Attribute Details
#members ⇒ Object (readonly)
Returns the value of attribute members.
7 8 9 |
# File 'lib/bearcat/api_array.rb', line 7 def members @members end |
#raw_response ⇒ Object (readonly)
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) ApiArray.new(response, api_client) elsif key = array_key(response) ApiArray.new(response, api_client, key) else make_indifferent(response.body) end end |
Instance Method Details
#[](i) ⇒ Object
48 49 50 |
# File 'lib/bearcat/api_array.rb', line 48 def [](i) @members[i] end |
#all_pages!(page_count = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bearcat/api_array.rb', line 100 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
92 93 94 95 96 97 98 |
# File 'lib/bearcat/api_array.rb', line 92 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
56 57 58 |
# File 'lib/bearcat/api_array.rb', line 56 def each(&block) @members.each { |member| block.call(member) } end |
#each_page(page_count = nil, &block) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/bearcat/api_array.rb', line 78 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
52 53 54 |
# File 'lib/bearcat/api_array.rb', line 52 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
64 65 66 67 68 69 70 |
# File 'lib/bearcat/api_array.rb', line 64 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
60 61 62 |
# File 'lib/bearcat/api_array.rb', line 60 def pages? @link_hash['next'] || @link_hash['prev'] end |