Last active
August 29, 2015 14:11
-
-
Save i5okie/c30770dbb9218a126548 to your computer and use it in GitHub Desktop.
logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class API::V1::BaseController < Api::BaseController | |
protect_from_forgery with: :null_session | |
before_action :set_resource, only: [:destroy, :show, :update] | |
respond_to :json | |
# POST /api/{plural_resource_name} | |
def create | |
set_resource(resource_class.new(resource_params)) | |
if get_resource.save | |
render :show, status: :created | |
else | |
render json: get_resource.errors, status: :unprocessable_entity | |
end | |
end | |
# DELETE /api/{plural_resource_name}/1 | |
def destroy | |
get_resource.destroy | |
head :no_content | |
end | |
# GET /api/{plural_resource_name} | |
def index | |
plural_resource_name = "@#{resource_name.pluralize}" | |
resources = resource_class.where(query_params) | |
.page(page_params[:page]) | |
.per(page_params[:page_size]) | |
instance_variable_set(plural_resource_name, resources) | |
respond_with instance_variable_get(plural_resource_name) | |
end | |
# GET /api/{plural_resource_name}/1 | |
def show | |
respond_with get_resource | |
end | |
# PATCH/PUT /api/{plural_resource_name}/1 | |
def update | |
if get_resource.update(resource_params) | |
render :show | |
else | |
render json: get_resource.errors, status: :unprocessable_entity | |
end | |
end | |
private | |
# Returns the resource from the created instance variable | |
# @return [Object] | |
def get_resource | |
instance_variable_get("@#{resource_name}") | |
end | |
# Returns the allowed parameters for searching | |
# Override this method in each API controller | |
# to permit additional parameters to search on | |
# @return [Hash] | |
def query_params | |
{} | |
end | |
# Returns the allowed parameters for pagination | |
# @return [Hash] | |
def page_params | |
params.permit(:page, :page_size) | |
end | |
# The resource class based on the controller | |
# @return [Class] | |
def resource_class | |
@resource_class ||= resource_name.classify.constantize | |
end | |
# The singular name for the resource class based on the controller | |
# @return [String] | |
def resource_name | |
@resource_name ||= self.controller_name.singularize | |
end | |
# Only allow a trusted parameter "white list" through. | |
# If a single resource is loaded for #create or #update, | |
# then the controller for the resource must implement | |
# the method "#{resource_name}_params" to limit permitted | |
# parameters for the individual model. | |
def resource_params | |
@resource_params ||= self.send("#{resource_name}_params") | |
end | |
# Use callbacks to share common setup or constraints between actions. | |
def set_resource(resource = nil) | |
resource ||= resource_class.find(params[:id]) | |
instance_variable_set("@#{resource_name}", resource) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class API::V1::LocationsController < API::V1::BaseController | |
def index | |
@locations = Location.all | |
respond_to do |format| | |
format.json { render :json => @locations } | |
end | |
end | |
private | |
def location_params | |
params.require(:location).permit(:name) | |
end | |
def query_params | |
params.permit(:location_id, :name) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Started GET "/v1/locations?customer_id=2" for 50.66.32.121 at 2014-12-10 05:53:13 +0000 | |
Processing by API::V1::LocationsController#index as JSON | |
Parameters: {"customer_id"=>"2", "subdomain"=>"api.ship"} | |
[1m[36mLocation Load (0.8ms)[0m [1mSELECT "locations".* FROM "locations"[0m | |
Completed 200 OK in 11ms (Views: 6.3ms | ActiveRecord: 0.8ms) | |
Started GET "/v1/locations.json" for 50.66.32.121 at 2014-12-10 05:53:34 +0000 | |
Processing by API::V1::LocationsController#index as JSON | |
Parameters: {"subdomain"=>"api.ship"} | |
[1m[35mLocation Load (1.0ms)[0m SELECT "locations".* FROM "locations" | |
Completed 200 OK in 8ms (Views: 4.1ms | ActiveRecord: 1.0ms) | |
Started GET "/v1/locations.json" for 50.66.32.121 at 2014-12-10 05:53:52 +0000 | |
Processing by API::V1::LocationsController#index as JSON | |
Parameters: {"subdomain"=>"api.ship"} | |
[1m[36mLocation Load (0.5ms)[0m [1mSELECT "locations".* FROM "locations"[0m | |
Completed 200 OK in 5ms (Views: 2.7ms | ActiveRecord: 0.5ms) | |
Started GET "/v1/locations.json" for 50.66.32.121 at 2014-12-10 05:54:05 +0000 | |
Processing by API::V1::LocationsController#index as JSON | |
Parameters: {"subdomain"=>"api.ship"} | |
[1m[35mLocation Load (0.5ms)[0m SELECT "locations".* FROM "locations" | |
Completed 200 OK in 5ms (Views: 2.9ms | ActiveRecord: 0.5ms) | |
Started GET "/v1/location" for 50.66.32.121 at 2014-12-10 05:54:13 +0000 | |
ActionController::RoutingError (No route matches [GET] "/v1/location"): | |
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' | |
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app' | |
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged' | |
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' | |
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' | |
rack (1.5.2) lib/rack/runtime.rb:17:in `call' | |
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' | |
rack (1.5.2) lib/rack/lock.rb:17:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call' | |
rack (1.5.2) lib/rack/sendfile.rb:112:in `call' | |
railties (4.1.6) lib/rails/engine.rb:514:in `call' | |
railties (4.1.6) lib/rails/application.rb:144:in `call' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:576:in `process_client' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:670:in `worker_loop' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:525:in `spawn_missing_workers' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:140:in `start' | |
unicorn (4.8.3) bin/unicorn:126:in `<top (required)>' | |
/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn:23:in `load' | |
/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn:23:in `<main>' | |
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval' | |
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>' | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (5.0ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (9.4ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (68.7ms) | |
Started GET "/v1/location?id=1" for 50.66.32.121 at 2014-12-10 05:54:28 +0000 | |
ActionController::RoutingError (No route matches [GET] "/v1/location"): | |
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' | |
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app' | |
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged' | |
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' | |
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' | |
rack (1.5.2) lib/rack/runtime.rb:17:in `call' | |
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' | |
rack (1.5.2) lib/rack/lock.rb:17:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call' | |
rack (1.5.2) lib/rack/sendfile.rb:112:in `call' | |
railties (4.1.6) lib/rails/engine.rb:514:in `call' | |
railties (4.1.6) lib/rails/application.rb:144:in `call' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:576:in `process_client' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:670:in `worker_loop' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:525:in `spawn_missing_workers' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:140:in `start' | |
unicorn (4.8.3) bin/unicorn:126:in `<top (required)>' | |
/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn:23:in `load' | |
/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn:23:in `<main>' | |
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval' | |
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>' | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (6.4ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (15.7ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (69.6ms) | |
Started GET "/v1/location.json?id=1" for 50.66.32.121 at 2014-12-10 05:54:33 +0000 | |
ActionController::RoutingError (No route matches [GET] "/v1/location.json"): | |
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' | |
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app' | |
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged' | |
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged' | |
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' | |
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' | |
rack (1.5.2) lib/rack/runtime.rb:17:in `call' | |
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' | |
rack (1.5.2) lib/rack/lock.rb:17:in `call' | |
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call' | |
rack (1.5.2) lib/rack/sendfile.rb:112:in `call' | |
railties (4.1.6) lib/rails/engine.rb:514:in `call' | |
railties (4.1.6) lib/rails/application.rb:144:in `call' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:576:in `process_client' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:670:in `worker_loop' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:525:in `spawn_missing_workers' | |
unicorn (4.8.3) lib/unicorn/http_server.rb:140:in `start' | |
unicorn (4.8.3) bin/unicorn:126:in `<top (required)>' | |
/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn:23:in `load' | |
/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn:23:in `<main>' | |
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval' | |
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>' | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (4.6ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (8.2ms) | |
Rendered /usr/local/rvm/gems/ruby-2.1.3/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (57.4ms) | |
Started GET "/v1/locations.json" for 50.66.32.121 at 2014-12-10 05:55:28 +0000 | |
Processing by API::V1::LocationsController#index as JSON | |
Parameters: {"subdomain"=>"api.ship"} | |
[1m[36mLocation Load (1.0ms)[0m [1mSELECT "locations".* FROM "locations"[0m | |
Completed 200 OK in 10ms (Views: 5.8ms | ActiveRecord: 1.0ms) | |
Started GET "/v1/customers.json" for 50.66.32.121 at 2014-12-10 05:55:44 +0000 | |
Processing by API::V1::CustomersController#index as JSON | |
Parameters: {"subdomain"=>"api.ship"} | |
[1m[36mCustomer Load (1.5ms)[0m [1mSELECT "customers".* FROM "customers"[0m | |
Completed 200 OK in 35ms (Views: 15.2ms | ActiveRecord: 6.1ms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment