api_recipes 3.0.0.beta3 → 3.0.0.beta4
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/README.md +194 -8
- data/UPGRADING.md +84 -26
- data/lib/api_recipes/cli/code.rb +153 -0
- data/lib/api_recipes/cli/convert/migration.rb +624 -0
- data/lib/api_recipes/cli/convert/snippet.rb +67 -0
- data/lib/api_recipes/cli/convert.rb +533 -0
- data/lib/api_recipes/cli/recipes/summary.rb +23 -4
- data/lib/api_recipes/cli/routes.rb +1 -1
- data/lib/api_recipes/cli/upgrade/project.rb +194 -0
- data/lib/api_recipes/cli/upgrade.rb +353 -24
- data/lib/api_recipes/cli.rb +94 -7
- data/lib/api_recipes/configuration.rb +18 -5
- data/lib/api_recipes/endpoint.rb +1 -1
- data/lib/api_recipes/internals/compiler.rb +28 -0
- data/lib/api_recipes/internals/context.rb +35 -6
- data/lib/api_recipes/internals/definition.rb +1 -1
- data/lib/api_recipes/internals/option_value.rb +92 -0
- data/lib/api_recipes/internals/request.rb +17 -6
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +1 -0
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '0758e23f654ba318829b6a64cc4fce5f2241efb5f1d872348d7b8d45d31f9684'
|
|
4
|
+
data.tar.gz: 4612f273b0cd8e76d7f9a96e9ed5c3577995fec08065f22678cf01da4dde8331
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a1ceeb393133583bd1645545a1e211f7ddbc8342e23947a2dbeed7483714c8f8d3363edfdac420520a1d2c70fd44d31ad7e73996c89061fb2e396c06733670d3
|
|
7
|
+
data.tar.gz: 0fed2e2602edd6f233ce07b5f7f54ee535c9a1a432928abd152e6ab27509e766b81ea2677dd3bf25ea311d9e70e28aba814b38c1377a9e14b78c75484f644e9f
|
data/README.md
CHANGED
|
@@ -345,8 +345,10 @@ request must be a Hash.
|
|
|
345
345
|
## Variables
|
|
346
346
|
|
|
347
347
|
A variable is a `{name}` placeholder in the `host`, the `base_url`, the `path`
|
|
348
|
-
of an endpoint,
|
|
349
|
-
request
|
|
348
|
+
of an endpoint, the value of a header or of a parameter sent with every
|
|
349
|
+
request, or in one of the options that are read when a request is sent:
|
|
350
|
+
`protocol`, `port`, `timeout`, `on_bad_code`, `mime_type`, `verb` and
|
|
351
|
+
`ok_code`. Use it for what changes from one account or one environment to the
|
|
350
352
|
next, such as the address of a shop. Declare the variables of the API, with an
|
|
351
353
|
optional `default` and `description`:
|
|
352
354
|
|
|
@@ -469,6 +471,48 @@ every thread. That is why it is given only where the application starts, to
|
|
|
469
471
|
while requests are served goes to `with` or to an object. The same holds for
|
|
470
472
|
credentials.
|
|
471
473
|
|
|
474
|
+
A service whose address changes with the environment is described once, and
|
|
475
|
+
each environment gives its values:
|
|
476
|
+
|
|
477
|
+
```yaml
|
|
478
|
+
billing:
|
|
479
|
+
variables:
|
|
480
|
+
protocol:
|
|
481
|
+
default: https
|
|
482
|
+
host:
|
|
483
|
+
default: billing.example.com
|
|
484
|
+
port:
|
|
485
|
+
default: 443
|
|
486
|
+
timeout:
|
|
487
|
+
default: 5
|
|
488
|
+
protocol: '{protocol}'
|
|
489
|
+
host: '{host}'
|
|
490
|
+
port: '{port}'
|
|
491
|
+
timeout: '{timeout}'
|
|
492
|
+
endpoints:
|
|
493
|
+
invoices:
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
```ruby
|
|
497
|
+
ApiRecipes.configure do |config|
|
|
498
|
+
config.values = { billing: { variables: { protocol: 'http', host: 'localhost', port: 3000 } } }
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
ApiRecipes.billing.invoices.url.to_s
|
|
502
|
+
# => "http://localhost:3000/invoices"
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
In these options the variable gives a value of the option: `http` or `https`
|
|
506
|
+
for the protocol, an integer from 1 to 65535 for the port, a positive number
|
|
507
|
+
for the timeout, `raise` or `ignore`, a verb, a status. It is made when the
|
|
508
|
+
request is built, and a value that is not one raises
|
|
509
|
+
`ApiRecipes::InvalidVariable` before anything is sent, as the default of a
|
|
510
|
+
variable that gives the whole option raises `ApiRecipes::ConfigurationError`
|
|
511
|
+
when the configuration is loaded. `definition`, `routes` and the `api_recipes routes` command show such
|
|
512
|
+
an option as it is written. What decides the shape of the tree is read when
|
|
513
|
+
the configuration is loaded and takes no variable: `endpoints`, `resource`,
|
|
514
|
+
`route`, the names, `verify_with` and `auth`.
|
|
515
|
+
|
|
472
516
|
The value of a variable is a String, a Symbol or a number, and is kept as
|
|
473
517
|
text. A value is checked for the place where it ends up. In a `host` it can contain
|
|
474
518
|
only letters, digits, dots, hyphens and underscores. In a `base_url` and in a
|
|
@@ -504,8 +548,11 @@ together: they are applied in the order they are assigned, so assigning
|
|
|
504
548
|
|
|
505
549
|
A file that is missing, empty, not valid YAML or not valid UTF-8 raises
|
|
506
550
|
`ApiRecipes::RecipeFileError`, which names the file. A file called
|
|
507
|
-
`something.recipe.yml` is
|
|
508
|
-
|
|
551
|
+
`something.recipe.yml` is a [portable recipe](#portable-recipes) and goes in
|
|
552
|
+
`recipes_paths`, where it is never evaluated with ERB: here it is refused,
|
|
553
|
+
unless `recipes_paths` has it too, in which case it is left to `recipes_paths`.
|
|
554
|
+
So one pattern, such as `config/apis/*.yml*`, can match both kinds of file in
|
|
555
|
+
one folder.
|
|
509
556
|
|
|
510
557
|
Files are evaluated with ERB before being parsed, so values can come from the
|
|
511
558
|
environment:
|
|
@@ -883,6 +930,145 @@ A repository of recipes keeps each one in `recipes/NAME.recipe.yml`. A
|
|
|
883
930
|
published version is the tag `NAME-VERSION`, for example `billing-1.2.0`.
|
|
884
931
|
`api_recipes routes billing.recipe.yml` lists the calls of a recipe.
|
|
885
932
|
|
|
933
|
+
### From a file of your project
|
|
934
|
+
|
|
935
|
+
When the API is already described in a file of your project, `convert` moves
|
|
936
|
+
it to a portable recipe. Run it in the root of the project: a Rails
|
|
937
|
+
application or any other Ruby project, wherever `ApiRecipes.configure` is
|
|
938
|
+
called.
|
|
939
|
+
|
|
940
|
+
$ bundle exec api_recipes convert config/apis/billing.yml.erb
|
|
941
|
+
billing 1.0.0 now comes from a portable recipe: 2 calls
|
|
942
|
+
|
|
943
|
+
Changed in the project
|
|
944
|
+
created config/api_recipes/billing.recipe.yml, with its line in config/api_recipes/recipes.lock
|
|
945
|
+
edited config/initializers/api_recipes.rb: recipes_paths and config.values
|
|
946
|
+
edited app/models/invoice.rb: verify_with on the declaration of the api
|
|
947
|
+
deleted config/apis/billing.yml.erb
|
|
948
|
+
|
|
949
|
+
What changed in the description
|
|
950
|
+
host: the ERB tag is now the variable {billing_host}, with the default written in the tag
|
|
951
|
+
port: the ERB tag is now the variable {billing_port}, with the default written in the tag
|
|
952
|
+
verify_with: left out, a recipe cannot carry it: give it where a class declares the api, as `api :billing, verify_with: ...`
|
|
953
|
+
default_headers.Api-Key-Client: the ERB tag is now the variable {billing_client}; the value is not in the recipe
|
|
954
|
+
|
|
955
|
+
Every change is in `git status` and `git diff`. Check the recipes with `api_recipes check`, then start the application.
|
|
956
|
+
|
|
957
|
+
The project works as before, and the values still come from where they came:
|
|
958
|
+
the code of each tag is moved, as it is written, to `config.values`.
|
|
959
|
+
|
|
960
|
+
```ruby
|
|
961
|
+
ApiRecipes.configure do |config|
|
|
962
|
+
config.apis_files_paths = Dir[File.expand_path('../apis/*.yml.erb', __dir__)]
|
|
963
|
+
config.values = {
|
|
964
|
+
billing: { variables: {
|
|
965
|
+
billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
|
|
966
|
+
billing_port: (ENV['BILLING_PORT'] || 443),
|
|
967
|
+
billing_client: ENV['BILLING_CLIENT']
|
|
968
|
+
}.compact }
|
|
969
|
+
}
|
|
970
|
+
config.recipes_paths = Dir[File.expand_path('../api_recipes/*.recipe.yml', __dir__)]
|
|
971
|
+
end
|
|
972
|
+
```
|
|
973
|
+
|
|
974
|
+
What the command does:
|
|
975
|
+
|
|
976
|
+
- **It writes the recipe**, with the key `recipe` (the name of the API,
|
|
977
|
+
`--version`, `1.0.0` by default, and the format), and installs it in
|
|
978
|
+
`config/api_recipes` with its line in `recipes.lock`. When the file describes
|
|
979
|
+
several APIs, `--name` chooses one. Anchors, aliases and merge keys are
|
|
980
|
+
written out.
|
|
981
|
+
- **An `<%= ... %>` tag becomes a variable**, in every place where a recipe
|
|
982
|
+
can have one: the `host`, the `base_url`, a `path`, the value of a header or
|
|
983
|
+
of a parameter, and the `protocol`, `port`, `timeout`, `on_bad_code`,
|
|
984
|
+
`mime_type`, `verb` and `ok_code` when the tag gives the whole value. The
|
|
985
|
+
variable is named after the environment variable that the tag reads, or
|
|
986
|
+
after its place, and a default written in the tag as plain text or as a
|
|
987
|
+
number (`ENV.fetch('NAME', 'text')`, `ENV['NAME'] || 443`) becomes the
|
|
988
|
+
default of the variable. A default that a tag gave to a variable the file
|
|
989
|
+
declares is given by the application from now on.
|
|
990
|
+
- **Nothing in the file is run**, and there is no option that runs it. The
|
|
991
|
+
code of a tag goes to `config.values` as text, and each tag has its own
|
|
992
|
+
variable, also two tags in one value. `.compact` leaves out a variable whose
|
|
993
|
+
code gives `nil`: the default of the recipe applies when the tag had one
|
|
994
|
+
written, and otherwise the request says which variable is missing, where
|
|
995
|
+
2.x went on with the default of the gem.
|
|
996
|
+
- **A credential is never copied into the recipe.** A header or a parameter
|
|
997
|
+
whose name contains `auth`, `key`, `token`, `secret`, `passw`, `credential`,
|
|
998
|
+
`signature`, `cookie` or `session` becomes a variable without a default,
|
|
999
|
+
unless the file already gives it with a variable it declares, and
|
|
1000
|
+
its value, written in the file or given by a tag, goes to `config.values`.
|
|
1001
|
+
The command cannot know a secret written anywhere else: read the recipe
|
|
1002
|
+
before you share it.
|
|
1003
|
+
- **The old description is removed**: the file is deleted, or only the lines
|
|
1004
|
+
of that API when it describes others, because a recipe never takes the place
|
|
1005
|
+
of an API that another source defines.
|
|
1006
|
+
- **`verify_with`**, which a recipe cannot carry, is given to each `api :name`
|
|
1007
|
+
that a class or a module declares. One that a tag gave, or that an endpoint
|
|
1008
|
+
had, is left to you, as it is when one of the declarations gives its options
|
|
1009
|
+
in a way the command cannot read, such as `**options`.
|
|
1010
|
+
- **When it changes the project it prints names and paths**, nothing of what
|
|
1011
|
+
the file holds.
|
|
1012
|
+
|
|
1013
|
+
It changes your files only when that is safe, and all at once or not at all:
|
|
1014
|
+
|
|
1015
|
+
- Lines are added only to a plain file of the project that is in the last
|
|
1016
|
+
commit of git. A file is deleted, or lines are taken out of it, only when
|
|
1017
|
+
git has it committed as it is now. A symbolic link, and a file that git was told to skip, are never
|
|
1018
|
+
changed. Outside a git repository the project is not changed.
|
|
1019
|
+
- Ruby is read as a tree, never matched as lines and never run, and the
|
|
1020
|
+
command edits only what it can read whole:
|
|
1021
|
+
- the call to `ApiRecipes.configure`, when the project has exactly one, with
|
|
1022
|
+
a block `do |config|` on several lines that has no `rescue` and cannot end
|
|
1023
|
+
early with `next`, `break` or `return`;
|
|
1024
|
+
- `config.values`, which is added, or which gets one more entry when it is
|
|
1025
|
+
given a Hash between braces whose keys are all written as names, none of
|
|
1026
|
+
them the API, and whose closing brace starts a line;
|
|
1027
|
+
- `config.apis_files_paths`, when the old file is deleted: it must be given
|
|
1028
|
+
patterns of `Dir`, or a list between brackets of names written as text,
|
|
1029
|
+
from which the one element that names the file is taken out.
|
|
1030
|
+
|
|
1031
|
+
Anything else stops it: another mention of the old file in the code, another
|
|
1032
|
+
file that describes the API, a `recipes_paths` it cannot read.
|
|
1033
|
+
- Every Ruby file it changes must still be Ruby with the change, and when a
|
|
1034
|
+
write fails every file is put back as it was.
|
|
1035
|
+
- When a step is not possible, nothing is written, the command says why, and
|
|
1036
|
+
it gives the steps to do by hand with the code to paste: the commands that
|
|
1037
|
+
write and install the recipe, the lines for the block of
|
|
1038
|
+
`ApiRecipes.configure` with the file and the line, the line that gives
|
|
1039
|
+
`verify_with`, and what to remove.
|
|
1040
|
+
|
|
1041
|
+
2. In config/initializers/api_recipes.rb, inside the block of ApiRecipes.configure (line 1), add:
|
|
1042
|
+
|
|
1043
|
+
config.recipes_paths = Dir[File.expand_path('../api_recipes/*.recipe.yml', __dir__)]
|
|
1044
|
+
config.values = {
|
|
1045
|
+
billing: { variables: {
|
|
1046
|
+
billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
|
|
1047
|
+
billing_port: (ENV['BILLING_PORT'] || 443),
|
|
1048
|
+
billing_client: ENV['BILLING_CLIENT']
|
|
1049
|
+
}.compact }
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
The code is what the file holds, the code of each tag and each value that
|
|
1053
|
+
was written, so that what you paste works; on a terminal it is shown in
|
|
1054
|
+
colours. Only what a terminal would obey is left out, with a comment that
|
|
1055
|
+
says the line to copy it from.
|
|
1056
|
+
- What it could not do by itself after changing the project is listed under
|
|
1057
|
+
`Left to you`: a `verify_with` when no class declares the API, or on an
|
|
1058
|
+
endpoint.
|
|
1059
|
+
- A tag that is not `<%= ... %>`, that gives the name of an option, or that
|
|
1060
|
+
gives a part of the protocol, of the port or of another such option, stops
|
|
1061
|
+
the command, which says where.
|
|
1062
|
+
|
|
1063
|
+
To write the recipe alone, as for the repository of the recipes, name a
|
|
1064
|
+
folder: the project is not touched, and the command gives the code for the
|
|
1065
|
+
block of `ApiRecipes.configure`.
|
|
1066
|
+
|
|
1067
|
+
$ api_recipes convert config/apis/billing.yml.erb --to ../api-recipes/recipes
|
|
1068
|
+
|
|
1069
|
+
The command does not publish: the recipe reaches a repository with `git`,
|
|
1070
|
+
through the review that every recipe gets.
|
|
1071
|
+
|
|
886
1072
|
## Where an API is available
|
|
887
1073
|
|
|
888
1074
|
### On the `ApiRecipes` module
|
|
@@ -1750,11 +1936,11 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1750
1936
|
|
|
1751
1937
|
| Option | Default | Meaning |
|
|
1752
1938
|
| --- | --- | --- |
|
|
1753
|
-
| `protocol` | `https` | `http` or `https` |
|
|
1939
|
+
| `protocol` | `https` | `http` or `https`; can be a `{variable}` |
|
|
1754
1940
|
| `host` | `localhost` | host name, which can contain `{variables}` |
|
|
1755
|
-
| `port` | port of the protocol | port number |
|
|
1941
|
+
| `port` | port of the protocol | port number; can be a `{variable}` |
|
|
1756
1942
|
| `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}` |
|
|
1757
|
-
| `timeout` | `3` | seconds allowed for the whole request |
|
|
1943
|
+
| `timeout` | `3` | seconds allowed for the whole request; can be a `{variable}`, as `on_bad_code` and `mime_type` |
|
|
1758
1944
|
| `default_headers` | none | headers sent with every request; a value can contain `{variables}` |
|
|
1759
1945
|
| `default_params` | none | query parameters sent with every request; a value is text, a number or a boolean, and can contain `{variables}` |
|
|
1760
1946
|
| `on_bad_code` | `raise` | `raise` or `ignore` when the response is not a success |
|
|
@@ -1769,7 +1955,7 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1769
1955
|
| Option | Default | Meaning |
|
|
1770
1956
|
| --- | --- | --- |
|
|
1771
1957
|
| `path` | the endpoint name | segment added to the parent path; `''` adds nothing; can contain `:name` and `*name` placeholders and `{variables}` |
|
|
1772
|
-
| `verb` | `get` | HTTP verb |
|
|
1958
|
+
| `verb` | `get` | HTTP verb; can be a `{variable}`, as `ok_code` (one status), `timeout`, `on_bad_code` and `mime_type` |
|
|
1773
1959
|
| `route` | `yes` | `no` makes the endpoint a group that cannot be called |
|
|
1774
1960
|
| `ok_code` | any 2xx | the status, or the list of statuses, accepted as a success |
|
|
1775
1961
|
| `timeout` | looked for up the tree | seconds allowed for the whole request |
|
data/UPGRADING.md
CHANGED
|
@@ -51,8 +51,9 @@ github:
|
|
|
51
51
|
cannot upgrade Ruby yet, stay on 2.10.
|
|
52
52
|
- **http.rb 6.** The gem needs `http` 6.0.4 or higher, below 7. http.rb 5 is no
|
|
53
53
|
longer supported.
|
|
54
|
-
- **Run `api_recipes upgrade`**
|
|
55
|
-
[below](#the-api_recipes-upgrade-command)). It finds
|
|
54
|
+
- **Run `api_recipes upgrade`** in the root of your project (see
|
|
55
|
+
[below](#the-api_recipes-upgrade-command)). It finds your code and your
|
|
56
|
+
configuration files by itself, then the removed
|
|
56
57
|
`route`, `base_configs`, `ApiRecipes.print_urls`, the removed error classes and
|
|
57
58
|
`Hash` and `Array` methods, and a few lines that deserve a look (manual
|
|
58
59
|
encoding of path parameters, a bare `rescue`, `follow:`). It then lists what it
|
|
@@ -422,8 +423,9 @@ You can adopt these when you are ready. The [README](README.md) describes each o
|
|
|
422
423
|
- **Credentials along a chain**: the chain, the object, the class that declares
|
|
423
424
|
the API, then the whole application. A credential is bound to its origin.
|
|
424
425
|
- **`variables`**: `{name}` placeholders in `host`, `base_url`, the `path` of an
|
|
425
|
-
endpoint
|
|
426
|
-
|
|
426
|
+
endpoint, the values of `default_headers` and `default_params`, and in
|
|
427
|
+
`protocol`, `port`, `timeout`, `on_bad_code`, `mime_type`, `verb` and
|
|
428
|
+
`ok_code`, with values given at any of those levels.
|
|
427
429
|
- **`default_params`**: query parameters sent with every request, as
|
|
428
430
|
`default_headers` does for headers. Both can be written on an endpoint, for
|
|
429
431
|
that endpoint and the ones below it.
|
|
@@ -474,37 +476,92 @@ See [Security in the README](README.md#security) for the same list in its place.
|
|
|
474
476
|
## The `api_recipes upgrade` command
|
|
475
477
|
|
|
476
478
|
The gem installs a command that looks for the changes of this guide. It never
|
|
477
|
-
changes a file.
|
|
478
|
-
|
|
479
|
-
$ api_recipes upgrade
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
`
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
- The
|
|
491
|
-
|
|
479
|
+
changes a file. Run it in the root of your project, with no argument:
|
|
480
|
+
|
|
481
|
+
$ bundle exec api_recipes upgrade
|
|
482
|
+
|
|
483
|
+
It finds by itself what to check, and says what it checked:
|
|
484
|
+
|
|
485
|
+
- **The Ruby files of the project**, `*.rb` and `*.rake`. It leaves out the
|
|
486
|
+
folders `vendor`, `node_modules`, `tmp`, `log`, `coverage`, `public` and
|
|
487
|
+
`storage` at the top of the project, `node_modules` wherever it is, what
|
|
488
|
+
starts with a dot, and every symbolic link. Code that stops working is marked
|
|
489
|
+
`[breaks]`; a line that deserves a look because its result changes is marked
|
|
490
|
+
`[check]`: a manual encoding of a path parameter, a bare `rescue`, `follow:`,
|
|
491
|
+
`response.request`, the removed `Hash` and `Array` methods.
|
|
492
|
+
- **The files that describe an API**: the YAML files (`.yml`, `.yaml`, also
|
|
493
|
+
followed by `.erb`) where a name has `endpoints`, each with its options or
|
|
494
|
+
with none, and the files called `NAME.recipe.yml`. Those under `spec`, `test`
|
|
495
|
+
and `features` are fixtures and are left out. Each is checked with the rules
|
|
496
|
+
of 3.0 (marked `[config]`).
|
|
497
|
+
- **Less noise.** A bare `rescue` or a call to `CGI.escape` is in every
|
|
498
|
+
application. The checks that also match code that has nothing to do with the
|
|
499
|
+
gem are applied only to the files whose code names `ApiRecipes` or calls one
|
|
500
|
+
of your APIs by its name, and the one about the `Hash` methods is left out
|
|
501
|
+
when the project has ActiveSupport.
|
|
502
|
+
- **A file that contains ERB is checked without running its code.** The value
|
|
503
|
+
that a `<%= ... %>` tag gives is not checked, and the command says how many
|
|
504
|
+
it left out; the other values are. A file where a tag gives a name, a
|
|
505
|
+
mapping or whole lines, or where a `<% ... %>` tag decides which lines exist,
|
|
506
|
+
is not guessed: the command says so, and reads it only with `--erb`, which
|
|
507
|
+
evaluates the file. That runs the code in it, and outside your application a
|
|
508
|
+
value read from the environment may be missing.
|
|
509
|
+
- **It gives the code to paste** where what to write is known. The values
|
|
510
|
+
that lines such as `ApiRecipes.print_urls = ...` and
|
|
511
|
+
`ApiRecipes.github.authorization = ...` gave when the application started
|
|
512
|
+
are put together in the whole block of `ApiRecipes.configure`, to paste over
|
|
513
|
+
the one that is there, with the file and its lines. Where what to write
|
|
514
|
+
depends on the code around it, the replacement is shown under the finding.
|
|
515
|
+
The code holds what the line assigned, the value of a credential too, so
|
|
516
|
+
that it works when pasted: `--no-code` leaves it out, as in a CI whose
|
|
517
|
+
output is kept. A line is never printed as it is, and the command copies
|
|
518
|
+
only what it is sure of: one whole expression, with no `if`, `unless`,
|
|
519
|
+
`rescue`, `and` or `or` after it, on a line that is always run.
|
|
492
520
|
|
|
493
521
|
```
|
|
494
522
|
app/client.rb:5: [breaks] endpoint.route was removed: use response.fill or endpoint.definition
|
|
495
523
|
app/client.rb:6: [check] a bare rescue now catches ApiRecipes errors too
|
|
496
524
|
config/apis/github.yml: [config] github: unknown option 'hots'
|
|
497
525
|
|
|
526
|
+
Checked 214 Ruby files, 6 of them use api_recipes
|
|
527
|
+
Checked 2 configuration files: config/apis/billing.yml, config/apis/github.yml
|
|
528
|
+
app/models/shop.rb:4: declares the api 'shopify', and no configuration file found here describes it
|
|
529
|
+
config/apis/billing.yml: 2 values come from ERB and were not checked
|
|
530
|
+
|
|
531
|
+
Code to paste
|
|
532
|
+
|
|
533
|
+
In config/initializers/api_recipes.rb, replace the block of ApiRecipes.configure (lines 1 to 3) with:
|
|
534
|
+
|
|
535
|
+
ApiRecipes.configure do |config|
|
|
536
|
+
config.apis_files_paths = Dir['config/apis/*.yml']
|
|
537
|
+
config.values = {
|
|
538
|
+
github: { authorization: "Bearer #{ENV['GITHUB_TOKEN']}" }
|
|
539
|
+
}
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
Then remove the lines that gave these values: config/initializers/github.rb:1
|
|
543
|
+
|
|
498
544
|
(the list of what the command cannot find)
|
|
499
545
|
|
|
500
546
|
3 findings
|
|
501
547
|
```
|
|
502
548
|
|
|
503
|
-
|
|
504
|
-
|
|
549
|
+
The line about `api :shopify` is said of a declaration in the code when no file
|
|
550
|
+
found in the project describes that API and the code never assigns
|
|
551
|
+
`apis_configs`: look for where its description comes from.
|
|
552
|
+
|
|
553
|
+
To check something else, name it: every directory is searched for `*.rb` and
|
|
554
|
+
`*.rake` files, a file you name is read whatever its name, and `--config FILE`,
|
|
555
|
+
once for each file, checks a description. Every check then applies to every
|
|
556
|
+
file.
|
|
557
|
+
|
|
558
|
+
$ api_recipes upgrade app lib --config config/apis/github.yml
|
|
559
|
+
|
|
560
|
+
The command then prints the list of changes that a search in the text
|
|
505
561
|
cannot find, which you check by hand (this guide has the details of each), and a
|
|
506
|
-
summary line: `Nothing to change`, `1 finding` or `N findings`.
|
|
507
|
-
|
|
562
|
+
summary line: `Nothing to change`, `1 finding` or `N findings`. When it found
|
|
563
|
+
no description of an API it says so instead of `Nothing to change`. The summary
|
|
564
|
+
line is left out when there is no finding and something could not be read.
|
|
508
565
|
|
|
509
566
|
A file or a directory that could not be read (missing, binary, not valid UTF-8,
|
|
510
567
|
or not readable for lack of permission) is reported on the error stream, as
|
|
@@ -513,6 +570,7 @@ least one finding, even if something could not be read; otherwise it is 2 when
|
|
|
513
570
|
something could not be read, and 0 when there is nothing to change. It can run
|
|
514
571
|
in a CI.
|
|
515
572
|
|
|
516
|
-
The command does not rewrite code
|
|
517
|
-
|
|
518
|
-
|
|
573
|
+
The command does not rewrite code: it gives the code to paste where it is sure
|
|
574
|
+
of it. What matters most depends on values that exist only when the program
|
|
575
|
+
runs, and an automatic rewrite would give a false sense of safety. For those,
|
|
576
|
+
use the warnings that the gem logs.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module CLI
|
|
5
|
+
# The code a command gives to paste: how it is shown, and the block of
|
|
6
|
+
# ApiRecipes.configure of a file, which a command gives whole.
|
|
7
|
+
module Code
|
|
8
|
+
# What a terminal would obey, or would show in another order.
|
|
9
|
+
UNSAFE = /[^[:print:]\n\t]|[---]/
|
|
10
|
+
|
|
11
|
+
# Colours for the code shown on a terminal. The text it is given holds
|
|
12
|
+
# nothing a terminal obeys.
|
|
13
|
+
module Paint
|
|
14
|
+
RUBY = /('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")|(#[^\n]*)|(\b[a-z_]\w*:(?!:))|((?<![:\w]):[a-z_]\w*)|(\b[A-Z]\w*)|(\b(?:nil|true|false|do|end)\b)/
|
|
15
|
+
|
|
16
|
+
def self.ruby(line)
|
|
17
|
+
line.gsub(RUBY) do
|
|
18
|
+
match = Regexp.last_match
|
|
19
|
+
code = if match[1] then 32
|
|
20
|
+
elsif match[2] then 2
|
|
21
|
+
elsif match[3] || match[4] then 36
|
|
22
|
+
elsif match[5] then 33
|
|
23
|
+
else 35
|
|
24
|
+
end
|
|
25
|
+
"\e[#{code}m#{match[0]}\e[0m"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.shell(line) = line.sub(/\A(\S+(?: exec \S+)?)/) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
|
|
30
|
+
|
|
31
|
+
# Whether out is a terminal that takes colours.
|
|
32
|
+
def self.on?(out) = out.respond_to?(:tty?) && out.tty? && !ENV.key?('NO_COLOR')
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Prints a block of code, indented, in colours on a terminal.
|
|
36
|
+
def self.show(out, lines, kind = :ruby)
|
|
37
|
+
paint = Paint.on?(out)
|
|
38
|
+
out.puts
|
|
39
|
+
lines.each { |line| out.puts(line.empty? ? '' : " #{paint ? Paint.public_send(kind, line) : line}") }
|
|
40
|
+
out.puts
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Ruby read as a tree, never run. nil when the text is not valid Ruby.
|
|
44
|
+
def self.tree(text)
|
|
45
|
+
require 'prism'
|
|
46
|
+
result = Prism.parse(text)
|
|
47
|
+
result.success? ? result.value : nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Every node of a tree for which the block is true.
|
|
51
|
+
def self.nodes(node, found = [], &wanted)
|
|
52
|
+
return found if node.nil?
|
|
53
|
+
|
|
54
|
+
found << node if wanted.call(node)
|
|
55
|
+
node.compact_child_nodes.each { |child| nodes(child, found, &wanted) }
|
|
56
|
+
found
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# text with each [from, to, replacement] applied; from and to count
|
|
60
|
+
# bytes, as the tree does.
|
|
61
|
+
def self.edited(text, edits)
|
|
62
|
+
bytes = text.b
|
|
63
|
+
edits.sort_by { |from, _, _| -from }.each { |from, to, replacement| bytes[from...to] = replacement.b }
|
|
64
|
+
bytes.force_encoding(Encoding::UTF_8)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The one call `ApiRecipes.configure do |config| ... end` of a text, read
|
|
68
|
+
# from its tree: a comment or a string that looks like one is not one.
|
|
69
|
+
class ConfigureBlock
|
|
70
|
+
# first, last: the lines of the call and of the end of its block,
|
|
71
|
+
# counted from 1. problem: why the block cannot be edited, or nil.
|
|
72
|
+
attr_reader :config, :first, :last, :problem, :call
|
|
73
|
+
|
|
74
|
+
# nil unless text is valid Ruby with exactly one such call.
|
|
75
|
+
def self.in(text)
|
|
76
|
+
tree = text && Code.tree(text)
|
|
77
|
+
calls = tree ? Code.nodes(tree) { |node| configure?(node) } : []
|
|
78
|
+
calls.size == 1 ? new(text, calls.first) : nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.configure?(node)
|
|
82
|
+
node.is_a?(Prism::CallNode) && node.name == :configure && node.receiver.is_a?(Prism::ConstantReadNode) &&
|
|
83
|
+
node.receiver.name == :ApiRecipes
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def initialize(text, call)
|
|
87
|
+
@text = text
|
|
88
|
+
@lines = text.lines
|
|
89
|
+
@call = call
|
|
90
|
+
block = call.block
|
|
91
|
+
parameters = block.is_a?(Prism::BlockNode) && block.parameters.is_a?(Prism::BlockParametersNode) ? block.parameters.parameters : nil
|
|
92
|
+
names = parameters ? parameters.requireds : []
|
|
93
|
+
@first = call.location.start_line
|
|
94
|
+
@last = block.is_a?(Prism::BlockNode) ? block.closing_loc.start_line : @first
|
|
95
|
+
@config = names.size == 1 && names.first.respond_to?(:name) ? names.first.name.to_s : nil
|
|
96
|
+
@statements = block.is_a?(Prism::BlockNode) && block.body.is_a?(Prism::StatementsNode) ? block.body.body : []
|
|
97
|
+
@problem = problem_of(block)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The statements of the block that assign config.name.
|
|
101
|
+
def assignments(name)
|
|
102
|
+
@statements.select do |node|
|
|
103
|
+
node.is_a?(Prism::CallNode) && node.name == :"#{name}=" && node.receiver.is_a?(Prism::LocalVariableReadNode) &&
|
|
104
|
+
node.receiver.name.to_s == @config
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Whether config.name is assigned anywhere in the block, also where
|
|
109
|
+
# the block does not always run it.
|
|
110
|
+
def assigned_anywhere?(name)
|
|
111
|
+
Code.nodes(@call.block) { |node| node.is_a?(Prism::CallNode) && node.name == :"#{name}=" }.any?
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The lines where the statements of the block start.
|
|
115
|
+
def statement_lines = @statements.map { |node| node.location.start_line }
|
|
116
|
+
|
|
117
|
+
# The line that assigns config.name, counted from 1, or nil.
|
|
118
|
+
def line_of(name) = assignments(name).first&.location&.start_line
|
|
119
|
+
|
|
120
|
+
# The indentation of the statements of the block.
|
|
121
|
+
def indent
|
|
122
|
+
line = @statements.empty? ? nil : @lines[@statements.first.location.start_line - 1]
|
|
123
|
+
line ? line[/\A[ \t]*/] : "#{@lines[@last - 1][/\A[ \t]*/]} "
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Where a new statement goes: the byte at which the line of the end
|
|
127
|
+
# of the block starts.
|
|
128
|
+
def end_offset = @call.block.closing_loc.start_offset - @call.block.closing_loc.start_column
|
|
129
|
+
|
|
130
|
+
# The whole call as lines of text, without the lines numbered in
|
|
131
|
+
# without and with added before its end, indented as the block is.
|
|
132
|
+
def lines(added = [], without: [])
|
|
133
|
+
kept = (@first...@last - 1).reject { |index| without.include?(index + 1) }.map { |index| @lines[index].chomp }
|
|
134
|
+
[@lines[@first - 1].chomp, *kept, *added.map { |line| line.empty? ? '' : "#{indent}#{line}" }, @lines[@last - 1].chomp]
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
private
|
|
138
|
+
|
|
139
|
+
def problem_of(block)
|
|
140
|
+
return 'is not given a block with do and end' unless block.is_a?(Prism::BlockNode)
|
|
141
|
+
return 'does not take its one parameter, as in `do |config|`' unless @config
|
|
142
|
+
return 'is written on one line' if @first == @last
|
|
143
|
+
return 'has a rescue or an ensure' unless block.body.nil? || block.body.is_a?(Prism::StatementsNode)
|
|
144
|
+
early = Code.nodes(block) { |node| [Prism::NextNode, Prism::BreakNode, Prism::ReturnNode].any? { |kind| node.is_a?(kind) } }
|
|
145
|
+
return 'can end before its last line, with next, break or return' unless early.empty?
|
|
146
|
+
return 'does not end with a line of its own' unless @lines[@last - 1].strip.match?(/\A(end|\})\z/)
|
|
147
|
+
|
|
148
|
+
nil
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|