Commit 5fb5c16f0db065e1adced925e2d379c2e41928bc

Authored by Robert Lail
1 parent f26b61cd
Exists in master and in 1 other branch production

use YAJL directly to render responses to problems API rather than using RABL

Gemfile
... ... @@ -38,8 +38,7 @@ platform :ruby do
38 38 end
39 39  
40 40 gem 'ri_cal'
41   -gem 'rabl'
42   -gem 'yajl-ruby'
  41 +gem 'yajl-ruby', :require => "yajl"
43 42  
44 43 group :development, :test do
45 44 gem 'rspec-rails', '~> 2.6'
... ...
Gemfile.lock
... ... @@ -193,9 +193,6 @@ GEM
193 193 coderay (~> 1.0.5)
194 194 method_source (~> 0.7.1)
195 195 slop (>= 2.4.4, < 3)
196   - rabl (0.6.13)
197   - activesupport (>= 2.3.14)
198   - multi_json (~> 1.0)
199 196 rack (1.4.1)
200 197 rack-cache (1.2)
201 198 rack (>= 0.4)
... ... @@ -325,7 +322,6 @@ DEPENDENCIES
325 322 oruen_redmine_client
326 323 pivotal-tracker
327 324 pry
328   - rabl
329 325 rack-ssl-enforcer
330 326 rails (= 3.2.6)
331 327 rails_autolink (~> 1.0.9)
... ...
app/controllers/api/v1/problems_controller.rb
1 1 class Api::V1::ProblemsController < ApplicationController
  2 + respond_to :json, :xml
2 3  
3 4 def index
4 5 if params.key?(:start_date) && params.key?(:end_date)
... ... @@ -8,6 +9,12 @@ class Api::V1::ProblemsController &lt; ApplicationController
8 9 else
9 10 @problems = Problem.all
10 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 18 end
12 19  
13 20 end
... ...
app/presenters/problem_presenter.rb 0 → 100644
... ... @@ -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
... ... @@ -1,12 +0,0 @@
1   -collection @problems
2   -
3   -attributes :app_id,
4   - :app_name,
5   - :environment,
6   - :message,
7   - :where,
8   - :first_notice_at,
9   - :last_notice_at,
10   - :resolved,
11   - :resolved_at,
12   - :notice_count
config/initializers/rabl.rb
... ... @@ -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 37 delete :unlink_issue
38 38 end
39 39 end
40   -
  40 +
41 41 resources :deploys, :only => [:index]
42 42 end
43 43  
44 44 namespace :api do
45 45 namespace :v1 do
46   - resources :problems, :only => [:index]
  46 + resources :problems, :only => [:index], :defaults => { :format => 'json' }
47 47 end
48 48 end
49 49  
... ...