This repository was archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.rb
More file actions
93 lines (81 loc) · 2.75 KB
/
api.rb
File metadata and controls
93 lines (81 loc) · 2.75 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
83
84
85
86
87
88
89
90
91
92
93
#encoding: utf-8
$stdout.sync = true
require "rubygems"
require "bundler/setup"
require "grape"
require "json"
require "rack/contrib" # needed for RACK::JSONP
require_relative "./config/init.rb"
# trap all exceptions and fail gracefuly with a 500 and a proper message
class ApiErrorHandler < Grape::Middleware::Base
def logger
logger = Logger.new(File.expand_path("../logs/#{ENV['RACK_ENV']}.log", __FILE__))
end
def call!(env)
@env = env
begin
@app.call(@env)
rescue Exception => e
logger.error "#{e}"
throw :error, :message => e.message || options[:default_message], :status => 500
end
end
end
module API
# Custom validators
class Email < Grape::Validations::SingleOptionValidator
def validate_param!(attr_name, params)
unless params[attr_name] =~ /[[:ascii:]]+@[[:ascii:]]+\.[[:ascii:]]{2,4}/
throw :error, :status => 400, :message => "#{attr_name}: must be a valid email"
end
end
end
class Length < Grape::Validations::SingleOptionValidator
def validate_param!(attr_name, params)
unless params[attr_name].length >= @option
throw :error, :status => 400, :message => "#{attr_name}: must be at least #{@option} characters long"
end
end
end
class Root < Grape::API
use ApiErrorHandler
use Rack::JSONP
helpers do
def logger
logger = Logger.new(File.expand_path("../logs/#{ENV['RACK_ENV']}.log", __FILE__))
end
end
# load all external api routes
Dir[File.dirname(__FILE__) + '/api/*.rb'].each do |file|
require file
end
version 'v1', :using => :header, :vendor => 'deichman.no'
prefix 'api'
rescue_from :all, :backtrace => true
format :json
default_format :json
mount API::Reviews
mount API::Works
mount API::Users
mount API::Sources
before do
# Of course this makes the request.body unavailable afterwards.
# You can just use a helper method to store it away for later if needed.
logger.info "#{env}"
logger.info "#{env['REMOTE_ADDR']} #{env['HTTP_USER_AGENT']} #{env['REQUEST_METHOD']} #{env['REQUEST_PATH']} -- Request: #{request.body.read}"
# strip out empty params
#params.remove_empty_params!
end
# Rescue and log validation errors gracefully
# NB: Grape::Exceptions::ValidationError changes to Grape::Exceptions::Validation in future Grape releases!
rescue_from Grape::Exceptions::ValidationError do |e|
logger = Logger.new(File.expand_path("../logs/#{ENV['RACK_ENV']}.log", __FILE__))
logger.error "#{e.message}"
Rack::Response.new(MultiJson.encode(
'status' => e.status,
'message' => e.message,
'param' => e.param),
e.status)
end
end
end