Class: Bearcat::ApiArray

Inherits:
Object
  • 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.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bearcat/api_array.rb', line 17

def initialize(response, api_client, array_key = nil)
  @api_client = api_client
  @array_key = array_key
  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

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/bearcat/api_array.rb', line 5

def headers
  @headers
end

#membersObject (readonly)

Returns the value of attribute members.



5
6
7
# File 'lib/bearcat/api_array.rb', line 5

def members
  @members
end

#statusObject (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.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



30
31
32
# File 'lib/bearcat/api_array.rb', line 30

def [](i)
  @members[i]
end

#all_pages!(page_count = 50) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bearcat/api_array.rb', line 63

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



38
39
40
# File 'lib/bearcat/api_array.rb', line 38

def each(&block)
  @members.each { |member| block.call(member) }
end

#first_pageObject



55
56
57
# File 'lib/bearcat/api_array.rb', line 55

def first_page
  load_page('first')
end

#lastObject



34
35
36
# File 'lib/bearcat/api_array.rb', line 34

def last
  @members.last
end

#last_pageObject



59
60
61
# File 'lib/bearcat/api_array.rb', line 59

def last_page
  load_page('last')
end

#next_pageObject



46
47
48
49
# File 'lib/bearcat/api_array.rb', line 46

def next_page
  load_page('next')

end

#pages?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bearcat/api_array.rb', line 42

def pages?
  @link_hash['next'] || @link_hash['prev']
end

#prev_pageObject



51
52
53
# File 'lib/bearcat/api_array.rb', line 51

def prev_page
  load_page('prev')
end