ruby_event_store-browser 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92e89418ff5db0378e3a97a1f9ccd99a599ab315f1dd7bd5679735589a4a9a94
4
- data.tar.gz: bf9584207ddd6cd207d2876d93808006f708db225827d6b1cad5ee57e27bf877
3
+ metadata.gz: fe8aab269fcbe0762c1d524b4c139aad39cd31ce96e1cc8b9a961260f60f5a03
4
+ data.tar.gz: 6d74542a6032a09e4e0499b08e24b86d5eeed732a30426e5dc5ceb164a1b3362
5
5
  SHA512:
6
- metadata.gz: 373adb1044718a26f823e5d3978afbed086d7269a989c7e98b4b2d9de4e4fc090fe34840067fc5e8a1ee23f970ed8f7c230c523cc28ba468c86bbafcaf417446
7
- data.tar.gz: 43f37adb0303b86588b068aaca140e2ee4bace7c7a8ba8d58321b37c60dbcd47760ce8421f0dea1c5c5e637dab1aee647a27ebb681e82b23e14dea98abd471b2
6
+ metadata.gz: 8c56cc4e817a5897da2716449c0c003cf10c14bd58ac679be8d27ef3220c905179973b256d35aaa8929fac3ea72cdcf25e9f52ac535a8dd0b6adb59098e9f9ac
7
+ data.tar.gz: b8e1d073f84142d75d40490322c194919f58ac7b738cd2c2040ba780975d1dbecddc448dd5d4b8a0b53c666d09bbd55fb15d863dbf0b70779145b2834c810ba1
@@ -6,13 +6,14 @@ require 'sinatra/base'
6
6
  module RubyEventStore
7
7
  module Browser
8
8
  class App < Sinatra::Base
9
- def self.for(event_store_locator:, host: nil, path: nil, environment: :production, related_streams_query: DEFAULT_RELATED_STREAMS_QUERY)
9
+ def self.for(event_store_locator:, host: nil, path: nil, api_url: nil, environment: :production, related_streams_query: DEFAULT_RELATED_STREAMS_QUERY)
10
10
  self.tap do |app|
11
11
  app.settings.instance_exec do
12
12
  set :event_store_locator, event_store_locator
13
13
  set :related_streams_query, -> { related_streams_query }
14
14
  set :host, host
15
15
  set :root_path, path
16
+ set :api_url, api_url
16
17
  set :environment, environment
17
18
  set :public_folder, "#{__dir__}/../../../public"
18
19
  end
@@ -22,6 +23,7 @@ module RubyEventStore
22
23
  configure do
23
24
  set :host, nil
24
25
  set :root_path, nil
26
+ set :api_url, nil
25
27
  set :event_store_locator, -> {}
26
28
  set :related_streams_query, nil
27
29
  set :protection, except: :path_traversal
@@ -29,32 +31,7 @@ module RubyEventStore
29
31
  mime_type :json, 'application/vnd.api+json'
30
32
  end
31
33
 
32
- get '/' do
33
- erb %{
34
- <!DOCTYPE html>
35
- <html>
36
- <head>
37
- <title>RubyEventStore::Browser</title>
38
- <link type="text/css" rel="stylesheet" href="<%= path %>/ruby_event_store_browser.css">
39
- </head>
40
- <body>
41
- <script type="text/javascript" src="<%= path %>/ruby_event_store_browser.js"></script>
42
- <script type="text/javascript">
43
- RubyEventStore.Browser.Elm.Main.init({
44
- flags: {
45
- rootUrl: "<%= path %>",
46
- eventsUrl: "<%= path %>/events",
47
- streamsUrl: "<%= path %>/streams",
48
- resVersion: "<%= RubyEventStore::VERSION %>"
49
- }
50
- });
51
- </script>
52
- </body>
53
- </html>
54
- }, locals: { path: settings.root_path || request.script_name }
55
- end
56
-
57
- get '/events/:id' do
34
+ get '/api/events/:id' do
58
35
  begin
59
36
  json Event.new(
60
37
  event_store: settings.event_store_locator,
@@ -65,7 +42,7 @@ module RubyEventStore
65
42
  end
66
43
  end
67
44
 
68
- get '/streams/:id' do
45
+ get '/api/streams/:id' do
69
46
  json GetStream.new(
70
47
  stream_name: params[:id],
71
48
  routing: routing,
@@ -73,7 +50,7 @@ module RubyEventStore
73
50
  )
74
51
  end
75
52
 
76
- get '/streams/:id/relationships/events' do
53
+ get '/api/streams/:id/relationships/events' do
77
54
  json GetEventsFromStream.new(
78
55
  event_store: settings.event_store_locator,
79
56
  params: symbolized_params,
@@ -81,12 +58,28 @@ module RubyEventStore
81
58
  )
82
59
  end
83
60
 
84
- get '/streams/:id/relationships/events/:position/:direction/:count' do
85
- json GetEventsFromStream.new(
86
- event_store: settings.event_store_locator,
87
- params: symbolized_params,
88
- routing: routing,
89
- )
61
+ get %r{/(events/.*|streams/.*)?} do
62
+ erb %{
63
+ <!DOCTYPE html>
64
+ <html>
65
+ <head>
66
+ <title>RubyEventStore::Browser</title>
67
+ <link type="text/css" rel="stylesheet" href="<%= path %>/ruby_event_store_browser.css">
68
+ </head>
69
+ <body>
70
+ <script type="text/javascript" src="<%= path %>/ruby_event_store_browser.js"></script>
71
+ <script type="text/javascript">
72
+ RubyEventStore.Browser.Elm.Main.init({
73
+ flags: {
74
+ rootUrl: "<%= routing.root_url %>",
75
+ apiUrl: "<%= api_url %>",
76
+ resVersion: "<%= RubyEventStore::VERSION %>"
77
+ }
78
+ });
79
+ </script>
80
+ </body>
81
+ </html>
82
+ }, locals: { path: settings.root_path || request.script_name, api_url: settings.api_url || routing.api_url }
90
83
  end
91
84
 
92
85
  helpers do
@@ -84,11 +84,11 @@ module RubyEventStore
84
84
  end
85
85
 
86
86
  def count
87
- Integer(params.fetch(:count, PAGE_SIZE))
87
+ Integer(pagination_param[:count] || PAGE_SIZE)
88
88
  end
89
89
 
90
90
  def direction
91
- case params[:direction]
91
+ case pagination_param[:direction]
92
92
  when 'forward'
93
93
  :forward
94
94
  else
@@ -97,17 +97,21 @@ module RubyEventStore
97
97
  end
98
98
 
99
99
  def position
100
- case params[:position]
100
+ case pagination_param[:position]
101
101
  when 'head', nil
102
102
  HEAD
103
103
  else
104
- params.fetch(:position)
104
+ pagination_param.fetch(:position)
105
105
  end
106
106
  end
107
107
 
108
108
  def stream_name
109
109
  params.fetch(:id)
110
110
  end
111
+
112
+ def pagination_param
113
+ params[:page] || {}
114
+ end
111
115
  end
112
116
  end
113
117
  end
@@ -29,16 +29,8 @@ module RubyEventStore
29
29
 
30
30
  def metadata
31
31
  event.metadata.to_h.tap do |m|
32
- m[:timestamp] = as_time(m.fetch(:timestamp)).iso8601(3)
33
- end
34
- end
35
-
36
- def as_time(value)
37
- case value
38
- when String
39
- Time.parse(value)
40
- else
41
- value
32
+ m[:timestamp] = event.metadata.fetch(:timestamp).iso8601(TIMESTAMP_PRECISION)
33
+ m[:valid_at] = event.metadata.fetch(:valid_at).iso8601(TIMESTAMP_PRECISION)
42
34
  end
43
35
  end
44
36
 
@@ -6,20 +6,43 @@ module RubyEventStore
6
6
  @root_path = root_path
7
7
  end
8
8
 
9
+ def root_url
10
+ base_url
11
+ end
12
+
13
+ def events_url
14
+ "#{api_url}/events"
15
+ end
16
+
17
+ def api_url
18
+ "#{base_url}/api"
19
+ end
20
+
21
+ def streams_url
22
+ "#{api_url}/streams"
23
+ end
24
+
9
25
  def paginated_events_from_stream_url(id:, position: nil, direction: nil, count: nil)
10
- base = [host, root_path].join
11
- args = [position, direction, count].compact
12
26
  stream_name = Rack::Utils.escape(id)
27
+ query_string = URI.encode_www_form({
28
+ "page[position]" => position,
29
+ "page[direction]" => direction,
30
+ "page[count]" => count,
31
+ }.compact)
13
32
 
14
- if args.empty?
15
- "#{base}/streams/#{stream_name}/relationships/events"
33
+ if query_string.empty?
34
+ "#{api_url}/streams/#{stream_name}/relationships/events"
16
35
  else
17
- "#{base}/streams/#{stream_name}/relationships/events/#{args.join('/')}"
36
+ "#{api_url}/streams/#{stream_name}/relationships/events?#{query_string}"
18
37
  end
19
38
  end
20
39
 
21
40
  private
22
41
  attr_reader :host, :root_path
42
+
43
+ def base_url
44
+ [host, root_path].join
45
+ end
23
46
  end
24
47
  end
25
48
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RubyEventStore
4
4
  module Browser
5
- VERSION = "1.3.1"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end