-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathapi.rb
90 lines (76 loc) · 2.24 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'grape'
require 'versionomy'
require 'geocoder'
require 'cgi'
require_relative 'app/entities/vendor'
require_relative 'app/models/vendor/vendor'
require_relative 'lib/broker/broker'
module Profiles
class API < Grape::API
version 'v1', using: :header, vendor: 'paasfinder'
format :json
prefix :api
resource :vendors do
desc 'Returns all vendors'
get do
present Vendor.all
end
desc 'Returns a vendor.'
params do
requires :name, type: String, desc: 'Vendor name.'
end
get ':name' do
begin
vendor = Vendor.find_by(name: /\A#{params[:name].tr('_', '.')}\z/i)
present vendor
rescue Exception
error! 'Vendor not found', 404
end
end
get ':name/infrastructures' do
name = params[:name].tr('_', '.')
# TODO: move to main configuration file
Geocoder.configure(
timeout: 5,
use_https: true,
api_key: ENV['GEO_KEY']
)
markers = []
vendor = Vendor.where(name: /#{name}/i).only(:infrastructures).first
unless vendor.nil? || vendor['infrastructures'].blank?
vendor['infrastructures'].each do |infra|
next if infra['region'].blank?
begin
dc = Datacenter.find_by(region: infra['region'], country: infra['country'])
markers << { latLng: dc.coordinates, name: dc.to_s }
rescue Exception
coord = Geocoder.coordinates("#{infra['region']}, #{infra['country']}")
markers << { latLng: coord, name: infra['region'] }
end
end
end
markers
end
end
post '/broker' do
begin
data = JSON.parse(request.body.read)
# match this vendor
result = Broker.new.match(data)
present result
status 200
rescue JSON::ParserError => e
error! "JSON request has a bad format #{e}", 400
rescue StandardError => e
error! "Matching error #{e}", 500
end
end
get '/infrastructures' do
markers = []
Datacenter.all.each do |center|
markers << { latLng: center.coordinates, name: center.to_s }
end
markers
end
end
end