-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
executable file
·137 lines (119 loc) · 3.27 KB
/
app.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
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sinatra/base'
require 'haml'
require 'json'
require 'date'
require 'time'
require 'mongo'
class SinatraApiProvider < Sinatra::Base
#require './helpers/render_partial'
def initialize(app = nil, params = {})
super(app)
@mongo = Mongo::Connection.new('localhost', 27017)
@db = @mongo.db('houseapi')
@root = Sinatra::Application.environment == :production ? '/api/' : '/'
end
def logger
env['app.logger'] || env['rack.logger']
end
def period_parser
if @params['period']
case @params['period']
when "day"
period = 1
when "week"
period = 7
when "month"
period = 30
else
period = 0
end
from = Time.parse((Date.today - period).strftime("%Y%m%d"))
from += from.utc_offset
from = from.utc
@query_params[:time] = {"$gt" => from}
end
end
def time_parser
if @params['from']
begin
from = Time.strptime(@params['from'], "%Y%m%d%H%M%S")
from += from.utc_offset
from = from.utc
rescue ArgumentError
from = nil
end
else
from = nil
end
if @params['to']
begin
to = Time.strptime(@params['to'], "%Y%m%d%H%M%S")
to += to.utc_offset
to = to.utc
rescue ArgumentError
to = nil
end
else
to = nil
end
if from and to
@query_params[:time] = {"$gt" => from , "$lt" => to}
end
end
def limit_parser
if @params['limit'] == 0
@limit_params[:limit] = nil
elsif @params['limit']
@limit_params[:limit] = @params['limit'].to_i
end
end
def sort_parser
if @params['sort'] == "asc"
@sort_params[:time] = :asc
end
end
def finder
@query_params = {}
@limit_params = {:limit => 1}
@sort_params = {:time => :desc}
period_parser
time_parser
limit_parser
sort_parser
@coll.find(@query_params, @limit_params).sort(@sort_params)
end
def query(coll_name)
@coll = @db.collection(coll_name)
@params = Rack::Utils.parse_query(@env['rack.request.query_string'])
finder.to_a
end
# Root Index
get '/' do
haml :index
end
# Generic Routing
get '/:tag_h/:tag_f' do
content_type :json, :charset => 'utf-8'
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST"
result = query([@params[:tag_h], @params[:tag_f]].join("."))
result.length == 1 ? result.last.to_json : result.to_json
end
get '/:tag_h/:tag_m/:tag_f' do
content_type :json, :charset => 'utf-8'
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST"
result = query([@params[:tag_h], @params[:tag_m], @params[:tag_f]].join("."))
result.length == 1 ? result.last.to_json : result.to_json
end
get '/:tag_1/:tag_2/:tag_3/:tag_4' do
content_type :json, :charset => 'utf-8'
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST"
result = query([@params[:tag_1], @params[:tag_2], @params[:tag_3], @params[:tag_4]].join("."))
result.length == 1 ? result.last.to_json : result.to_json
end
run! if app_file == $0
end