Commit 5fb5c16f0db065e1adced925e2d379c2e41928bc
1 parent
f26b61cd
Exists in
master
and in
1 other branch
use YAJL directly to render responses to problems API rather than using RABL
Showing
7 changed files
with
52 additions
and
40 deletions
Show diff stats
Gemfile
@@ -38,8 +38,7 @@ platform :ruby do | @@ -38,8 +38,7 @@ platform :ruby do | ||
38 | end | 38 | end |
39 | 39 | ||
40 | gem 'ri_cal' | 40 | gem 'ri_cal' |
41 | -gem 'rabl' | ||
42 | -gem 'yajl-ruby' | 41 | +gem 'yajl-ruby', :require => "yajl" |
43 | 42 | ||
44 | group :development, :test do | 43 | group :development, :test do |
45 | gem 'rspec-rails', '~> 2.6' | 44 | gem 'rspec-rails', '~> 2.6' |
Gemfile.lock
@@ -193,9 +193,6 @@ GEM | @@ -193,9 +193,6 @@ GEM | ||
193 | coderay (~> 1.0.5) | 193 | coderay (~> 1.0.5) |
194 | method_source (~> 0.7.1) | 194 | method_source (~> 0.7.1) |
195 | slop (>= 2.4.4, < 3) | 195 | slop (>= 2.4.4, < 3) |
196 | - rabl (0.6.13) | ||
197 | - activesupport (>= 2.3.14) | ||
198 | - multi_json (~> 1.0) | ||
199 | rack (1.4.1) | 196 | rack (1.4.1) |
200 | rack-cache (1.2) | 197 | rack-cache (1.2) |
201 | rack (>= 0.4) | 198 | rack (>= 0.4) |
@@ -325,7 +322,6 @@ DEPENDENCIES | @@ -325,7 +322,6 @@ DEPENDENCIES | ||
325 | oruen_redmine_client | 322 | oruen_redmine_client |
326 | pivotal-tracker | 323 | pivotal-tracker |
327 | pry | 324 | pry |
328 | - rabl | ||
329 | rack-ssl-enforcer | 325 | rack-ssl-enforcer |
330 | rails (= 3.2.6) | 326 | rails (= 3.2.6) |
331 | rails_autolink (~> 1.0.9) | 327 | rails_autolink (~> 1.0.9) |
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 | 3 | ||
3 | def index | 4 | def index |
4 | if params.key?(:start_date) && params.key?(:end_date) | 5 | if params.key?(:start_date) && params.key?(:end_date) |
@@ -8,6 +9,12 @@ class Api::V1::ProblemsController < ApplicationController | @@ -8,6 +9,12 @@ class Api::V1::ProblemsController < ApplicationController | ||
8 | else | 9 | else |
9 | @problems = Problem.all | 10 | @problems = Problem.all |
10 | end | 11 | end |
12 | + | ||
13 | + respond_to do |format| | ||
14 | + format.html { render json: ProblemPresenter.new(@problems) } # render JSON if no extension specified on path | ||
15 | + format.json { render json: ProblemPresenter.new(@problems) } | ||
16 | + format.xml { render xml: ProblemPresenter.new(@problems) } | ||
17 | + end | ||
11 | end | 18 | end |
12 | 19 | ||
13 | end | 20 | end |
@@ -0,0 +1,42 @@ | @@ -0,0 +1,42 @@ | ||
1 | +class ProblemPresenter | ||
2 | + | ||
3 | + def initialize(model_or_collection) | ||
4 | + @model_or_collection = model_or_collection | ||
5 | + end | ||
6 | + | ||
7 | + def to_xml(options={}) | ||
8 | + as_json(options).to_xml(options) | ||
9 | + end | ||
10 | + | ||
11 | + def to_json(options={}) | ||
12 | + Yajl.dump(as_json(options)) | ||
13 | + end | ||
14 | + | ||
15 | + def as_json(options={}) | ||
16 | + if collection? | ||
17 | + @model_or_collection.map { |model| model_as_json(model, options) } | ||
18 | + else | ||
19 | + model_as_json(@model_or_collection, options) | ||
20 | + end | ||
21 | + end | ||
22 | + | ||
23 | + def collection? | ||
24 | + @model_or_collection.respond_to?(:each) | ||
25 | + end | ||
26 | + | ||
27 | + def model_as_json(problem, options={}) | ||
28 | + { | ||
29 | + app_id: problem.app_id, | ||
30 | + app_name: problem.app_name, | ||
31 | + environment: problem.environment, | ||
32 | + message: problem.message, | ||
33 | + where: problem.where, | ||
34 | + first_notice_at: problem.first_notice_at, | ||
35 | + last_notice_at: problem.last_notice_at, | ||
36 | + resolved: problem.resolved, | ||
37 | + resolved_at: problem.resolved_at, | ||
38 | + notices_count: problem.notices_count | ||
39 | + } | ||
40 | + end | ||
41 | + | ||
42 | +end |
app/views/api/v1/problems/index.rabl
config/initializers/rabl.rb
@@ -1,20 +0,0 @@ | @@ -1,20 +0,0 @@ | ||
1 | -Rabl.configure do |config| | ||
2 | - # Commented as these are defaults | ||
3 | - # config.cache_all_output = false | ||
4 | - # config.cache_sources = Rails.env != 'development' # Defaults to false | ||
5 | - # config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache | ||
6 | - # config.escape_all_output = false | ||
7 | - # config.json_engine = nil # Any multi\_json engines | ||
8 | - # config.msgpack_engine = nil # Defaults to ::MessagePack | ||
9 | - # config.bson_engine = nil # Defaults to ::BSON | ||
10 | - # config.plist_engine = nil # Defaults to ::Plist::Emit | ||
11 | - # config.include_json_root = true | ||
12 | - # config.include_msgpack_root = true | ||
13 | - # config.include_bson_root = true | ||
14 | - # config.include_plist_root = true | ||
15 | - # config.include_xml_root = false | ||
16 | - # config.include_child_root = true | ||
17 | - # config.enable_json_callbacks = false | ||
18 | - # config.xml_options = { :dasherize => true, :skip_types => false } | ||
19 | - # config.view_paths = [] | ||
20 | -end |
config/routes.rb
@@ -37,13 +37,13 @@ Errbit::Application.routes.draw do | @@ -37,13 +37,13 @@ Errbit::Application.routes.draw do | ||
37 | delete :unlink_issue | 37 | delete :unlink_issue |
38 | end | 38 | end |
39 | end | 39 | end |
40 | - | 40 | + |
41 | resources :deploys, :only => [:index] | 41 | resources :deploys, :only => [:index] |
42 | end | 42 | end |
43 | 43 | ||
44 | namespace :api do | 44 | namespace :api do |
45 | namespace :v1 do | 45 | namespace :v1 do |
46 | - resources :problems, :only => [:index] | 46 | + resources :problems, :only => [:index], :defaults => { :format => 'json' } |
47 | end | 47 | end |
48 | end | 48 | end |
49 | 49 |