Sha256: 8ef595f743dabbdda59445843f0b2108bc46e10aa72c6685dea8b89d42420700

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

require 'httparty' unless defined? HTTParty

module Hungry
  class Collection
    include Enumerable
    include HTTParty

    autoload :Pagination, 'hungry/collection/pagination'

    attr_reader :klass, :endpoint, :criteria

    ### CLASS METHODS:

    def self.get(*args)
      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 [](index)
      build_resource results[index]
    end

    def first(n = 1)
      if n == 1 && (value = results.first)
        build_resource value
      elsif n > 1
        results.first(n).map do |result|
          build_resource result
        end
      end
    end

    def count(*args)
      if args.present?
        super
      else
        results.count
      end
    end

    def each(&block)
      results.each do |result|
        yield build_resource(result)
      end
    end

    def results
      @results ||= json['results']
    end

    def results=(results)
      @results = results
    end

    protected

    def build_resource(result)
      resource = klass.new(result)
      resource.data_source = data_source
      resource
    end

    def data_source
      Util.uri_with_params(endpoint, criteria)
    end

    def json
      @json ||= Util.parse_json(response.body)
    end

    def response
      raise NoEndpointSpecified unless endpoint

      @response ||= begin
        Util.log "GET: #{data_source}"
        self.class.get data_source
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hungry-0.2.0 lib/hungry/collection.rb
hungry-0.1.5 lib/hungry/collection.rb
hungry-0.1.4 lib/hungry/collection.rb
hungry-0.1.3 lib/hungry/collection.rb
hungry-0.1.2 lib/hungry/collection.rb