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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db59e80cc30f17c8fdce83886172e77f3d2743f2
4
- data.tar.gz: bcc17cb24b5727a794bf8ce443fc68633f9dbd4d
3
+ metadata.gz: e51f9e7d5152f185c513691f110207963da446aa
4
+ data.tar.gz: 7fb45bbd4d370c91a2a7461e37cd5a01737bcded
5
5
  SHA512:
6
- metadata.gz: f9bfbb3841cd49260a632027f159501c8e245dc74fd3ab21f8c81b13cf3fa014be7412ec499f859982bb0c7b6d3343083c4968d0637d8a8f24b84ff5e35f8a1e
7
- data.tar.gz: 900fa98bebe2fe092df553abd59d8088de0171555fe965aa9fac60a60643097851b39802f332dcae9ccf2c3403d786d6d4bb6ca2bdeb4325cda2e2c604210e93
6
+ metadata.gz: 858b1027ee9b6272d36ffe4a3e7a0d5de419a71ef71433e3cf6bcf22b739c1e5c830d8b6ab393b7724d1c25413e482d7ab90c6cf78cf3e67163608fe69729ce3
7
+ data.tar.gz: 8b5eec21c8d3b5d084a15cf85f8113dd0b7d503835de8b23aefdd1f3ca1c921e3da2b328e43c6b6318c2b920499ed27f2aaedf39c11541fbe2077ffb16de6897
data/.babelrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["es2015"]
3
+ }
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /vendor
11
+ /node_modules/
11
12
 
12
13
  # rspec failure tracking
13
14
  .rspec_status
data/.travis.yml CHANGED
@@ -2,4 +2,10 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.2.3
5
+
5
6
  before_install: gem install bundler -v 1.14.3
7
+ install:
8
+ - bundle install
9
+ - npm install
10
+ script:
11
+ - bundle exec rspec & npm test
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Lieutenant Governor
2
2
 
3
+ [![Build Status](https://travis-ci.org/JordanLittell/lieutenant_governor.svg?branch=master)](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 path_to_js_helper is a required param that specifies the location of the generated helper.
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='app/assets/javascripts/pathHelpers.js')
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)
@@ -26,7 +26,7 @@ module LieutenantGovernor
26
26
  routes.reduce(table) do |memo, obj|
27
27
  name = get_name(obj)
28
28
  path = get_path(obj)
29
- table[name] = path if name.present? && path.present?
29
+ table[name] = path if name.length > 0 && path.length > 0
30
30
  end
31
31
 
32
32
  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
- path = makePath({
14
+ const requestPath = makeRequestPath({
16
15
  path,
17
16
  params,
18
17
  });
19
18
  const queryStrings = makeQueryStrings(query);
20
- return queryStrings.length > 0 ? `${path}?${queryStrings}`:path;
19
+ return queryStrings.length > 0
20
+ ? `${requestPath}?${queryStrings}`
21
+ :requestPath;
21
22
  }
22
23
 
23
- const makePath = ({ path, params }) => {
24
- const isArray = obj => Object.prototype.toString.call(obj) === '[object Array]';
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 `${params.length} params given, but ${numRequiredParams} are required`;
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 => Object.prototype.toString.call(obj) === '[object Array]';
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' ? v() : v === null ? '' : v === undefined ? '' : v;
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(`${prefix}[${typeof obj[i] === 'object' ? i : ''}]`, obj[i]);
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]') {
@@ -1,3 +1,3 @@
1
1
  module LieutenantGovernor
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-20 00:00:00.000000000 Z
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