Commit f39fed7db9d0275da4c0889c32a23103e1f847fb

Authored by Callum Dryden
1 parent 78c34e2f
Exists in master and in 1 other branch production

Add show action to the api v1 for problems

This allows a problem id to be passed to the problem api and only
information for that problem will be returned.
CONTRIBUTORS.md
@@ -42,11 +42,14 @@ @@ -42,11 +42,14 @@
42 - [@nashby][] 42 - [@nashby][]
43 - [@shingara][] 43 - [@shingara][]
44 - [@tscolari][] 44 - [@tscolari][]
  45 +- [@callumd][]
  46 +
45 47
46 [@anicet]: https://github.com/anicet 48 [@anicet]: https://github.com/anicet
47 [@nashby]: https://github.com/nashby 49 [@nashby]: https://github.com/nashby
48 [@shingara]: https://github.com/shingara 50 [@shingara]: https://github.com/shingara
49 [@tscolari]: https://github.com/tscolari 51 [@tscolari]: https://github.com/tscolari
  52 +[@callumd]: https://github.com/callumd
50 53
51 ## 0.2.0 - 2013-09-11 54 ## 0.2.0 - 2013-09-11
52 55
app/controllers/api/v1/problems_controller.rb
1 class Api::V1::ProblemsController < ApplicationController 1 class Api::V1::ProblemsController < ApplicationController
2 respond_to :json, :xml 2 respond_to :json, :xml
3 3
  4 + def show
  5 + result = benchmark("[api/v1/problems_controller/show] query time") do
  6 + Problem.find(params[:id])
  7 + end
  8 +
  9 + respond_to do |format|
  10 + format.any(:html, :json) { render :json => Yajl.dump(result) } # render JSON if no extension specified on path
  11 + format.xml { render :xml => result }
  12 + end
  13 + end
  14 +
4 def index 15 def index
5 query = {} 16 query = {}
6 fields = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count} 17 fields = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count}
@@ -11,7 +22,7 @@ class Api::V1::ProblemsController &lt; ApplicationController @@ -11,7 +22,7 @@ class Api::V1::ProblemsController &lt; ApplicationController
11 query = {:first_notice_at=>{"$lte"=>end_date}, "$or"=>[{:resolved_at=>nil}, {:resolved_at=>{"$gte"=>start_date}}]} 22 query = {:first_notice_at=>{"$lte"=>end_date}, "$or"=>[{:resolved_at=>nil}, {:resolved_at=>{"$gte"=>start_date}}]}
12 end 23 end
13 24
14 - results = benchmark("[api/v1/problems_controller] query time") do 25 + results = benchmark("[api/v1/problems_controller/index] query time") do
15 Problem.where(query).with(:consistency => :strong).only(fields).to_a 26 Problem.where(query).with(:consistency => :strong).only(fields).to_a
16 end 27 end
17 28
config/routes.rb
@@ -50,7 +50,7 @@ Errbit::Application.routes.draw do @@ -50,7 +50,7 @@ Errbit::Application.routes.draw do
50 50
51 namespace :api do 51 namespace :api do
52 namespace :v1 do 52 namespace :v1 do
53 - resources :problems, :only => [:index], :defaults => { :format => 'json' } 53 + resources :problems, :only => [:index, :show], :defaults => { :format => 'json' }
54 resources :notices, :only => [:index], :defaults => { :format => 'json' } 54 resources :notices, :only => [:index], :defaults => { :format => 'json' }
55 resources :stats, :only => [], :defaults => { :format => 'json' } do 55 resources :stats, :only => [], :defaults => { :format => 'json' } do
56 collection do 56 collection do
spec/controllers/api/v1/problems_controller_spec.rb
@@ -7,6 +7,23 @@ describe Api::V1::ProblemsController do @@ -7,6 +7,23 @@ describe Api::V1::ProblemsController do
7 @user = Fabricate(:user) 7 @user = Fabricate(:user)
8 end 8 end
9 9
  10 + describe "GET /api/v1/problems/:id" do
  11 + before do
  12 + Fabricate(:problem, :first_notice_at => Date.new(2012, 8, 01), :resolved_at => Date.new(2012, 8, 02))
  13 + end
  14 +
  15 + it "returns JSON if JSON is requested" do
  16 + get :show, :auth_token => @user.authentication_token, :format => "json", :id => Problem.first.id
  17 + expect { JSON.load(response.body) }.not_to raise_error()#JSON::ParserError)
  18 + end
  19 +
  20 + it "returns the correct problem" do
  21 + requested_problem = Problem.first
  22 + get :show, :auth_token => @user.authentication_token, :format => "json", :id => requested_problem.id
  23 + expect( response.body ).to eq(requested_problem.to_json)
  24 + end
  25 + end
  26 +
10 describe "GET /api/v1/problems" do 27 describe "GET /api/v1/problems" do
11 before do 28 before do
12 Fabricate(:problem, :first_notice_at => Date.new(2012, 8, 01), :resolved_at => Date.new(2012, 8, 02)) 29 Fabricate(:problem, :first_notice_at => Date.new(2012, 8, 01), :resolved_at => Date.new(2012, 8, 02))