Class: Bearcat::ApiArray
- Inherits:
-
Object
- Object
- Bearcat::ApiArray
- Includes:
- Enumerable
- Defined in:
- lib/bearcat/api_array.rb
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.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bearcat/api_array.rb', line 27 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.
5 6 7 |
# File 'lib/bearcat/api_array.rb', line 5 def members @members end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
5 6 7 |
# File 'lib/bearcat/api_array.rb', line 5 def raw_response @raw_response end |
Class Method Details
.make_indifferent(thing) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/bearcat/api_array.rb', line 17 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
7 8 9 10 11 12 13 14 15 |
# File 'lib/bearcat/api_array.rb', line 7 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
46 47 48 |
# File 'lib/bearcat/api_array.rb', line 46 def [](i) @members[i] end |
#all_pages!(page_count = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/bearcat/api_array.rb', line 98 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
90 91 92 93 94 95 96 |
# File 'lib/bearcat/api_array.rb', line 90 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
54 55 56 |
# File 'lib/bearcat/api_array.rb', line 54 def each(&block) @members.each { |member| block.call(member) } end |
#each_page(page_count = nil, &block) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bearcat/api_array.rb', line 76 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
50 51 52 |
# File 'lib/bearcat/api_array.rb', line 50 def last @members.last end |
#method ⇒ Object
42 43 44 |
# File 'lib/bearcat/api_array.rb', line 42 def method raw_response.env[:method] end |
#page_count ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/bearcat/api_array.rb', line 62 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
58 59 60 |
# File 'lib/bearcat/api_array.rb', line 58 def pages? @link_hash['next'] || @link_hash['prev'] end |