-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
150 lines (131 loc) · 4.2 KB
/
server.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# server.rb
require 'sinatra'
require "sinatra/namespace"
require 'mongoid'
require 'rack'
require 'rack/cors'
# DB Setup
Mongoid.load! "mongoid.yml"
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :delete, :put, :options, :patch]
end
end
#Data Models
class Company
include Mongoid::Document
#all the variables that a company can contain
field :companyID, type: String
field :companyName, type: String
field :address, type: String
field :city, type: String
field :country, type: String
field :owners, type: String
field :email, type: String
field :phoneNumber, type: String
#validates makes sure that the user provide the correct information, the email and phoneNumber is optional
validates :companyID, presence: true
validates :companyName, presence: true
validates :address, presence: true
validates :city, presence: true
validates :country, presence: true
validates :owners, presence: true
#assigning a "primary key" I decided to make the companyID unique
index({ companyID: 'text'})
index({ companyID: 1},{unique: true,drop_dups: true ,name: "companyID_index"})
#sorting on the different fields
scope :companyName, -> (companyName) { where(companyName: /^#{companyName}/i) }
scope :companyID, -> (companyID) { where(companyID: /^#{companyID}/) }
scope :city, -> (city) { where(city: /^#{city}/i) }
scope :country, -> (country) { where(country: /^#{country}/i) }
scope :owners, -> (owners) { where(owners: /^#{owners}/i)}
end
#Serializers
class CompanySerializer
def initialize(company)
@company = company
end
def as_json(*)
data = {
id:@company.id.to_s,
companyID:@company.companyID,
companyName:@company.companyName,
address:@company.address,
city:@company.city,
country:@company.country,
owners:@company.owners,
email:@company.email,
phoneNumber:@company.phoneNumber
}
data[:errors] = @company.errors [email protected]?
data
end
end
#api namespace
namespace '/api/v1' do
before do
content_type 'application/json'
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['GET', 'POST', 'DELETE', 'PATCH']
end
#set :protection, false
#generates base_url and parse the request body
helpers do
def base_url
@base_url ||= "#{request.env['rack.url_scheme']}://{request.env['HTTP_HOST']}"
#found a bug in the @base_url which would return curl(52): Empty reply from server the "" fixed the problem.
""
end
def json_params
begin
JSON.parse(request.body.read)
rescue
halt 400,{message:'Invalid JSON'}.to_json
end
end
end
get '/companies/:companyID' do
index = params['companyID']
company = Company.where(companyID: index).first
halt(404, {message:'The company you are searching for do not exist'}.to_json) unless company
CompanySerializer.new(company).to_json
end
#returns all created companies
get '/companies' do
companies = Company.all
[:companyName, :companyID, :city, :country, :owners].each do |filter|
companies = companies.send(filter, params[filter]) if params[filter]
end
companies.map {|company| CompanySerializer.new(company)}.to_json
end
#create a new company
post '/companies' do
company = Company.new(json_params)
if company.save
response.headers['Location'] = "#{base_url}/api/v1/company/#{company.companyID}"
status 201
else
status 422
body CompanySerializer.new(company).to_json
end
end
#updates the specific company and the client can update a specific field
patch '/companies/:companyID' do |companyID|
company = Company.where(companyID: companyID).first
halt(404, {message:'The company you are searching for do not exist'}.to_json) unless company
if company.update_attributes(json_params)
CompanySerializer.new(company).to_json
else
status 422
body CompanySerializer.new(company).to_json
end
end
delete '/companies/:companyID' do
response.headers['Location'] = "deleted"
tempCompanyID = params['companyID']
company = Company.where(companyID: tempCompanyID).first
company.destroy
end
end
#Endpoint