ruby_event_store-browser 2.2.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea08c29ab8816eb9104607256504c8e8b92b48c4e4002422206e7f55ff409b1c
4
- data.tar.gz: 60c9f99a88eb13ed4f0e50b60ff4c34bc77408d4b3dfb4d352087f472a9cf1ba
3
+ metadata.gz: 7098204142d74b946bd45666e171992bbe6d804954afaffa21b418c7e694c75a
4
+ data.tar.gz: '0997bcfc91dfccc4160eae1af9c57b971aa5168e3f552672b199da893691c15d'
5
5
  SHA512:
6
- metadata.gz: 3e9a51e51ae20b2e5cc3d2b49fea49fe3c46378e6f0a88d7ee71ae7e7a8e584334ca3243cee0923444cb8edf2547376b59f367fb3f488a308df8d86e0a294568
7
- data.tar.gz: d6d4df531df8184418a11d1942a5119062d3a6ed67f0ec42ac54cd78b59591067e77fef9c56df22e7a5bcec9372424ce97ca2fbe3b192b1a8d13cd36f02d8052
6
+ metadata.gz: b0c88c6ef6ce0689450a223b2a24ddd36475f4f77d030a0e2851a4208001efa6f7da45c61c4d02151d51be7faed956bc4d3c0f24202bfa1b12422d9e153cef96
7
+ data.tar.gz: 385e5a83a3e3b01d0ebe6f391236efab941da5f75efa20c2690efeef38262bbc6a7c17c95adbfa126c69450d513898723cbc50eba0408ade383aafa1374dbdd9
@@ -1,12 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../browser'
4
- require 'sinatra/base'
3
+ require_relative "../browser"
4
+ require "sinatra/base"
5
5
 
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, api_url: nil, environment: :production, related_streams_query: DEFAULT_RELATED_STREAMS_QUERY)
9
+ def self.for(
10
+ event_store_locator:,
11
+ host: nil,
12
+ path: nil,
13
+ api_url: nil,
14
+ environment: :production,
15
+ related_streams_query: DEFAULT_RELATED_STREAMS_QUERY
16
+ )
10
17
  self.tap do |app|
11
18
  app.settings.instance_exec do
12
19
  set :event_store_locator, event_store_locator
@@ -24,62 +31,54 @@ module RubyEventStore
24
31
  set :host, nil
25
32
  set :root_path, nil
26
33
  set :api_url, nil
27
- set :event_store_locator, -> {}
34
+ set :event_store_locator, -> { }
28
35
  set :related_streams_query, nil
29
36
  set :protection, except: :path_traversal
30
37
 
31
- mime_type :json, 'application/vnd.api+json'
38
+ mime_type :json, "application/vnd.api+json"
32
39
  end
33
40
 
34
- get '/api/events/:id' do
41
+ get "/api/events/:id" do
35
42
  begin
36
- json Event.new(
37
- event_store: settings.event_store_locator,
38
- params: symbolized_params,
39
- )
43
+ json Event.new(event_store: settings.event_store_locator, params: symbolized_params)
40
44
  rescue RubyEventStore::EventNotFound
41
45
  404
42
46
  end
43
47
  end
44
48
 
45
- get '/api/streams/:id' do
49
+ get "/api/streams/:id" do
46
50
  json GetStream.new(
47
- stream_name: params[:id],
48
- routing: routing,
49
- related_streams_query: settings.related_streams_query,
50
- )
51
+ stream_name: params[:id],
52
+ routing: routing,
53
+ related_streams_query: settings.related_streams_query
54
+ )
51
55
  end
52
56
 
53
- get '/api/streams/:id/relationships/events' do
57
+ get "/api/streams/:id/relationships/events" do
54
58
  json GetEventsFromStream.new(
55
- event_store: settings.event_store_locator,
56
- params: symbolized_params,
57
- routing: routing,
58
- )
59
+ event_store: settings.event_store_locator,
60
+ params: symbolized_params,
61
+ routing: routing
62
+ )
59
63
  end
60
64
 
61
65
  get %r{/(events/.*|streams/.*)?} do
62
- erb %{
66
+ erb "
63
67
  <!DOCTYPE html>
64
68
  <html>
65
69
  <head>
66
70
  <title>RubyEventStore::Browser</title>
67
- <link type="text/css" rel="stylesheet" href="<%= path %>/ruby_event_store_browser.css">
71
+ <meta name=\"ruby-event-store-browser-settings\" content='<%= browser_settings %>'>
68
72
  </head>
69
73
  <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>
74
+ <script type=\"text/javascript\" src=\"<%= path %>/ruby_event_store_browser.js\"></script>
75
+ <script type=\"text/javascript\" src=\"<%= path %>/bootstrap.js\"></script>
80
76
  </body>
81
77
  </html>
82
- }, locals: { path: settings.root_path || request.script_name, api_url: settings.api_url || routing.api_url }
78
+ ",
79
+ locals: {
80
+ path: settings.root_path || request.script_name
81
+ }
83
82
  end
84
83
 
85
84
  helpers do
@@ -88,9 +87,16 @@ module RubyEventStore
88
87
  end
89
88
 
90
89
  def routing
91
- Routing.new(
92
- settings.host || request.base_url,
93
- settings.root_path || request.script_name
90
+ Routing.new(settings.host || request.base_url, settings.root_path || request.script_name)
91
+ end
92
+
93
+ def browser_settings
94
+ JSON.dump(
95
+ {
96
+ rootUrl: routing.root_url,
97
+ apiUrl: settings.api_url || routing.api_url,
98
+ resVersion: RubyEventStore::VERSION
99
+ }
94
100
  )
95
101
  end
96
102
 
@@ -7,13 +7,11 @@ module RubyEventStore
7
7
 
8
8
  def initialize(event_store:, params:)
9
9
  @event_store = event_store
10
- @params = params
10
+ @params = params
11
11
  end
12
12
 
13
13
  def as_json
14
- {
15
- data: JsonApiEvent.new(event, parent_event_id).to_h,
16
- }
14
+ { data: JsonApiEvent.new(event, parent_event_id).to_h }
17
15
  end
18
16
 
19
17
  def event
@@ -21,9 +19,7 @@ module RubyEventStore
21
19
  end
22
20
 
23
21
  def parent_event_id
24
- if event.metadata.has_key?(:causation_id)
25
- event_store.read.event(event.metadata.fetch(:causation_id))&.event_id
26
- end
22
+ event_store.read.event(event.metadata.fetch(:causation_id))&.event_id if event.metadata.has_key?(:causation_id)
27
23
  end
28
24
 
29
25
  def event_id
@@ -9,51 +9,50 @@ module RubyEventStore
9
9
 
10
10
  def initialize(event_store:, params:, routing:)
11
11
  @event_store = event_store
12
- @params = params
12
+ @params = params
13
13
  @routing = routing
14
14
  end
15
15
 
16
16
  def as_json
17
- {
18
- data: events.map { |e| JsonApiEvent.new(e, nil).to_h },
19
- links: links
20
- }
17
+ { data: events.map { |e| JsonApiEvent.new(e, nil).to_h }, links: links }
21
18
  end
22
19
 
23
20
  def events
24
- @events ||= case direction
25
- when :forward
26
- events_forward(position).reverse
27
- when :backward
28
- events_backward(position)
29
- end
21
+ @events ||=
22
+ case direction
23
+ when :forward
24
+ events_forward(position).reverse
25
+ when :backward
26
+ events_backward(position)
27
+ end
30
28
  end
31
29
 
32
30
  def links
33
- @links ||= {}.tap do |h|
34
- if prev_event?
35
- h[:prev] = prev_page_link(events.first.event_id)
36
- h[:first] = first_page_link
37
- end
38
-
39
- if next_event?
40
- h[:next] = next_page_link(events.last.event_id)
41
- h[:last] = last_page_link
31
+ @links ||=
32
+ {}.tap do |h|
33
+ if prev_event?
34
+ h[:prev] = prev_page_link(events.first.event_id)
35
+ h[:first] = first_page_link
36
+ end
37
+
38
+ if next_event?
39
+ h[:next] = next_page_link(events.last.event_id)
40
+ h[:last] = last_page_link
41
+ end
42
42
  end
43
- end
44
43
  end
45
44
 
46
45
  def events_forward(position)
47
46
  spec = event_store.read.limit(count)
48
47
  spec = spec.stream(stream_name) unless stream_name.eql?(SERIALIZED_GLOBAL_STREAM_NAME)
49
- spec = spec.from(position) unless position.equal?(HEAD)
48
+ spec = spec.from(position) unless position.equal?(HEAD)
50
49
  spec.to_a
51
50
  end
52
51
 
53
52
  def events_backward(position)
54
53
  spec = event_store.read.limit(count).backward
55
54
  spec = spec.stream(stream_name) unless stream_name.eql?(SERIALIZED_GLOBAL_STREAM_NAME)
56
- spec = spec.from(position) unless position.equal?(HEAD)
55
+ spec = spec.from(position) unless position.equal?(HEAD)
57
56
  spec.to_a
58
57
  end
59
58
 
@@ -72,7 +71,12 @@ module RubyEventStore
72
71
  end
73
72
 
74
73
  def next_page_link(event_id)
75
- routing.paginated_events_from_stream_url(id: stream_name, position: event_id, direction: :backward, count: count)
74
+ routing.paginated_events_from_stream_url(
75
+ id: stream_name,
76
+ position: event_id,
77
+ direction: :backward,
78
+ count: count
79
+ )
76
80
  end
77
81
 
78
82
  def first_page_link
@@ -89,7 +93,7 @@ module RubyEventStore
89
93
 
90
94
  def direction
91
95
  case pagination_param[:direction]
92
- when 'forward'
96
+ when "forward"
93
97
  :forward
94
98
  else
95
99
  :backward
@@ -98,7 +102,7 @@ module RubyEventStore
98
102
 
99
103
  def position
100
104
  case pagination_param[:position]
101
- when 'head', nil
105
+ when "head", nil
102
106
  HEAD
103
107
  else
104
108
  pagination_param.fetch(:position)
@@ -13,12 +13,12 @@ module RubyEventStore
13
13
  id: stream_name,
14
14
  type: "streams",
15
15
  attributes: {
16
- related_streams: related_streams,
16
+ related_streams: related_streams
17
17
  },
18
18
  relationships: {
19
19
  events: {
20
20
  links: {
21
- self: routing.paginated_events_from_stream_url(id: stream_name),
21
+ self: routing.paginated_events_from_stream_url(id: stream_name)
22
22
  }
23
23
  }
24
24
  }
@@ -27,12 +27,11 @@ module RubyEventStore
27
27
  end
28
28
 
29
29
  private
30
+
30
31
  attr_reader :stream_name, :routing, :related_streams_query
31
32
 
32
33
  def related_streams
33
- unless related_streams_query.equal?(DEFAULT_RELATED_STREAMS_QUERY)
34
- related_streams_query.call(stream_name)
35
- end
34
+ related_streams_query.call(stream_name) unless related_streams_query.equal?(DEFAULT_RELATED_STREAMS_QUERY)
36
35
  end
37
36
  end
38
37
  end
@@ -19,18 +19,19 @@ module RubyEventStore
19
19
  correlation_stream_name: correlation_stream_name,
20
20
  causation_stream_name: causation_stream_name,
21
21
  type_stream_name: type_stream_name,
22
- parent_event_id: parent_event_id,
23
- },
22
+ parent_event_id: parent_event_id
23
+ }
24
24
  }
25
25
  end
26
26
 
27
27
  private
28
+
28
29
  attr_reader :event, :parent_event_id
29
30
 
30
31
  def metadata
31
32
  event.metadata.to_h.tap do |m|
32
33
  m[:timestamp] = event.metadata.fetch(:timestamp).iso8601(TIMESTAMP_PRECISION)
33
- m[:valid_at] = event.metadata.fetch(:valid_at).iso8601(TIMESTAMP_PRECISION)
34
+ m[:valid_at] = event.metadata.fetch(:valid_at).iso8601(TIMESTAMP_PRECISION)
34
35
  end
35
36
  end
36
37
 
@@ -24,11 +24,10 @@ module RubyEventStore
24
24
 
25
25
  def paginated_events_from_stream_url(id:, position: nil, direction: nil, count: nil)
26
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)
27
+ query_string =
28
+ URI.encode_www_form(
29
+ { "page[position]" => position, "page[direction]" => direction, "page[count]" => count }.compact
30
+ )
32
31
 
33
32
  if query_string.empty?
34
33
  "#{api_url}/streams/#{stream_name}/relationships/events"
@@ -38,6 +37,7 @@ module RubyEventStore
38
37
  end
39
38
 
40
39
  private
40
+
41
41
  attr_reader :host, :root_path
42
42
 
43
43
  def base_url
@@ -9,51 +9,50 @@ module RubyEventStore
9
9
 
10
10
  def initialize(event_store:, params:, routing:)
11
11
  @event_store = event_store
12
- @params = params
12
+ @params = params
13
13
  @routing = routing
14
14
  end
15
15
 
16
16
  def as_json
17
- {
18
- data: events.map { |e| JsonApiEvent.new(e, nil).to_h },
19
- links: links
20
- }
17
+ { data: events.map { |e| JsonApiEvent.new(e, nil).to_h }, links: links }
21
18
  end
22
19
 
23
20
  def events
24
- @events ||= case direction
25
- when :forward
26
- events_forward(position).reverse
27
- when :backward
28
- events_backward(position)
29
- end
21
+ @events ||=
22
+ case direction
23
+ when :forward
24
+ events_forward(position).reverse
25
+ when :backward
26
+ events_backward(position)
27
+ end
30
28
  end
31
29
 
32
30
  def links
33
- @links ||= {}.tap do |h|
34
- if prev_event?
35
- h[:prev] = prev_page_link(events.first.event_id)
36
- h[:first] = first_page_link
37
- end
38
-
39
- if next_event?
40
- h[:next] = next_page_link(events.last.event_id)
41
- h[:last] = last_page_link
31
+ @links ||=
32
+ {}.tap do |h|
33
+ if prev_event?
34
+ h[:prev] = prev_page_link(events.first.event_id)
35
+ h[:first] = first_page_link
36
+ end
37
+
38
+ if next_event?
39
+ h[:next] = next_page_link(events.last.event_id)
40
+ h[:last] = last_page_link
41
+ end
42
42
  end
43
- end
44
43
  end
45
44
 
46
45
  def events_forward(position)
47
46
  spec = event_store.read.limit(count)
48
47
  spec = spec.stream(stream_name) unless stream_name.eql?(SERIALIZED_GLOBAL_STREAM_NAME)
49
- spec = spec.from(position) unless position.equal?(HEAD)
48
+ spec = spec.from(position) unless position.equal?(HEAD)
50
49
  spec.to_a
51
50
  end
52
51
 
53
52
  def events_backward(position)
54
53
  spec = event_store.read.limit(count).backward
55
54
  spec = spec.stream(stream_name) unless stream_name.eql?(SERIALIZED_GLOBAL_STREAM_NAME)
56
- spec = spec.from(position) unless position.equal?(HEAD)
55
+ spec = spec.from(position) unless position.equal?(HEAD)
57
56
  spec.to_a
58
57
  end
59
58
 
@@ -72,7 +71,12 @@ module RubyEventStore
72
71
  end
73
72
 
74
73
  def next_page_link(event_id)
75
- routing.paginated_events_from_stream_url(id: stream_name, position: event_id, direction: :backward, count: count)
74
+ routing.paginated_events_from_stream_url(
75
+ id: stream_name,
76
+ position: event_id,
77
+ direction: :backward,
78
+ count: count
79
+ )
76
80
  end
77
81
 
78
82
  def first_page_link
@@ -89,7 +93,7 @@ module RubyEventStore
89
93
 
90
94
  def direction
91
95
  case params[:direction]
92
- when 'forward'
96
+ when "forward"
93
97
  :forward
94
98
  else
95
99
  :backward
@@ -98,7 +102,7 @@ module RubyEventStore
98
102
 
99
103
  def position
100
104
  case params[:position]
101
- when 'head', nil
105
+ when "head", nil
102
106
  HEAD
103
107
  else
104
108
  params.fetch(:position)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RubyEventStore
4
4
  module Browser
5
- VERSION = "2.2.0"
5
+ VERSION = "2.4.1"
6
6
  end
7
7
  end
@@ -3,13 +3,13 @@
3
3
  module RubyEventStore
4
4
  module Browser
5
5
  PAGE_SIZE = 20
6
- SERIALIZED_GLOBAL_STREAM_NAME = 'all'.freeze
6
+ SERIALIZED_GLOBAL_STREAM_NAME = "all".freeze
7
7
  DEFAULT_RELATED_STREAMS_QUERY = ->(stream_name) { [] }
8
8
  end
9
9
  end
10
10
 
11
- require_relative 'browser/event'
12
- require_relative 'browser/json_api_event'
13
- require_relative 'browser/get_events_from_stream'
14
- require_relative 'browser/get_stream'
15
- require_relative 'browser/routing'
11
+ require_relative "browser/event"
12
+ require_relative "browser/json_api_event"
13
+ require_relative "browser/get_events_from_stream"
14
+ require_relative "browser/get_stream"
15
+ require_relative "browser/routing"
@@ -0,0 +1,3 @@
1
+ Elm.Main.init({
2
+ flags: JSON.parse(document.querySelector("meta[name='ruby-event-store-browser-settings']").getAttribute("content")),
3
+ });