railbus 0.1.1 → 0.1.2

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: cbcb54ddcd018a65bb1345be55f6433656109be6fa47d5bcfb20f86114130d27
4
- data.tar.gz: 7321d65b3ac0b95912d9d4ea2580cf7ecdb5719264f8a0bd9c7f6e0059ef8a13
3
+ metadata.gz: eca04d46928b46af6ee37969c8cd9e34ffa535ae8295b9a3efba843c4a1fbe32
4
+ data.tar.gz: a708f98d5b39524baf0888bd96f458df8a6dbc63e97a1827447fd2423ffeb03a
5
5
  SHA512:
6
- metadata.gz: 6c00077aaa236f244565a3e87691a594e309806248ad36de5de5e3ba51379fe880e1406d1ac61ca895c683e713062d3ea3c1e80c3dc2c3450e7c4fd0db9961d6
7
- data.tar.gz: 7a1c090bd7163c9c2aa01c2e2098c71870d4a7f494aa1f3f231a8220787fc61f027b7fb59b15e16467931712875f29fd112126ce60a5351f363ca8560daaf593
6
+ metadata.gz: b3f9a587e3d6658f6cacda733db67d37af4d8b2d8316e9d63f11f0a0899e54ad24b2d124a4f8750536786a923faa0200dd6a1c3025f926230d2257a8f3a6cca3
7
+ data.tar.gz: 35964ef3fd6e599cfca9aee232abc332d9ebf86ba072b1efde5fd5ad43aef1ad5c8670f268bef98ff3f4d2796cb25091a14357605bbc70647691ae7cba3f1001
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ # Version 0.1.2
4
+
5
+ Extract JavaScript generators to NPM package called 'yambus'.
6
+ Added Rails generator: `rails g railbus:install`.
7
+
3
8
  # Version 0.1.1
4
9
 
5
10
  Change prefix for `delete` routes from `destroy_` to `delete_`.
data/README.md CHANGED
@@ -22,8 +22,15 @@ Or install it yourself as:
22
22
 
23
23
  $ gem install railbus
24
24
 
25
- To use it with Webpack, create `*.js.erb` file in `app/javascript`
26
- (e.g. `lib/routes.js.erb`):
25
+ You may call this to generate example file and install JavaScript dependencies:
26
+
27
+ $ rails g railbus:install
28
+
29
+ Or do it manually:
30
+
31
+ 1. Run `yarn add @crosspath/yambus`.
32
+ 2. Create `*.js.erb` file in `app/javascript` (e.g. `lib/routes.js.erb`)
33
+ to use it with Webpack:
27
34
 
28
35
  ```erb
29
36
  /* rails-erb-loader-dependencies ../config/routes */
@@ -63,7 +70,7 @@ Here `String or Number, ...` represents values used in paths, e.g.
63
70
  And `...path_params` means hash-like object with values for paths, e.g.
64
71
  `{id: 123, category_id: 56}` (zero, one or more params).
65
72
 
66
- `data` means request body (payload), it can be hash-like object, `FormData` and so on, its allowed types depend on XHR library.
73
+ `data` means request body (payload), it can be hash-like object, `FormData` and so on, its allowed types depend on XHR library or request function.
67
74
 
68
75
  All arguments are optional.
69
76
 
@@ -0,0 +1,22 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Railbus
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ desc 'Install dependencies and example file with routes'
7
+
8
+ def install
9
+ exit unless run 'yarn add @crosspath/yambus axios'
10
+
11
+ create_file('app/javascript/lib/routes.js.erb') do |f|
12
+ <<-LINE
13
+ /* rails-erb-loader-dependencies ../config/routes */
14
+ <%= Railbus.generate %>
15
+ LINE
16
+ end
17
+
18
+ puts 'Railbus installed!'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,92 +1,25 @@
1
- const routes = <%= routes_json.html_safe %>;
1
+ import yambus from '@crosspath/yambus'
2
2
 
3
3
  <% if client == 'axios' %>
4
4
  import axios from 'axios'
5
-
6
- function request(route, args) {
7
- const params = build_request_options(route, args)
8
- return axios.request({
9
- url: params.path,
10
- method: route.verb,
11
- params: params.url_options,
12
- data: params.data
13
- })
14
- }
15
- <% else %>
16
- function request(route, args) {
17
- const params = build_request_options(route, args)
18
- return <%= client %>(route, params)
19
- }
20
5
  <% end %>
21
6
 
22
- const request_functions = Object.fromEntries(
23
- Object.entries(routes).map(pair => {
24
- const route = pair[1]
25
- const func = (...args) => request(route, args)
26
- return [pair[0], func]
27
- })
28
- )
29
-
30
- const path_functions = Object.fromEntries(
31
- Object.entries(routes).map(pair => {
32
- const route = pair[1]
33
- const func = (...args) => build_request_options(route, args).path
34
- return [`${pair[0]}_path`, func]
35
- })
36
- )
37
-
38
- export default Object.assign({}, request_functions, path_functions)
39
-
40
- function split_args(route, args) {
41
- const required_params = route.required
42
- let path_params = {}, url_options = {}, data = {}
43
- for (let index in args) {
44
- const arg = args[index]
45
- if (['string', 'number'].includes(typeof arg)) {
46
- path_params[required_params[index]] = arg
47
- } else {
48
- break
49
- }
7
+ const routes = <%= routes_json.html_safe %>
8
+
9
+ const route_functions = yambus.generate_route_functions(
10
+ routes,
11
+ (route, args) => {
12
+ const params = yambus.build_request_options(route, args)
13
+ <% if client == 'axios' %>
14
+ return axios.request({
15
+ url: params.path,
16
+ method: route.verb,
17
+ params: params.url_options,
18
+ data: params.data
19
+ })
20
+ <% else %>
21
+ return <%= client %>(route, params)
22
+ <% end %>
50
23
  }
51
- let options = args[Object.keys(path_params).length]
52
- data = args[Object.keys(path_params).length + 1]
53
- if (typeof options !== 'object')
54
- options = {}
55
- if (typeof data !== 'object')
56
- data = {}
57
- for (let param of required_params) {
58
- if (!(param in path_params)) {
59
- if (param in options) {
60
- path_params[param] = options[param]
61
- } else {
62
- throw new Error(required_param(route, param))
63
- }
64
- }
65
- }
66
- const format = presence(options.format)
67
- const extracted = required_params.concat(['format'])
68
- url_options = Object.fromEntries(
69
- Object.entries(options).filter(pair => !extracted.includes(pair[0]))
70
- )
71
- return {path_params, format, url_options, data}
72
- }
73
-
74
- function presence(v) {
75
- return (v != null && v !== '') ? v : null
76
- }
77
-
78
- function required_param(route, param) {
79
- return `Cannot find "${param}" in passed URL options. `+
80
- `This value is required for route "${route.name}" (${route.path}).`;
81
- }
82
-
83
- function build_request_options(route, args) {
84
- const params = split_args(route, args)
85
- let path = route.parts.
86
- map(v => typeof v === 'string' ? v : params.path_params[v.a]).join('')
87
- if (params.format)
88
- path += `.${params.format}`
89
- if (['get', 'delete'].includes(route.verb))
90
- params.data = null
91
- return {path: path, url_options: params.url_options, data: params.data}
92
- }
24
+ )
25
+ export default route_functions
@@ -1,3 +1,3 @@
1
1
  module Railbus
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Nochevnov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-22 00:00:00.000000000 Z
11
+ date: 2020-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -69,6 +69,7 @@ files:
69
69
  - Rakefile
70
70
  - bin/console
71
71
  - bin/setup
72
+ - lib/generators/railbus/install_generator.rb
72
73
  - lib/railbus.rb
73
74
  - lib/railbus/route_set.rb
74
75
  - lib/railbus/route_set_presenter.rb