Skip to content

Commit

Permalink
API on Projects creation implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDenisov committed Aug 31, 2012
1 parent 65abd8b commit c1173e2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/api/projects.rb
Expand Up @@ -23,6 +23,27 @@ class Projects < Grape::API
present user_project, with: Entities::Project
end

# Create new project
#
# Parameters:
# name (required) - name for new project
# code (optional) - code for new project, if not set used name
# path (optional) - oath for new project, if not set used name
# Example Request
# POST /projects
post do
project = {}
project[:name] = params[:name]
project[:code] = params[:code] || project[:name]
project[:path] = params[:path] || project[:name]
@project = Project.create_by_user(project, current_user)
if @project.saved?
present @project, with: Entities::Project
else
error!({'message' => '404 Not found'}, 404)
end
end

# Get a project repository branches
#
# Parameters:
Expand Down
37 changes: 37 additions & 0 deletions spec/requests/api/projects_spec.rb
Expand Up @@ -25,6 +25,43 @@
end
end

describe "POST /projects" do
it "should create new project without code and path" do
lambda {
name = "foo"
post api("/projects", user), {
name: name
}
response.status.should == 201
json_response["name"].should == name
json_response["code"].should == name
json_response["path"].should == name
}.should change{Project.count}.by(1)
end
it "should create new project" do
lambda {
name = "foo"
path = "bar"
code = "bazz"
post api("/projects", user), {
code: code,
path: path,
name: name
}
response.status.should == 201
json_response["name"].should == name
json_response["path"].should == path
json_response["code"].should == code
}.should change{Project.count}.by(1)
end
it "should not create project without name" do
lambda {
post api("/projects", user)
response.status.should == 404
}.should_not change{Project.count}
end
end

describe "GET /projects/:id" do
it "should return a project by id" do
get api("/projects/#{project.id}", user)
Expand Down

0 comments on commit c1173e2

Please sign in to comment.