Skip to content

Commit

Permalink
Fixed those ugly spaces messed with spaces problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
steventen committed Apr 22, 2015
1 parent fcc90f9 commit a42b6a3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 99 deletions.
100 changes: 50 additions & 50 deletions grape-example/grape-on-rack/api.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
module MySite
class Project < ActiveRecord::Base
belongs_to :company
end

class Company < ActiveRecord::Base
has_many :projects
end

class API < Grape::API
format :json
default_format :json
# prefix 'api'
cascade false
default_error_formatter :json

helpers do
def current_company
key = params[:key]
@current_company ||= Company.where(:api => key).first
end

def authenticate!
error!({ "status" => "Fail", "error_message" => "Bad Key" }, 401) unless current_company
end
end

rescue_from :all do |e|
Rack::Response.new({ "status" => "Fail", "error_message" => e.message }.to_json, 405)
end

before do
authenticate!
end

get "projects" do
projects = current_company.projects
present :data, projects, :with => APIEntities::Project
present :status, "Success"
end

get "projects/:id" do
project = current_company.projects.where(id: params[:id]).first
if project
{"data" => {"id" => project.id, "name" => project.name}, "status" => "Success"}
else
# error!({ "status" => "Fail", "error_message" => "Failed to save project" }, 404)
{ "status" => "Fail", "error_message" => "Failed to save project" }
end
end
end
class Project < ActiveRecord::Base
belongs_to :company
end

class Company < ActiveRecord::Base
has_many :projects
end

class API < Grape::API
format :json
default_format :json
# prefix 'api'
cascade false
default_error_formatter :json

helpers do
def current_company
key = params[:key]
@current_company ||= Company.where(:api => key).first
end

def authenticate!
error!({ "status" => "Fail", "error_message" => "Bad Key" }, 401) unless current_company
end
end

rescue_from :all do |e|
Rack::Response.new({ "status" => "Fail", "error_message" => e.message }.to_json, 405)
end

before do
authenticate!
end

get "projects" do
projects = current_company.projects
present :data, projects, :with => APIEntities::Project
present :status, "Success"
end

get "projects/:id" do
project = current_company.projects.where(id: params[:id]).first
if project
{"data" => {"id" => project.id, "name" => project.name}, "status" => "Success"}
else
# error!({ "status" => "Fail", "error_message" => "Failed to save project" }, 404)
{ "status" => "Fail", "error_message" => "Failed to save project" }
end
end
end
end
12 changes: 6 additions & 6 deletions grape-example/grape-on-rack/entities.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module MySite
module APIEntities
class Project < Grape::Entity
expose :id
expose :name
end
end
module APIEntities
class Project < Grape::Entity
expose :id
expose :name
end
end
end
72 changes: 36 additions & 36 deletions grape-example/grape-on-rails/app/api/api.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
require "entities"
module GrapeApiRails
class API < Grape::API
format :json
default_format :json
# prefix 'api'
cascade false
default_error_formatter :json
class API < Grape::API
format :json
default_format :json
# prefix 'api'
cascade false
default_error_formatter :json

helpers do
def current_company
key = params[:key]
@current_company ||= Company.where(:api => key).first
end
helpers do
def current_company
key = params[:key]
@current_company ||= Company.where(:api => key).first
end

def authenticate!
error!({ "status" => "Fail", "error_message" => "Bad Key" }, 401) unless current_company
end
end
def authenticate!
error!({ "status" => "Fail", "error_message" => "Bad Key" }, 401) unless current_company
end
end

rescue_from :all do |e|
Rack::Response.new({ "status" => "Fail", "error_message" => e.message }.to_json, 405)
end
rescue_from :all do |e|
Rack::Response.new({ "status" => "Fail", "error_message" => e.message }.to_json, 405)
end

before do
authenticate!
end
before do
authenticate!
end

get "projects" do
projects = current_company.projects
present :data, projects, :with => APIEntities::Project
present :status, "Success"
end
get "projects" do
projects = current_company.projects
present :data, projects, :with => APIEntities::Project
present :status, "Success"
end

get "projects/:id" do
project = current_company.projects.where(id: params[:id]).first
if project
{"data" => {"id" => project.id, "name" => project.name}, "status" => "Success"}
else
# error!({ "status" => "Fail", "error_message" => "Failed to save project" }, 404)
{ "status" => "Fail", "error_message" => "Failed to save project" }
end
end
end
get "projects/:id" do
project = current_company.projects.where(id: params[:id]).first
if project
{"data" => {"id" => project.id, "name" => project.name}, "status" => "Success"}
else
# error!({ "status" => "Fail", "error_message" => "Failed to save project" }, 404)
{ "status" => "Fail", "error_message" => "Failed to save project" }
end
end
end
end
12 changes: 6 additions & 6 deletions grape-example/grape-on-rails/app/api/entities.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module GrapeApiRails
module APIEntities
class Project < Grape::Entity
expose :id
expose :name
end
end
module APIEntities
class Project < Grape::Entity
expose :id
expose :name
end
end
end
2 changes: 1 addition & 1 deletion grape-example/grape-on-rails/app/models/company.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Company < ActiveRecord::Base
has_many :projects
has_many :projects
end

0 comments on commit a42b6a3

Please sign in to comment.