Sha256: 90e464e7892e32165641ea5904adefc3d5ed8c43479a4911e4d6c9716ea025d8

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module Pollster

  class Chart < Base

    attr_reader :title, :slug, :poll_count, :last_updated, :url, :estimates, :estimates_by_date, :state, :topic

    def initialize(params={})
      params.each_pair do |k,v|
        instance_variable_set("@#{k}", v)
      end
    end

    # Get a list of polls for this chart.
    def polls(params={})
      Poll.where(params.merge({:chart => self.slug}))
    end

    # Get a list of all charts.
    def self.all
      invoke('charts').map { |chart| self.create(chart) }
    end

    # Get a chart based on its slug.
    def self.find(slug)
      self.create invoke("charts/#{slug}")
    end

    # Get a list of charts based on the given parameters.
    # See API documentation for acceptable parameters.
    def self.where(params={})
      if params.empty?
        raise "A search parameter is required"
      end
      invoke('charts', params).map { |chart| self.create(chart) }
    end

    def to_s
      "#{self.class}: #{self.title}"
    end

    def inspect
      "<#{self.class}: #{self.title}>"
    end

    def estimates_by_date
      @estimates_by_date ||= Pollster::Chart.find(slug).estimates_by_date
    end

    private

      def self.create(data)
        data = Hash[*data.map { |k, v| [k.to_sym, v] }.flatten(1)]
        data[:last_updated] = Time.parse(data[:last_updated])
        data[:estimates].map! { |estimate| {:choice => estimate['choice'], :value => estimate['value']} }
        if data[:estimates_by_date]
          data[:estimates_by_date] = data[:estimates_by_date].map do |x|
            estimate = hash_keys_to_sym(x)
            estimate[:date] = Date.parse(estimate[:date])
            estimate[:estimates] = estimate[:estimates].map { |e| hash_keys_to_sym(e) }
            estimate
          end
        end
        self.new(data)
      end


  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pollster-0.2.2 lib/pollster/chart.rb
pollster-0.2.1 lib/pollster/chart.rb
pollster-0.2.0 lib/pollster/chart.rb
pollster-0.1.1 lib/pollster/chart.rb
pollster-0.1.0 lib/pollster/chart.rb