lib/bearcat/api_array.rb in bearcat-0.4 vs lib/bearcat/api_array.rb in bearcat-0.6

- old
+ new

@@ -3,29 +3,32 @@ include Enumerable attr_reader :status, :headers, :members def self.process_response(response, api_client) - if response.body.is_a? Array - ApiArray.new 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 - def initialize(response, api_client) + def initialize(response, api_client, array_key = nil) @api_client = api_client case response.status when 200..206 if response.body.is_a?(Array) @members = response.body - @status = response.status - @headers = response.headers - @method = response.env[:method] - init_pages(@headers['Link']) + elsif response.body.is_a?(Hash) && array_key + @members = response.body[array_key] end - else + @status = response.status + @headers = response.headers + @method = response.env[:method] + init_pages(@headers['Link']) end end def [](i) @members[i] @@ -111,9 +114,16 @@ def load_page(rel) if @link_hash.has_key? rel response = get_page(@link_hash[rel]) ApiArray.process_response(response, @api_client) + end + end + + #TODO: This is a quick fix for JSONAPI responses if we need to do this for anything else we need to do this a better way + def self.array_key(response) + if response.env[:method] == :get && response.env[:url].path.match(/.*\/(courses||groups)\/\d+\/conferences/) + 'conferences' end end end end \ No newline at end of file