lib/namely/resource_gateway.rb in namely-0.2.1 vs lib/namely/resource_gateway.rb in namely-0.2.2

- old
+ new

@@ -4,14 +4,15 @@ def initialize(options) @access_token = options.fetch(:access_token) @endpoint = options.fetch(:endpoint) @subdomain = options.fetch(:subdomain) + @paged = options.fetch(:paged, false) end def json_index - get("/#{endpoint}", limit: :all)[resource_name] + paged ? json_index_paged : json_index_all end def json_show(id) get("/#{endpoint}/#{id}")[resource_name].first end @@ -32,10 +33,29 @@ put("/#{endpoint}/#{id}", endpoint => [changes]) end private - attr_reader :access_token, :subdomain + def json_index_all + get("/#{endpoint}", limit: :all)[resource_name] + end + + def json_index_paged + Enumerator.new do |y| + params = {} + + loop do + objects = get("/#{endpoint}", params)[resource_name] + break if objects.empty? + + objects.each { |o| y << o } + + params[:after] = objects.last["id"] + end + end + end + + attr_reader :access_token, :subdomain, :paged def resource_name endpoint.split("/").last end