Skip to content

Commit

Permalink
Import data and post a new bucket and get back the ID
Browse files Browse the repository at this point in the history
  • Loading branch information
courtenay committed Jan 12, 2010
1 parent b348b0d commit 65a05b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def app
assert_equal 0, last_response.content_length
end

it "creates a bucket" do
post "/buckets", :name => "foo-bar"
assert last_response.ok?
bucket = Weed::Bucket.find_by_name("foo-bar")
assert_equal bucket.id.to_s, last_response.body
end

# only supports 'name' right now
it "records stats with params describing a bucket" do
Weed::Bucket.delete_all
Expand Down Expand Up @@ -92,4 +99,16 @@ def app
get "/stats/5/week/#{Date.today.year}/#{Date.today.month}/#{Date.today.day}/daily"
assert_equal "[0,0,0,0,0,1,2]", last_response.body
end

it "imports data" do
Weed::CachedStats.delete_all # wtf
Weed::Stats.delete_all # wtf

post "/import/6", :data => ["2009-12-5", "2009-12-5", "2009-12-5", "2009-12-6", "2009-12-18"]
assert last_response.ok?
assert_equal({:state=>"success", "imported"=>5}.to_json, last_response.body)

get "/stats/6/day/2009-12-5"
assert_equal({"count" => 4}.to_json, last_response.body)
end
end
17 changes: 16 additions & 1 deletion weed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ class Application < Sinatra::Base
# ok
end

post '/import/:bucket_id' do
# todo: csv?
counter = 0
# todo: move to model/class method?
params[:data].each do |date|
Stats.hit! "cdate" => date, "bucket_id" => params[:bucket_id]
counter += 1
end
{ "state" => "success", "imported" => counter }.to_json
end

# todo: auth


get '/buckets/:name' do
bucket = Bucket.find_by_name params[:name]
{"bucket" => { "id" => bucket.id, "counter" => bucket.counter }}.to_json
end

post "/buckets" do
bucket = Bucket.find_or_create_by_name params[:name]
bucket.id.to_s
end

# todo: get /stats/1/day/2009-12-5
# todo: get /stats/14/month/2009-12
Expand Down

0 comments on commit 65a05b6

Please sign in to comment.