forked from archangel519/Burndown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
66 lines (55 loc) · 1.68 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require File.join(File.dirname(__FILE__), "system/api_response.rb")
require File.join(File.dirname(__FILE__), "system/api_error.rb")
require File.join(File.dirname(__FILE__), "models/story.rb")
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "burndown",
:password => "HappyBunnyFuzzySquirrel",
:database => "burndown"
)
class FireBurn < Grape::API
version 'v1'
format :json
default_format :json
rescue_from :all do |e|
response = APIResponse.new()
response.error = APIError.new('System Exception', 500, e.message)
Rack::Response.new([ response.toJSON ], 200, { "Content-type" => "application/json" }).finish
end
resource :stories do
post {
name = params[:name]
points = params[:points].to_i
description = params[:description]
story = Story.create(:name => name, :points => points, :description => description)
response = APIResponse.new(story)
response.toJSON
}
get {
stories = Story.all()
response = APIResponse.new(stories)
response.toJSON
}
put ":id" do
name = params[:name]
points = params[:points].to_i
description = params[:description]
story = Story.find(params[:id])
story.name = name
story.points = points
story.description = description
story.save
response = APIResponse.new(story)
response.toJSON
end
get ":id" do
response = APIResponse.new(Story.find(params[:id]))
response.toJSON
end
delete ":id" do
response = APIResponse.new(Story.delete(params[:id]))
response.toJSON
end
end
end