-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.rb
53 lines (46 loc) · 1.52 KB
/
migration.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
# frozen_string_literal: true
require 'rack'
require 'mustermann'
require 'pragma/migration/base'
require 'pragma/migration/repository'
require 'pragma/migration/version'
require 'pragma/migration/runner'
require 'pragma/migration/bond'
require 'pragma/migration/middleware'
require 'pragma/migration/hooks/operation'
require 'pragma/migration/gem_version'
module Pragma
# Provides API payload migrations to support clients on older versions of your API.
module Migration
# The default +user_version_proc+.
DEFAULT_USER_VERSION_PROC = lambda do |request|
request.get_header('X-Api-Version')
end
class << self
# @!attribute [rw] repository
# @return [Pragma::Migration::Repository] your migrations repository
#
# @!attribute [rw] user_version_proc
# @return [Object] a callable taking a +Rack::Request+ as argument and returning an API
# version identifier
attr_accessor :repository
attr_writer :user_version_proc
def user_version_proc
@user_version_proc ||= DEFAULT_USER_VERSION_PROC
end
# Returns the user's API version for the given request.
#
# @param request [Rack::Request] the request
#
# @return [String] the API version identifier
def user_version_from(request)
version = user_version_proc.call(request)
if version && repository.sorted_versions.include?(version)
version
else
repository.sorted_versions.last.number
end
end
end
end
end