Skip to content

Instantly share code, notes, and snippets.

@greatseth
Created December 27, 2012 20:52
Show Gist options
  • Select an option

  • Save greatseth/4391833 to your computer and use it in GitHub Desktop.

Select an option

Save greatseth/4391833 to your computer and use it in GitHub Desktop.

Revisions

  1. greatseth created this gist Dec 27, 2012.
    60 changes: 60 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/usr/bin/env ruby

    require 'rubygems'
    require 'bundler'
    Bundler.setup :gasbuddy

    require 'json'
    require 'couchrest'

    require_relative 'gasbuddy/averages'
    require_relative 'aaa/guage_report'

    def build_doc(location_type, name, data, timestamp)
    {
    timestamp: timestamp,
    location_type: location_type,
    name: name,
    price: data[:price],
    change: data[:change]
    }
    end

    STATES = CouchRest.database("http://127.0.0.1:5984/gasbuddy-averages-dev")
    CITIES = CouchRest.database("http://127.0.0.1:5984/gasbuddy-averages-dev")

    next_run = nil
    interval = 60 * 30

    loop do
    if next_run.nil? or (next_run < (time = Time.new).to_i)
    timestamp = time.to_i

    ###

    puts "#{time} getting GasBuddy averages..."

    next_run = timestamp + interval

    data = Gasbuddy::Averages.new.data

    data[:states].each do |name, data|
    new_doc = build_doc 'state', name, data, timestamp

    if new_doc
    saved_data = STATES.save_doc new_doc
    puts saved_data.inspect
    end
    end

    data[:cities].each do |name, data|
    new_doc = build_doc 'city', name, data, timestamp

    if new_doc
    saved_data = CITIES.save_doc new_doc
    puts saved_data.inspect
    end
    end

    end
    end