lieutenant_governor 0.1.0 → 0.1.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 +4 -4
- data/.babelrc +3 -0
- data/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/README.md +12 -6
- data/lib/lieutenant_governor/generators/js_route_helpers.rb +3 -1
- data/lib/lieutenant_governor/routing/extractor.rb +1 -1
- data/lib/lieutenant_governor/templates/js_paths_template_str.rb +20 -10
- data/lib/lieutenant_governor/version.rb +1 -1
- data/package.json +30 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e51f9e7d5152f185c513691f110207963da446aa
|
|
4
|
+
data.tar.gz: 7fb45bbd4d370c91a2a7461e37cd5a01737bcded
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 858b1027ee9b6272d36ffe4a3e7a0d5de419a71ef71433e3cf6bcf22b739c1e5c830d8b6ab393b7724d1c25413e482d7ab90c6cf78cf3e67163608fe69729ce3
|
|
7
|
+
data.tar.gz: 8b5eec21c8d3b5d084a15cf85f8113dd0b7d503835de8b23aefdd1f3ca1c921e3da2b328e43c6b6318c2b920499ed27f2aaedf39c11541fbe2077ffb16de6897
|
data/.babelrc
ADDED
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Lieutenant Governor
|
|
2
2
|
|
|
3
|
+
[](https://travis-ci.org/JordanLittell/lieutenant_governor)
|
|
4
|
+
|
|
3
5
|
**Objective**: A rails gem to make it simple to define your endpoints once in your ```routes.rb``` file and have them automatically generated for use on the client.
|
|
4
6
|
|
|
5
7
|
|
|
@@ -18,14 +20,18 @@ And then execute:
|
|
|
18
20
|
Or install it yourself as:
|
|
19
21
|
|
|
20
22
|
$ gem install lieutenant_governor
|
|
21
|
-
|
|
22
|
-
In the initialization of your app (ex. `config/initializers/lieutenant_governor.rb` ), invoke the generator:
|
|
23
|
-
|
|
24
|
-
$ LieutenantGovernor::Generator.generate(path_to_js_helper)
|
|
25
23
|
|
|
26
|
-
The
|
|
24
|
+
The lieutenant_governor gem uses your applications routes to construct a javascript module for making requests to the server. Hence, the routes need to be loaded before you invoke the generate method like so:
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
```ruby
|
|
27
|
+
config.after_initialize do
|
|
28
|
+
Rails.application.reload_routes!
|
|
29
|
+
LieutenantGovernor::Generator.generate(path_to_js_helper)
|
|
30
|
+
# path_to_js_helper is a string
|
|
31
|
+
# to the desired location of the JS module that gets generated.
|
|
32
|
+
end
|
|
33
|
+
```
|
|
34
|
+
We recommend doing this in `config/environments/development.rb`
|
|
29
35
|
|
|
30
36
|
## Example Usage
|
|
31
37
|
|
|
@@ -6,6 +6,7 @@ require_relative '../templates/js_paths_template_str'
|
|
|
6
6
|
module LieutenantGovernor
|
|
7
7
|
|
|
8
8
|
module Generators
|
|
9
|
+
|
|
9
10
|
class JsRouteHelpers < Thor::Group
|
|
10
11
|
extend Thor::Actions
|
|
11
12
|
# Use the extractor to get the hash
|
|
@@ -13,7 +14,8 @@ module LieutenantGovernor
|
|
|
13
14
|
# using Thor, open up a file, and then write the javascript text to
|
|
14
15
|
# the file
|
|
15
16
|
|
|
16
|
-
def self.generate_paths_file(path
|
|
17
|
+
def self.generate_paths_file(path)
|
|
18
|
+
raise TypeError.new('Path must be String.') unless path.class == String
|
|
17
19
|
routes = Rails.application.routes.routes
|
|
18
20
|
route_table = LieutenantGovernor::Routing::Extractor.extract(routes)
|
|
19
21
|
template = LieutenantGovernor::Templates::JsPaths.new(route_table)
|
|
@@ -4,7 +4,6 @@ module LieutenantGovernor
|
|
|
4
4
|
%{
|
|
5
5
|
const STRING = 'string';
|
|
6
6
|
const PARAM = 'param';
|
|
7
|
-
const path;
|
|
8
7
|
|
|
9
8
|
const makeFullURL = ({
|
|
10
9
|
path = [],
|
|
@@ -12,16 +11,20 @@ const makeFullURL = ({
|
|
|
12
11
|
query = {},
|
|
13
12
|
} = {}) => {
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
const requestPath = makeRequestPath({
|
|
16
15
|
path,
|
|
17
16
|
params,
|
|
18
17
|
});
|
|
19
18
|
const queryStrings = makeQueryStrings(query);
|
|
20
|
-
return queryStrings.length > 0
|
|
19
|
+
return queryStrings.length > 0
|
|
20
|
+
? `${requestPath}?${queryStrings}`
|
|
21
|
+
:requestPath;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
const
|
|
24
|
-
const isArray = obj =>
|
|
24
|
+
const makeRequestPath = ({ path, params }) => {
|
|
25
|
+
const isArray = obj => {
|
|
26
|
+
return Object.prototype.toString.call(obj) === '[object Array]';
|
|
27
|
+
}
|
|
25
28
|
|
|
26
29
|
if(!isArray(params)) {
|
|
27
30
|
throw 'params argument must of type array';
|
|
@@ -32,7 +35,7 @@ const makePath = ({ path, params }) => {
|
|
|
32
35
|
}, 0);
|
|
33
36
|
|
|
34
37
|
if(params.length !== numRequiredParams) {
|
|
35
|
-
throw
|
|
38
|
+
throw `params: ${params.length} given, but ${numRequiredParams} required`;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
let numUsedParams = 0;
|
|
@@ -48,11 +51,15 @@ const makePath = ({ path, params }) => {
|
|
|
48
51
|
|
|
49
52
|
const makeQueryStrings = a => {
|
|
50
53
|
const s = [];
|
|
51
|
-
const rbracket =
|
|
52
|
-
const isArray = obj =>
|
|
54
|
+
const rbracket = /\\[\\]$/;
|
|
55
|
+
const isArray = obj => {
|
|
56
|
+
return Object.prototype.toString.call(obj) === '[object Array]';
|
|
57
|
+
}
|
|
53
58
|
|
|
54
59
|
const add = (k, v) => {
|
|
55
|
-
v = typeof v === 'function'
|
|
60
|
+
v = typeof v === 'function'
|
|
61
|
+
? v()
|
|
62
|
+
: v === null ? '' : v === undefined ? '' : v;
|
|
56
63
|
s[s.length] = `${encodeURIComponent(k)}=${encodeURIComponent(v)}`;
|
|
57
64
|
};
|
|
58
65
|
|
|
@@ -65,7 +72,10 @@ const makeQueryStrings = a => {
|
|
|
65
72
|
if (rbracket.test(prefix)) {
|
|
66
73
|
add(prefix, obj[i]);
|
|
67
74
|
} else {
|
|
68
|
-
buildParams(
|
|
75
|
+
buildParams(
|
|
76
|
+
`${prefix}[${typeof obj[i] === 'object' ? i : ''}]`,
|
|
77
|
+
obj[i]
|
|
78
|
+
);
|
|
69
79
|
}
|
|
70
80
|
}
|
|
71
81
|
} else if (obj && String(obj) === '[object Object]') {
|
data/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lieutenant_governor",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sync routes in rails with client code",
|
|
5
|
+
"directories": {
|
|
6
|
+
"test": "test"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "mocha --require ./test/testHelper.js '+(test)/**/*test.js'"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/JordanLittell/lieutenant_governor.git"
|
|
14
|
+
},
|
|
15
|
+
"author": "Jordan Littell, Sam Turner",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/JordanLittell/lieutenant_governor/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/JordanLittell/lieutenant_governor#readme",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"mocha": "3.2.0",
|
|
23
|
+
"babel-core": "6.24.0",
|
|
24
|
+
"babel-preset-es2015": "6.24.0",
|
|
25
|
+
"expect.js": "0.3.1"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"expect.js": "0.3.1"
|
|
29
|
+
}
|
|
30
|
+
}
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lieutenant_governor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- jordanlittell
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-03-
|
|
11
|
+
date: 2017-03-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: thor
|
|
@@ -73,6 +73,7 @@ executables: []
|
|
|
73
73
|
extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
|
75
75
|
files:
|
|
76
|
+
- ".babelrc"
|
|
76
77
|
- ".gitignore"
|
|
77
78
|
- ".rspec"
|
|
78
79
|
- ".travis.yml"
|
|
@@ -91,6 +92,7 @@ files:
|
|
|
91
92
|
- lib/lieutenant_governor/templates/js_paths_template_str.rb
|
|
92
93
|
- lib/lieutenant_governor/version.rb
|
|
93
94
|
- lieutenant_governor.gemspec
|
|
95
|
+
- package.json
|
|
94
96
|
homepage: https://github.com/JordanLittell/lieutenant_governor
|
|
95
97
|
licenses:
|
|
96
98
|
- MIT
|