Commit f719e97e0bc5025a6e2353ac094012e54f6c1275
1 parent
f39fed7d
Exists in
master
and in
1 other branch
Add a problems err and notices in api show action
Showing
2 changed files
with
10 additions
and
6 deletions
Show diff stats
app/controllers/api/v1/problems_controller.rb
| 1 | 1 | class Api::V1::ProblemsController < ApplicationController |
| 2 | 2 | respond_to :json, :xml |
| 3 | + FIELDS = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count} | |
| 3 | 4 | |
| 4 | 5 | def show |
| 5 | 6 | result = benchmark("[api/v1/problems_controller/show] query time") do |
| 6 | - Problem.find(params[:id]) | |
| 7 | + problem = Problem.only(FIELDS).find(params[:id]) | |
| 8 | + problem.as_json(include: {errs: { include: :notices}}) | |
| 7 | 9 | end |
| 8 | 10 | |
| 11 | + | |
| 9 | 12 | respond_to do |format| |
| 10 | - format.any(:html, :json) { render :json => Yajl.dump(result) } # render JSON if no extension specified on path | |
| 13 | + format.any(:html, :json) { render :json => result } # render JSON if no extension specified on path | |
| 11 | 14 | format.xml { render :xml => result } |
| 12 | 15 | end |
| 13 | 16 | end |
| 14 | 17 | |
| 15 | 18 | def index |
| 16 | 19 | query = {} |
| 17 | - fields = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count} | |
| 18 | 20 | |
| 19 | 21 | if params.key?(:start_date) && params.key?(:end_date) |
| 20 | 22 | start_date = Time.parse(params[:start_date]).utc |
| ... | ... | @@ -23,7 +25,7 @@ class Api::V1::ProblemsController < ApplicationController |
| 23 | 25 | end |
| 24 | 26 | |
| 25 | 27 | results = benchmark("[api/v1/problems_controller/index] query time") do |
| 26 | - Problem.where(query).with(:consistency => :strong).only(fields).to_a | |
| 28 | + Problem.where(query).with(:consistency => :strong).only(FIELDS).to_a | |
| 27 | 29 | end |
| 28 | 30 | |
| 29 | 31 | respond_to do |format| | ... | ... |
spec/controllers/api/v1/problems_controller_spec.rb
| ... | ... | @@ -9,7 +9,9 @@ describe Api::V1::ProblemsController do |
| 9 | 9 | |
| 10 | 10 | describe "GET /api/v1/problems/:id" do |
| 11 | 11 | before do |
| 12 | - Fabricate(:problem, :first_notice_at => Date.new(2012, 8, 01), :resolved_at => Date.new(2012, 8, 02)) | |
| 12 | + notice = Fabricate(:notice) | |
| 13 | + err = Fabricate(:err, :notices => [notice]) | |
| 14 | + problem = Fabricate(:problem, :errs => [err]) | |
| 13 | 15 | end |
| 14 | 16 | |
| 15 | 17 | it "returns JSON if JSON is requested" do |
| ... | ... | @@ -20,7 +22,7 @@ describe Api::V1::ProblemsController do |
| 20 | 22 | it "returns the correct problem" do |
| 21 | 23 | requested_problem = Problem.first |
| 22 | 24 | get :show, :auth_token => @user.authentication_token, :format => "json", :id => requested_problem.id |
| 23 | - expect( response.body ).to eq(requested_problem.to_json) | |
| 25 | + expect( response.body ).to eq(requested_problem.to_json(include: {errs: { include: :notices}})) | |
| 24 | 26 | end |
| 25 | 27 | end |
| 26 | 28 | ... | ... |