Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Extend API #717

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class API::V1::ApiController < ApplicationController
respond_to :json, :xml

# The stats API only requires an api_key for the given app.
skip_before_filter :authenticate_user!
before_filter :require_api_key_or_authenticate_user!

protected

def require_api_key_or_authenticate_user!
if params[:api_key].present?
if @app = App.where(:api_key => params[:api_key]).first
return true
end
end

authenticate_user!
end
end
9 changes: 9 additions & 0 deletions app/controllers/api/v1/apps_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class API::V1::AppsController < API::V1::ApiController
def index
@apps = App.all
end

def show
@app = App.find(params[:id])
end
end
22 changes: 1 addition & 21 deletions app/controllers/api/v1/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
class Api::V1::StatsController < ApplicationController
respond_to :json, :xml

# The stats API only requires an api_key for the given app.
skip_before_filter :authenticate_user!
before_filter :require_api_key_or_authenticate_user!

class API::V1::StatsController < API::V1::ApiController
def app
if problem = @app.problems.order_by(:last_notice_at.desc).first
@last_error_time = problem.last_notice_at
Expand All @@ -22,18 +16,4 @@ def app
format.xml { render :xml => stats }
end
end


protected

def require_api_key_or_authenticate_user!
if params[:api_key].present?
if @app = App.where(:api_key => params[:api_key]).first
return true
end
end

authenticate_user!
end

end
1 change: 1 addition & 0 deletions app/views/api/v1/apps/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.apps @apps, :id, :name, :github_repo
4 changes: 4 additions & 0 deletions app/views/api/v1/apps/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.(@app, :id, :name)
json.errors @app.problems do |json, problem|
json.(problem, :message, :last_noticed_at, :notices_count)
end
13 changes: 3 additions & 10 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format
# (all these examples are active by default):
# ActiveSupport::Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'API'
end

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
namespace :v1 do
resources :problems, :only => [:index, :show], :defaults => { :format => 'json' }
resources :notices, :only => [:index], :defaults => { :format => 'json' }
resources :apps, :only => [:index, :show], :defaults => { :format => 'json' }
resources :stats, :only => [], :defaults => { :format => 'json' } do
collection do
get :app
Expand Down