Sha256: 888c9b6006e9fc9494200341bdb1bdd8e36d4d9607745bf065de7a7bcb931414
Contents?: true
Size: 1.72 KB
Versions: 5
Compression:
Stored size: 1.72 KB
Contents
require 'cgi' require_relative 'hal_resource' module Finix class Pagination include Enumerable include HalResource attr_accessor :resource_class def initialize(href, opts={}) @hyperlinks = {} @attributes = {} @hyperlinks[:self] = href @resource_class = nil end def each return enum_for :each unless block_given? current loop do items.each { |item| yield item } fetch :next end end def current refresh unless items items end def fetch(scope) # :next, :last, :first, :prev, :self scope = scope.to_s.to_sym if @hyperlinks[scope] load_from @hyperlinks[scope] return self.items end raise StopIteration end def refresh fetch :self end def create(attrs={}) attrs = attrs.attributes if attrs.is_a?(Finix::Resource) attrs = Finix::Utils.indifferent_read_access attrs href = @hyperlinks[:self] if @resource_class.nil? @resource_class = Finix.from_href(href) end @resource_class.new(attrs, href).save end private def load_from(url, params = {}) params ||= {} response = Finix.get url, params body = Finix::Utils.indifferent_read_access response.body links = body.delete('_links') links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] } @attributes = {} # clear attributes if body.has_key? '_embedded' resource_name, resources = body.delete('_embedded').first @resource_class = Finix.from_hypermedia_registry resource_name @attributes['items'] = resources.map { |item| @resource_class.construct_from_response item } end end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
finix-0.6 | lib/finix/pagination.rb |
finix-0.5 | lib/finix/pagination.rb |
finix-0.4 | lib/finix/pagination.rb |
finix-0.3 | lib/finix/pagination.rb |
finix-0.2 | lib/finix/pagination.rb |