Class: Bearcat::ApiArray
- Inherits:
-
Object
- Object
- Bearcat::ApiArray
- Includes:
- Enumerable
- Defined in:
- lib/bearcat/api_array.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #[](i) ⇒ Object
- #all_pages!(page_count = 50) ⇒ Object
- #each(&block) ⇒ Object
- #each_page(page_count = 50, &block) ⇒ Object
- #first_page ⇒ Object
-
#initialize(response, api_client, array_key = nil) ⇒ ApiArray
constructor
A new instance of ApiArray.
- #last ⇒ Object
- #last_page ⇒ Object
- #next_page ⇒ Object
- #pages? ⇒ Boolean
- #prev_page ⇒ Object
Constructor Details
#initialize(response, api_client, array_key = nil) ⇒ ApiArray
Returns a new instance of ApiArray.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bearcat/api_array.rb', line 17 def initialize(response, api_client, array_key = nil) @api_client = api_client @array_key = array_key @page_count = 100 case response.status when 200..206 @members = process_body(response) @status = response.status @headers = response.headers @method = response.env[:method] init_pages(@headers['Link']) end end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/bearcat/api_array.rb', line 5 def headers @headers end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
5 6 7 |
# File 'lib/bearcat/api_array.rb', line 5 def members @members end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/bearcat/api_array.rb', line 5 def status @status end |
Class Method Details
.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 response.body end end |
Instance Method Details
#[](i) ⇒ Object
31 32 33 |
# File 'lib/bearcat/api_array.rb', line 31 def [](i) @members[i] end |
#all_pages!(page_count = 50) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bearcat/api_array.rb', line 78 def all_pages!(page_count = 50) if pages? @page_count = page_count response = get_page(@link_hash['first']) @headers = response.headers @status = response.status @method = response.env[:method] init_pages(@headers[:link]) @members = process_body(response) while @link_hash['next'] response = get_page(@link_hash['next']) @headers = response.headers @status = response.status @method = response.env[:method] @members.concat(process_body(response)) init_pages(@headers[:link]) end @link_hash = {} end self end |
#each(&block) ⇒ Object
39 40 41 |
# File 'lib/bearcat/api_array.rb', line 39 def each(&block) @members.each { |member| block.call(member) } end |
#each_page(page_count = 50, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bearcat/api_array.rb', line 63 def each_page(page_count = 50, &block) @page_count = page_count block.call(@members) while @link_hash['next'] response = get_page(@link_hash['next']) @headers = response.headers @status = response.status @method = response.env[:method] @members = process_body(response) init_pages(@headers[:link]) block.call(@members) end @link_hash = {} end |
#first_page ⇒ Object
55 56 57 |
# File 'lib/bearcat/api_array.rb', line 55 def first_page load_page('first') end |
#last ⇒ Object
35 36 37 |
# File 'lib/bearcat/api_array.rb', line 35 def last @members.last end |
#last_page ⇒ Object
59 60 61 |
# File 'lib/bearcat/api_array.rb', line 59 def last_page load_page('last') end |
#next_page ⇒ Object
47 48 49 |
# File 'lib/bearcat/api_array.rb', line 47 def next_page load_page('next') end |
#pages? ⇒ Boolean
43 44 45 |
# File 'lib/bearcat/api_array.rb', line 43 def pages? @link_hash['next'] || @link_hash['prev'] end |
#prev_page ⇒ Object
51 52 53 |
# File 'lib/bearcat/api_array.rb', line 51 def prev_page load_page('prev') end |