-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed those ugly spaces messed with spaces problems.
- Loading branch information
Showing
5 changed files
with
99 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |