diff --git a/app/controllers/api/v1/notices_controller.rb b/app/controllers/api/v1/notices_controller.rb new file mode 100644 index 0000000..9b2e5f3 --- /dev/null +++ b/app/controllers/api/v1/notices_controller.rb @@ -0,0 +1,23 @@ +class Api::V1::NoticesController < ApplicationController + respond_to :json, :xml + + def index + query = {} + fields = %w{created_at message error_class} + + if params.key?(:start_date) && params.key?(:end_date) + start_date = Time.parse(params[:start_date]).utc + end_date = Time.parse(params[:end_date]).utc + query = {:created_at => {"$lte" => end_date, "$gte" => start_date}} + end + + results = benchmark("[api/v1/notices_controller] query time") { Mongoid.master["notices"].find(query, fields: fields).to_a } + + respond_to do |format| + format.html { render json: Yajl.dump(results) } # render JSON if no extension specified on path + format.json { render json: Yajl.dump(results) } + format.xml { render xml: results } + end + end + +end diff --git a/app/controllers/notices_controller.rb b/app/controllers/notices_controller.rb index 33ed38e..40bef96 100644 --- a/app/controllers/notices_controller.rb +++ b/app/controllers/notices_controller.rb @@ -18,4 +18,3 @@ class NoticesController < ApplicationController redirect_to app_err_path(problem.app, problem) end end - diff --git a/config/routes.rb b/config/routes.rb index 1d48329..fbdc652 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,6 +44,7 @@ Errbit::Application.routes.draw do namespace :api do namespace :v1 do resources :problems, :only => [:index], :defaults => { :format => 'json' } + resources :notices, :only => [:index], :defaults => { :format => 'json' } end end diff --git a/spec/controllers/api/v1/notices_controller_spec.rb b/spec/controllers/api/v1/notices_controller_spec.rb new file mode 100644 index 0000000..33650f8 --- /dev/null +++ b/spec/controllers/api/v1/notices_controller_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe Api::V1::NoticesController do + + context "when logged in" do + before do + @user = Fabricate(:user) + end + + describe "GET /api/v1/notices" do + before do + Fabricate(:notice, created_at: Time.new(2012, 8, 01)) + Fabricate(:notice, created_at: Time.new(2012, 8, 01)) + Fabricate(:notice, created_at: Time.new(2012, 8, 21)) + Fabricate(:notice, created_at: Time.new(2012, 8, 30)) + end + + + + it "should return JSON if JSON is requested" do + get :index, auth_token: @user.authentication_token, format: "json" + lambda { JSON.load(response.body) }.should_not raise_error(JSON::ParserError) + end + + it "should return XML if XML is requested" do + get :index, auth_token: @user.authentication_token, format: "xml" + lambda { XML::Parser.string(response.body).parse }.should_not raise_error + end + + it "should return JSON by default" do + get :index, auth_token: @user.authentication_token + lambda { JSON.load(response.body) }.should_not raise_error(JSON::ParserError) + end + + + + describe "given a date range" do + + it "should return only the notices created during the date range" do + get :index, {auth_token: @user.authentication_token, start_date: "2012-08-01", end_date: "2012-08-27"} + response.should be_success + notices = JSON.load response.body + notices.length.should == 3 + end + + end + + it "should return all notices" do + get :index, {auth_token: @user.authentication_token} + response.should be_success + notices = JSON.load response.body + notices.length.should == 4 + end + + end + end + +end -- libgit2 0.21.2