Commit b5d4395b449ce47fd67b7db3b860b5673541ff60
1 parent
dc4023c9
Exists in
master
and in
1 other branch
add API for notices
Showing
4 changed files
with
82 additions
and
1 deletions
Show diff stats
... | ... | @@ -0,0 +1,23 @@ |
1 | +class Api::V1::NoticesController < ApplicationController | |
2 | + respond_to :json, :xml | |
3 | + | |
4 | + def index | |
5 | + query = {} | |
6 | + fields = %w{created_at message error_class} | |
7 | + | |
8 | + if params.key?(:start_date) && params.key?(:end_date) | |
9 | + start_date = Time.parse(params[:start_date]).utc | |
10 | + end_date = Time.parse(params[:end_date]).utc | |
11 | + query = {:created_at => {"$lte" => end_date, "$gte" => start_date}} | |
12 | + end | |
13 | + | |
14 | + results = benchmark("[api/v1/notices_controller] query time") { Mongoid.master["notices"].find(query, fields: fields).to_a } | |
15 | + | |
16 | + respond_to do |format| | |
17 | + format.html { render json: Yajl.dump(results) } # render JSON if no extension specified on path | |
18 | + format.json { render json: Yajl.dump(results) } | |
19 | + format.xml { render xml: results } | |
20 | + end | |
21 | + end | |
22 | + | |
23 | +end | ... | ... |
app/controllers/notices_controller.rb
config/routes.rb
... | ... | @@ -44,6 +44,7 @@ Errbit::Application.routes.draw do |
44 | 44 | namespace :api do |
45 | 45 | namespace :v1 do |
46 | 46 | resources :problems, :only => [:index], :defaults => { :format => 'json' } |
47 | + resources :notices, :only => [:index], :defaults => { :format => 'json' } | |
47 | 48 | end |
48 | 49 | end |
49 | 50 | ... | ... |
... | ... | @@ -0,0 +1,58 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe Api::V1::NoticesController do | |
4 | + | |
5 | + context "when logged in" do | |
6 | + before do | |
7 | + @user = Fabricate(:user) | |
8 | + end | |
9 | + | |
10 | + describe "GET /api/v1/notices" do | |
11 | + before do | |
12 | + Fabricate(:notice, created_at: Time.new(2012, 8, 01)) | |
13 | + Fabricate(:notice, created_at: Time.new(2012, 8, 01)) | |
14 | + Fabricate(:notice, created_at: Time.new(2012, 8, 21)) | |
15 | + Fabricate(:notice, created_at: Time.new(2012, 8, 30)) | |
16 | + end | |
17 | + | |
18 | + | |
19 | + | |
20 | + it "should return JSON if JSON is requested" do | |
21 | + get :index, auth_token: @user.authentication_token, format: "json" | |
22 | + lambda { JSON.load(response.body) }.should_not raise_error(JSON::ParserError) | |
23 | + end | |
24 | + | |
25 | + it "should return XML if XML is requested" do | |
26 | + get :index, auth_token: @user.authentication_token, format: "xml" | |
27 | + lambda { XML::Parser.string(response.body).parse }.should_not raise_error | |
28 | + end | |
29 | + | |
30 | + it "should return JSON by default" do | |
31 | + get :index, auth_token: @user.authentication_token | |
32 | + lambda { JSON.load(response.body) }.should_not raise_error(JSON::ParserError) | |
33 | + end | |
34 | + | |
35 | + | |
36 | + | |
37 | + describe "given a date range" do | |
38 | + | |
39 | + it "should return only the notices created during the date range" do | |
40 | + get :index, {auth_token: @user.authentication_token, start_date: "2012-08-01", end_date: "2012-08-27"} | |
41 | + response.should be_success | |
42 | + notices = JSON.load response.body | |
43 | + notices.length.should == 3 | |
44 | + end | |
45 | + | |
46 | + end | |
47 | + | |
48 | + it "should return all notices" do | |
49 | + get :index, {auth_token: @user.authentication_token} | |
50 | + response.should be_success | |
51 | + notices = JSON.load response.body | |
52 | + notices.length.should == 4 | |
53 | + end | |
54 | + | |
55 | + end | |
56 | + end | |
57 | + | |
58 | +end | ... | ... |