-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
82 lines (67 loc) · 1.66 KB
/
main.rb
File metadata and controls
82 lines (67 loc) · 1.66 KB
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
# encoding: Windows-1252
require 'bundler/setup'
require "json"
Bundler.require
require 'sinatra/sequel'
Sequel.connect('postgres://127.0.0.1/samsheff')
class Company < Sequel::Model
def to_json
{
name: name,
short_name: short_name,
inn: inn,
type: type
}.to_json
end
end
class Region < Sequel::Model
def to_json
{
name: name,
type: type,
oblast_code: oblast_code
}.to_json
end
end
class Street < Sequel::Model
end
class Activity < Sequel::Model
def to_json
{
id: id,
code: code,
name: name
}.to_json
end
end
configure do
set :database, 'postgres://127.0.0.1/samsheff'
end
before do
content_type 'application/json; charset=utf-8'
use_cross_origin
end
get "/" do
{ here: "Yes." }.to_json
end
get "/region/:oblast_id/markets/:query" do
ids_array = database["SELECT id from activities WHERE name LIKE '#{params[:query]}%'"].to_a
ids = []
ids_array.each { |id| ids << id[:id] }
database["SELECT COUNT(*) as companies_count, SUM(revenue) as revenue_sum, SUM(profit) as profit_sum FROM companies WHERE activities @> '{#{ids.join(',')}}'::int[] #{"AND oblast_id = #{params[:oblast_id]}" if params[:oblast_id].to_i != 0}"].to_a.to_json
end
get "/companies/:company_id" do
Company[params[:company_id]].to_json
end
get "/markets/:market_id/companies" do
database["SELECT * from companies WHERE activities @> '{#{params[:market_id]}}'::int[]"].to_a.to_json
end
get "/regions" do
database[:oblasts].to_a.to_json
end
# JSONP Cross Origin Setup
def use_cross_origin
cross_origin :allow_origin => '*',
:allow_methods => [:get],
:expose_headers => ['Content-Type', ]
end