-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
146 lines (114 loc) · 3.31 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
# server.rb
require 'sinatra'
require "sinatra/namespace"
require "sinatra/cors"
require 'mongoid'
set :allow_origin, "*"
set :allow_methods, "GET,HEAD,POST,DELETE,OPTIONS"
set :allow_headers, "content-type,if-modified-since"
set :expose_headers, "location,link"
# MongoDB
Mongoid.load!("mongoid.config", :development)
# Company model
class Company
include Mongoid::Document
field :cvr, type: Integer
field :company_name, type: String
field :address, type: String
field :city, type: String
field :country, type: String
field :phone_number, type: Integer
validates :cvr, presence: true
validates :company_name, presence: true
validates :address, presence: true
validates :city, presence: true
validates :country, presence: true
validates :phone_number, presence: false
index({ cvr: 1 }, { unique: true, name: "cvr_index" })
index({ company_name: 'text' })
scope :cvr, -> (cvr) { where(cvr: cvr) }
scope :company_name, -> (company_name) { where(company_name: /^#{company_name}/) }
scope :address, -> (address) { where(address: /^#{address}/) }
scope :city, -> (city) { where(city: /^#{city}/) }
scope :country, -> (country) { where(country: /^#{country}/) }
scope :phone_number, -> (phone_number) { where(phone_number: /^#{phone_number}/) }
end
class CompanySerializer
def initialize(company)
@company = company
end
def as_json(*)
data = {
id: @company.id.to_s,
cvr: @company.cvr,
company_name: @company.company_name,
address: @company.address,
city: @company.city,
country: @company.country,
phone_number: @company.phone_number,
}
data[:errors] = @company.errors if @company.errors.any?
data
end
end
# Start Endpoints
get '/' do
'Hi, i am a company api'
end
namespace '/api/v1' do
before do
content_type 'application/json'
end
helpers do
def base_url
@base_url ||= "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
end
def json_params
begin
JSON.parse(request.body.read)
rescue
halt 400, { message: 'Invalid JSON' }.to_json
end
end
def company
@company ||= Company.where(id: params[:id]).first
end
def halt_if_not_found!
halt(404, { message: 'Company Not Found'}.to_json) unless company
end
def serialize(company)
CompanySerializer.new(company).to_json
end
end
#Search all companies
get '/companies' do
companies = Company.all
[:cvr, :company_name, :address, :city, :address, :phone_number].each do |filter|
companies = companies.send(filter, params[filter]) if params[filter]
end
companies.map { |company| CompanySerializer.new(company) }.to_json
end
#Get company by ID
get '/companies/:id' do |id|
halt_if_not_found!
serialize(company)
end
#Create new company
post '/companies' do
company = Company.new(json_params)
halt 422, serialize(company) unless company.save
response.headers['Location'] = "#{base_url}/api/v1/companies/#{company.id}"
status 201
end
#Edit company
patch '/companies/:id' do |id|
halt_if_not_found!
halt 422, serialize(company) unless company.update_attributes(json_params)
serialize(company)
end
#Delete company
delete '/companies/:id' do |id|
company.destroy if company
status 204
end
end