logux-rack 0.1.0

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop_todo.yml +17 -0
  3. data/CHANGELOG.md +9 -0
  4. data/Gemfile +8 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +130 -0
  7. data/Rakefile +12 -0
  8. data/bin/console +15 -0
  9. data/bin/setup +8 -0
  10. data/config.ru +6 -0
  11. data/docker-compose.yml +29 -0
  12. data/lib/logux.rb +3 -0
  13. data/lib/logux/action.rb +68 -0
  14. data/lib/logux/action_caller.rb +46 -0
  15. data/lib/logux/action_controller.rb +6 -0
  16. data/lib/logux/action_watcher.rb +21 -0
  17. data/lib/logux/actions.rb +17 -0
  18. data/lib/logux/add.rb +37 -0
  19. data/lib/logux/auth.rb +13 -0
  20. data/lib/logux/base_controller.rb +37 -0
  21. data/lib/logux/channel_controller.rb +24 -0
  22. data/lib/logux/class_finder.rb +63 -0
  23. data/lib/logux/client.rb +21 -0
  24. data/lib/logux/error_renderer.rb +40 -0
  25. data/lib/logux/meta.rb +36 -0
  26. data/lib/logux/node.rb +37 -0
  27. data/lib/logux/policy.rb +14 -0
  28. data/lib/logux/policy_caller.rb +36 -0
  29. data/lib/logux/process.rb +9 -0
  30. data/lib/logux/process/action.rb +60 -0
  31. data/lib/logux/process/auth.rb +31 -0
  32. data/lib/logux/process/batch.rb +59 -0
  33. data/lib/logux/rack.rb +133 -0
  34. data/lib/logux/rack/app.rb +55 -0
  35. data/lib/logux/rack/version.rb +7 -0
  36. data/lib/logux/rake_tasks.rb +118 -0
  37. data/lib/logux/response.rb +18 -0
  38. data/lib/logux/stream.rb +27 -0
  39. data/lib/logux/test.rb +35 -0
  40. data/lib/logux/test/helpers.rb +73 -0
  41. data/lib/logux/test/matchers.rb +10 -0
  42. data/lib/logux/test/matchers/base.rb +25 -0
  43. data/lib/logux/test/matchers/response_chunks.rb +48 -0
  44. data/lib/logux/test/matchers/send_to_logux.rb +51 -0
  45. data/lib/logux/test/store.rb +21 -0
  46. data/lib/logux/utils.rb +19 -0
  47. data/lib/tasks/logux_tasks.rake +4 -0
  48. data/logux-rack.gemspec +68 -0
  49. metadata +386 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a92195a6e6cda7d85659828eda8c36688f4631496770acc091310b8918ae0022
4
+ data.tar.gz: b011e2d6c5c567c15f7a85460ec3fb4b94d6d719656de0d67d90f77f768c4d9a
5
+ SHA512:
6
+ metadata.gz: 75bb7b8d7a1cdcc309b9395dc13043af53e2f3ebda8db0058bca72fe124f863395badc3af1d1e604db33965fd42cc6be084c620f4fd080d7a9bca3390703229d
7
+ data.tar.gz: b945746e700b958b415b417580a5d23c7f8bccb0f28578d34deab3789c710a1eb9802863ee890d49b46734da3bf23e82d4a22fb2c06cee4b41a04d4e6bb872cd
@@ -0,0 +1,17 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2018-07-26 13:29:22 +0300 using RuboCop version 0.57.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 17
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ # Offense count: 2
14
+ Style/MixinUsage:
15
+ Exclude:
16
+ - 'spec/dummy/bin/setup'
17
+ - 'spec/dummy/bin/update'
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.0] - 2019-08-29
8
+
9
+ - Initial public release.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in logux-rack.gemspec
8
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 WildDima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,130 @@
1
+ <a href="https://amplifr.com/?utm_source=logux-rack">
2
+ <img width="100" height="140" align="right"
3
+ alt="Sponsored by Amplifr" src="https://amplifr-direct.s3-eu-west-1.amazonaws.com/social_images/image/37b580d9-3668-4005-8d5a-137de3a3e77c.png" />
4
+ </a>
5
+
6
+ # Logux::Rack
7
+
8
+ [![Build Status](https://travis-ci.org/logux/logux-rack.svg?branch=master)](https://travis-ci.org/logux/logux-rack) [![Coverage Status](https://coveralls.io/repos/github/logux/logux-rack/badge.svg?branch=master)](https://coveralls.io/github/logux/logux-rack?branch=master)
9
+
10
+ Add WebSockets, live-updates and offline-first to any Rack-based application with [Logux](https://github.com/logux/logux/). It adds [Logux Back-end Protocol](https://github.com/logux/logux/blob/master/backend-protocol/spec.md) support to Ruby on Rails or any other Rack applications, so you can use Logux Server as a proxy between WebSocket and your web application.
11
+
12
+ Read [Creating Logux Proxy](https://github.com/logux/logux/blob/master/2-starting/2-creating-proxy.md) guide.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'logux-rack'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ bundle
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Here is a minimal Rack configuration to start new `Logux::Rack` server:
31
+
32
+ ```ruby
33
+ # config.ru
34
+ require 'logux/rack'
35
+
36
+ run Logux.application
37
+ ```
38
+
39
+ Note that the HTTP response streaming depends on the web server used to serve the application. Use a server with streaming capability. [Puma](https://puma.io/), for instance:
40
+
41
+ ```bash
42
+ gem install puma
43
+ ```
44
+
45
+ Start the server:
46
+
47
+ ```bash
48
+ puma config.ru
49
+ ```
50
+
51
+ It is possible to mount `Logux::Rack` server within an existing Rails application. First of all, you will need to configure Logux by defining a server address in an initializer. For example, `config/initializers/logux.rb`:
52
+
53
+ ```ruby
54
+ Logux.configuration do |config|
55
+ config.logux_host = 'http://localhost:31338'
56
+ end
57
+ ```
58
+
59
+ Mount `Logux::Rack` in routes:
60
+
61
+ ```ruby
62
+ Rails.application.routes.draw do
63
+ mount Logux::Rack::App => '/'
64
+ end
65
+ ```
66
+
67
+ After this, POST requests to `/logux` will be processed by `LoguxController`. You can redefine it or inherit from, if it necessary, for example, for implementing custom authorization flow.
68
+
69
+ Here is another routing example for [Roda](https://github.com/jeremyevans/roda) application routing:
70
+
71
+ ```ruby
72
+ class MyApp < Roda
73
+ route do |r|
74
+ r.is 'logux' { r.run Logux::Rack::App }
75
+ end
76
+ end
77
+ ```
78
+
79
+ [Hanami](https://hanamirb.org/) configuration example:
80
+
81
+ ```ruby
82
+ # config/environment.rb
83
+ Hanami.configure do
84
+ mount Logux::Rack::App, at: '/'
85
+ end
86
+ ```
87
+
88
+ `Logux::Rack` can also be embedded into another Rack application using [Rack::Builder](https://www.rubydoc.info/gems/rack/Rack/Builder):
89
+
90
+ ```ruby
91
+ # config.ru
92
+
93
+ require 'logux/rack'
94
+
95
+ app = Rack::Builder.new do
96
+ use Rack::CommonLogger
97
+ map '/logux' { run Logux::Rack::App }
98
+ # ...
99
+ end
100
+
101
+ run app
102
+ ```
103
+
104
+ `Logux::Rack` will try to find Action for the specific message from Logux Server. For example, for `project/rename` action, you should define `Action::Project` class, inherited from `Logux::Action` base class, and implement `rename` method.
105
+
106
+ You can execute `rake logux:actions` to get the list of available action types, or `rake logux:channels` to get the list of available channels. Use optional path parameter to limit the search scope: `rake logux:actions[lib/logux/actions]`
107
+
108
+ ## Development with Docker
109
+
110
+ After checking out the repo, run:
111
+
112
+ ```bash
113
+ docker-compose run app bundle install
114
+ ```
115
+
116
+ Run tests with:
117
+
118
+ ```bash
119
+ docker-compose run app bundle exec rspec
120
+ ```
121
+
122
+ Run RuboCop with:
123
+
124
+ ```bash
125
+ docker-compose run app bundle exec rubocop
126
+ ```
127
+
128
+ ## License
129
+
130
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+ require_relative 'lib/logux/rake_tasks'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+ RuboCop::RakeTask.new
10
+ Logux::RakeTasks.new
11
+
12
+ task default: %i[rubocop spec]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'logux/rack'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'logux/rack'
5
+
6
+ run Logux.application
@@ -0,0 +1,29 @@
1
+ version: '3.4'
2
+
3
+ services:
4
+ app:
5
+ image: ruby:2.5.1
6
+ environment:
7
+ - BUNDLE_PATH=/bundle
8
+ - BUNDLE_CONFIG=/app/.bundle/config
9
+
10
+ - DB_HOST=db
11
+ - DB_NAME=logux_rack
12
+ - DB_USERNAME=postgres
13
+ command: bash
14
+ working_dir: /app
15
+ volumes:
16
+ - .:/app:cached
17
+ - bundler_data:/bundle
18
+ tmpfs:
19
+ - /tmp
20
+ depends_on:
21
+ - db
22
+
23
+ db:
24
+ image: postgres:10
25
+ environment:
26
+ - POSTGRES_DB=logux_rack
27
+
28
+ volumes:
29
+ bundler_data:
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logux/rack'
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logux
4
+ class Action
5
+ extend Forwardable
6
+
7
+ def_delegators(
8
+ :parameters,
9
+ :[],
10
+ :keys,
11
+ :key?,
12
+ :has_key?,
13
+ :values,
14
+ :has_value?,
15
+ :value?,
16
+ :empty?,
17
+ :include?,
18
+ :as_json,
19
+ :to_s,
20
+ :each_key
21
+ )
22
+
23
+ def initialize(parameters = {})
24
+ raise ArgumentError, :parameters unless parameters.is_a?(Hash)
25
+
26
+ @parameters = parameters.transform_keys(&:to_s)
27
+ end
28
+
29
+ def action_name
30
+ type&.split('/')&.dig(0)
31
+ end
32
+
33
+ def action_type
34
+ type&.split('/')&.last
35
+ end
36
+
37
+ def channel_name
38
+ channel&.split('/')&.dig(0)
39
+ end
40
+
41
+ def channel_id
42
+ channel&.split('/')&.last
43
+ end
44
+
45
+ def type
46
+ fetch('type')
47
+ end
48
+
49
+ def channel
50
+ fetch('channel')
51
+ end
52
+
53
+ def fetch(key)
54
+ value = self[key]
55
+ raise ParameterMissingError, key if value.to_s.empty?
56
+
57
+ value
58
+ end
59
+
60
+ def [](key)
61
+ parameters[key.is_a?(Symbol) ? key.to_s : key]
62
+ end
63
+
64
+ private
65
+
66
+ attr_reader :parameters
67
+ end
68
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logux
4
+ class ActionCaller
5
+ extend Forwardable
6
+
7
+ attr_reader :action, :meta
8
+
9
+ def_delegator :Logux, :logger
10
+
11
+ def initialize(action:, meta:)
12
+ @action = action
13
+ @meta = meta
14
+ end
15
+
16
+ def call!
17
+ Logux.watch_action { call_action }
18
+ rescue Logux::UnknownActionError, Logux::UnknownChannelError => e
19
+ logger.warn(e)
20
+ format(nil)
21
+ end
22
+
23
+ protected
24
+
25
+ def call_action
26
+ logger.debug("Searching Logux action: #{action}, meta: #{meta}")
27
+ format(action_controller.public_send(action.action_type))
28
+ end
29
+
30
+ private
31
+
32
+ def format(response)
33
+ return response if response.is_a?(Logux::Response)
34
+
35
+ Logux::Response.new(:processed, action: action, meta: meta)
36
+ end
37
+
38
+ def class_finder
39
+ @class_finder ||= Logux::ClassFinder.new(action: action, meta: meta)
40
+ end
41
+
42
+ def action_controller
43
+ class_finder.find_action_class.new(action: action, meta: meta)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logux
4
+ class ActionController < Logux::BaseController
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logux
4
+ class ActionWatcher
5
+ def self.call(options = {}, &block)
6
+ new(options).call(&block)
7
+ end
8
+
9
+ attr_reader :options
10
+
11
+ def initialize(options = {})
12
+ raise ArgumentError, :options unless options.is_a?(Hash)
13
+
14
+ @options = options
15
+ end
16
+
17
+ def call
18
+ yield
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logux
4
+ class Actions < Action
5
+ class << self
6
+ extend Gem::Deprecate
7
+
8
+ deprecate :new, 'Logux::Action.new', 2020, 9
9
+
10
+ def warn(message)
11
+ Logux.logger.warn(message)
12
+ end
13
+ end
14
+
15
+ private_class_method :warn
16
+ end
17
+ end