Sha256: 0566ede58b2f01a9168444ab76a33dc4ca75e57896f73d54e0e8999f18d8fd15

Contents?: true

Size: 1.63 KB

Versions: 8

Compression:

Stored size: 1.63 KB

Contents

module Whatser
  class Collection < Whatser::Resource
    include Enumerable
    
    attr_accessor :data,:city,:city_name
    
    class << self
      def mine(opts={})
        collect_api_request :get, "/api/users/me/black/book", {:query => opts}
      end
      
      def list(user_id, opts={})
        collect_api_request :get, "/api/users/#{user_id}/black/book", {:query => opts}
      end   
      
      def add(poi_id, opts={})
        api_request :post, "/api/poi/#{poi_id}/black/book", {:body => ''}, :model => Whatser::Poi
      end
      
      def delete(poi_id)
        api_request :delete, "/api/poi/#{poi_id}/black/book", {:body => ''}, :model => Whatser::Poi
      end   
      
    private

      def collect_api_request(verb, path, opts={})
        res = api_request(verb, path, opts, :model => Whatser::Poi)
        collection = Whatser::Collection.new(res.data)
 
        collection.data = res.data
        res.data = collection
        return res
      end             
    end
    
    def add(poi_id, opts={})
      Whatser::Collection.add(poi_id, opts)
    end
    
    def delete(poi_id)
      Whatser::Collection.delete(poi_id, opts)
    end      
    
    def initialize(collection_data=nil)
      self.data = collection_data || []
      set_city
    end      
    
    def each(&block)
      (data || []).each(&block)
    end      
    
    def size
      (data || []).size
    end
    
  private      
    
    def set_city
      self.city ||= []
      data.each do |poi|
        next unless poi.is_a?(Whatser::Poi)
        self.city << poi.city unless city.include?(poi.city)
      end
      self.city_name = city.join(", ")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
whatser-0.6.1 lib/whatser/resources/collection.rb
whatser-0.6.0 lib/whatser/resources/collection.rb
whatser-0.5.0 lib/whatser/resources/collection.rb
whatser-0.4.0 lib/whatser/resources/collection.rb
whatser-0.3.2 lib/whatser/resources/collection.rb
whatser-0.3.1 lib/whatser/resources/collection.rb
whatser-0.3.0 lib/whatser/resources/collection.rb
whatser-0.2.0 lib/whatser/resources/collection.rb