Sha256: 2273ae4fa267cbcc54e0f905b11da0b83555964168ec7a1f5b7f60fa2e4e238f

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require "chicrime/version"
require "soda/client"

module Chicrime
  class Dataset
    attr_accessor :client, :soql
    attr_reader :dataset_id

    def initialize token=''
      @client = SODA::Client.new({:domain => 'data.cityofchicago.org', :app_token => token})
      @dataset_id = 'ijzp-q8t2'
      @soql = {}
    end

    def query &block
      @soql = {}
      instance_eval(&block)
      results
    end

    def method_missing m, *args, &block 
      if soql["$where"]
        prev = soql["$where"] + " AND"
      else
        prev = ""
      end

      q = "#{prev} #{m} #{args[0]} #{args[1]}".strip

      @soql.store("$where", "#{q}")
    end

    def equals data
      "= #{data}"
    end

    def where *args, &block
      if block_given?
        instance_eval(&block)
      else
        q = args.count > 0 ? args * " AND " : ""
        @soql.store("$where", "#{q}")
      end

      self
    end

    def limit n
      @soql.store("$limit", "#{n}")
      self
    end
    
    def select *args
      q = args.count > 0 ? args * ', ' : ''
      @soql.store("$select", "#{q}")
      self
    end

    def order column, order=:ASC
      @soql.store("$order", "#{column} #{order}")
      self
    end

    def results 
      puts @soql
      @client.get(@dataset_id, @soql)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chicrime-0.2.1 lib/chicrime.rb