Sha256: ed1fd4df2eef8cfbdc831aab03b4d5311ed7d62eac3ca88dd96779644a40ddb2
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
require 'httparty' require 'cgi' module Hungry class Collection include Enumerable include HTTParty autoload :Pagination, 'hungry/collection/pagination' attr_reader :klass, :endpoint, :criteria ### CLASS METHODS: def self.get(*args) puts "[Collection]: GET #{args.map(&:inspect).join(', ')}" self.base_uri Hungry.api_url super end ### INSTANCE METHODS: def initialize(klass, endpoint, criteria = {}) @klass = klass @endpoint = endpoint @criteria = criteria.symbolize_keys end def from_url(url) uri = URI.parse(url) options = Util.params_from_uri(uri) || klass.default_criteria self.class.new(klass, uri.path, options) end def all(new_criteria = {}) self.class.new(klass, endpoint, criteria.merge(new_criteria)) end def count(*args) if args.present? super else response['results'].count end end def each(&block) response['results'].each do |result| yield klass.new(result) end end private def response raise NoEndpointSpecified unless endpoint @response ||= begin uri = Util.uri_with_params(endpoint, criteria) self.class.get uri end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hungry-0.0.1 | lib/hungry/collection.rb |