api_recipes 3.0.0.beta3 → 3.0.0.beta5

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 (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +377 -76
  3. data/UPGRADING.md +223 -59
  4. data/lib/api_recipes/cli/calls.rb +27 -0
  5. data/lib/api_recipes/cli/code.rb +153 -0
  6. data/lib/api_recipes/cli/convert/migration.rb +819 -0
  7. data/lib/api_recipes/cli/convert/snippet.rb +67 -0
  8. data/lib/api_recipes/cli/convert.rb +614 -0
  9. data/lib/api_recipes/cli/description.rb +65 -0
  10. data/lib/api_recipes/cli/import/server.rb +20 -9
  11. data/lib/api_recipes/cli/import/writer.rb +16 -4
  12. data/lib/api_recipes/cli/import.rb +23 -9
  13. data/lib/api_recipes/cli/recipes/summary.rb +25 -6
  14. data/lib/api_recipes/cli/recipes.rb +5 -2
  15. data/lib/api_recipes/cli/routes.rb +63 -8
  16. data/lib/api_recipes/cli/tags.rb +19 -0
  17. data/lib/api_recipes/cli/upgrade/fix.rb +206 -0
  18. data/lib/api_recipes/cli/upgrade/project.rb +194 -0
  19. data/lib/api_recipes/cli/upgrade/screen.rb +165 -0
  20. data/lib/api_recipes/cli/upgrade/tree_rules.rb +290 -0
  21. data/lib/api_recipes/cli/upgrade.rb +706 -57
  22. data/lib/api_recipes/cli.rb +171 -17
  23. data/lib/api_recipes/configuration.rb +17 -95
  24. data/lib/api_recipes/endpoint.rb +1 -1
  25. data/lib/api_recipes/internals/compiler.rb +102 -67
  26. data/lib/api_recipes/internals/context.rb +35 -6
  27. data/lib/api_recipes/internals/declaration.rb +17 -15
  28. data/lib/api_recipes/internals/definition.rb +16 -3
  29. data/lib/api_recipes/internals/option_value.rb +92 -0
  30. data/lib/api_recipes/internals/recipe.rb +1 -1
  31. data/lib/api_recipes/internals/recipe_lock.rb +12 -9
  32. data/lib/api_recipes/internals/request.rb +17 -6
  33. data/lib/api_recipes/version.rb +1 -1
  34. data/lib/api_recipes.rb +1 -0
  35. metadata +13 -1
data/README.md CHANGED
@@ -69,10 +69,15 @@ Or install it directly:
69
69
 
70
70
  ## Quick start
71
71
 
72
- **1. Describe the API** in a YAML file, for example `config/apis/github.yml`:
72
+ **1. Describe the API** in a recipe, a YAML file called after the API, for
73
+ example `config/api_recipes/github.recipe.yml`:
73
74
 
74
75
  ```yaml
75
76
  github:
77
+ recipe:
78
+ name: github
79
+ version: 1.0.0
80
+ format: 1
76
81
  host: api.github.com
77
82
  endpoints:
78
83
  users: # GET /users
@@ -89,7 +94,7 @@ github:
89
94
  require 'api_recipes'
90
95
 
91
96
  ApiRecipes.configure do |config|
92
- config.apis_files_paths = ['config/apis/github.yml']
97
+ config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
93
98
  end
94
99
  ```
95
100
 
@@ -296,8 +301,10 @@ All options are listed in the [options reference](#options-reference).
296
301
  ### Headers and parameters sent with every request
297
302
 
298
303
  `default_headers` are sent with every request, and `default_params` are added
299
- to every query string. Both can also be written on an endpoint: there they hold
300
- for that endpoint and for the ones below it, on top of those of the API.
304
+ to every query string. Both can also be written on an endpoint. A request
305
+ looks for them up the tree: on its endpoint, then on each endpoint that
306
+ contains it, then on the API, and sends what it finds, the nearest winning
307
+ for a name written more than once.
301
308
 
302
309
  ```yaml
303
310
  billing:
@@ -345,10 +352,14 @@ request must be a Hash.
345
352
  ## Variables
346
353
 
347
354
  A variable is a `{name}` placeholder in the `host`, the `base_url`, the `path`
348
- of an endpoint, or the value of a header or of a parameter sent with every
349
- request. Use it for what changes from one account or one environment to the
350
- next, such as the address of a shop. Declare the variables of the API, with an
351
- optional `default` and `description`:
355
+ of an endpoint, the value of a header or of a parameter sent with every
356
+ request, or in one of the options that are read when a request is sent:
357
+ `protocol`, `port`, `timeout`, `on_bad_code`, `mime_type`, `verb` and
358
+ `ok_code`. Use it for what changes from one account or one environment to the
359
+ next, such as the address of a shop. Every `{name}` that the API uses is one of
360
+ its variables. Declare a variable under `variables` to give it a `default` or a
361
+ `description`; one with neither needs no declaration, and a value must be
362
+ given to it:
352
363
 
353
364
  ```yaml
354
365
  shopify:
@@ -357,8 +368,6 @@ shopify:
357
368
  description: the part of the shop address before .myshopify.com
358
369
  api_version:
359
370
  default: '2026-10'
360
- location:
361
- description: the id of the location this client works for
362
371
  host: '{shop}.myshopify.com'
363
372
  base_url: admin/api/{api_version}
364
373
  auth:
@@ -370,6 +379,8 @@ shopify:
370
379
  path: /locations/{location}/inventory_levels
371
380
  ```
372
381
 
382
+ Here `location` is a variable too, with no default.
383
+
373
384
  A variable without a value and without a default raises
374
385
  `ApiRecipes::MissingVariable` as soon as it is needed: when the chain reaches
375
386
  the `base_url` or the path that uses it, and at the latest when the request is
@@ -424,8 +435,8 @@ store.shopify.products.url.to_s
424
435
  ```
425
436
 
426
437
  The options that a class gives to `api` can use the variables of the API, as in
427
- `api :shopify, host: '{shop}.staging.example.test'`. When an API exists only in
428
- a class, the names given in `variables:` are its variables, so each one gets a
438
+ `api :shopify, host: '{shop}.staging.example.test'`, and new ones: a `{name}`
439
+ they use is a variable of the API for that class. `variables:` gives them a
429
440
  value there, and an object or `with` can give another:
430
441
 
431
442
  ```ruby
@@ -469,6 +480,48 @@ every thread. That is why it is given only where the application starts, to
469
480
  while requests are served goes to `with` or to an object. The same holds for
470
481
  credentials.
471
482
 
483
+ A service whose address changes with the environment is described once, and
484
+ each environment gives its values:
485
+
486
+ ```yaml
487
+ billing:
488
+ variables:
489
+ protocol:
490
+ default: https
491
+ host:
492
+ default: billing.example.com
493
+ port:
494
+ default: 443
495
+ timeout:
496
+ default: 5
497
+ protocol: '{protocol}'
498
+ host: '{host}'
499
+ port: '{port}'
500
+ timeout: '{timeout}'
501
+ endpoints:
502
+ invoices:
503
+ ```
504
+
505
+ ```ruby
506
+ ApiRecipes.configure do |config|
507
+ config.values = { billing: { variables: { protocol: 'http', host: 'localhost', port: 3000 } } }
508
+ end
509
+
510
+ ApiRecipes.billing.invoices.url.to_s
511
+ # => "http://localhost:3000/invoices"
512
+ ```
513
+
514
+ In these options the variable gives a value of the option: `http` or `https`
515
+ for the protocol, an integer from 1 to 65535 for the port, a positive number
516
+ for the timeout, `raise` or `ignore`, a verb, a status. It is made when the
517
+ request is built, and a value that is not one raises
518
+ `ApiRecipes::InvalidVariable` before anything is sent, as the default of a
519
+ variable that gives the whole option raises `ApiRecipes::ConfigurationError`
520
+ when the configuration is loaded. `definition`, `routes` and the `api_recipes routes` command show such
521
+ an option as it is written. What decides the shape of the tree is read when
522
+ the configuration is loaded and takes no variable: `endpoints`, `resource`,
523
+ `route`, the names, `verify_with` and `auth`.
524
+
472
525
  The value of a variable is a String, a Symbol or a number, and is kept as
473
526
  text. A value is checked for the place where it ends up. In a `host` it can contain
474
527
  only letters, digits, dots, hyphens and underscores. In a `base_url` and in a
@@ -478,8 +531,11 @@ In a parameter it is encoded like every value of a query string. Anything else
478
531
  raises `ApiRecipes::InvalidVariable`.
479
532
  A word in braces that is not a variable is written `\{word}` in a path and in
480
533
  a `base_url`.
481
- A name that the API does not declare raises `ApiRecipes::ConfigurationError` at
482
- every level, so a misspelled name is never ignored.
534
+ A value given for a name that the API neither declares nor uses raises
535
+ `ApiRecipes::ConfigurationError` at every level, so a name misspelled in your
536
+ code is never ignored. A `{name}` misspelled in the description is a variable
537
+ with no default: the first request that needs it raises
538
+ `ApiRecipes::MissingVariable`, which names it.
483
539
  `variables` without an assignment returns the values of the level you call it
484
540
  on: those of `config.values` on `ApiRecipes.shopify`, those given to `api` on a
485
541
  class, those assigned on an object. On an object, assigning `nil` clears them,
@@ -489,36 +545,52 @@ and a default declared in the file applies again.
489
545
 
490
546
  Configure ApiRecipes once, when your application starts.
491
547
 
492
- ### From YAML files
548
+ ### From recipes
493
549
 
494
550
  ```ruby
495
551
  ApiRecipes.configure do |config|
496
- config.apis_files_paths = ['config/apis/github.yml', 'config/apis/billing.yml']
552
+ config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
553
+ config.values = {
554
+ billing: { variables: { billing_host: ENV.fetch('BILLING_HOST', 'billing.example.com') } }
555
+ }
497
556
  end
498
557
  ```
499
558
 
500
- A file can describe one API or several. If two files describe the same API, the
501
- later one replaces the earlier one entirely. The same holds for the sources
502
- together: they are applied in the order they are assigned, so assigning
503
- `apis_configs` after `apis_files_paths` lets the Hash replace the file.
504
-
505
- A file that is missing, empty, not valid YAML or not valid UTF-8 raises
506
- `ApiRecipes::RecipeFileError`, which names the file. A file called
507
- `something.recipe.yml` is refused here: it is a [portable recipe](#portable-recipes)
508
- and goes in `recipes_paths`, where it is never evaluated with ERB.
509
-
510
- Files are evaluated with ERB before being parsed, so values can come from the
511
- environment:
559
+ A recipe is a file called `NAME.recipe.yml` that describes the API called
560
+ `NAME`, with the key `recipe` that gives its name, its version and the format.
561
+ It holds data and no code: it is never evaluated with ERB, and `<%` anywhere in
562
+ it is an error. What changes from one environment to another is a
563
+ [variable](#variables), and the application gives its value with
564
+ `config.values`:
512
565
 
513
566
  ```yaml
514
567
  billing:
515
- host: <%= ENV.fetch('BILLING_HOST', 'billing.example.com') %>
516
- timeout: <%= ENV.fetch('BILLING_TIMEOUT', 3) %>
568
+ recipe:
569
+ name: billing
570
+ version: 1.0.0
571
+ format: 1
572
+ variables:
573
+ billing_host:
574
+ default: billing.example.com
575
+ host: '{billing_host}'
517
576
  endpoints:
518
577
  invoices:
519
578
  ```
520
579
 
521
- Only files you wrote and trust should be loaded: ERB runs the Ruby code in them.
580
+ A recipe you write in your project is loaded as it is written, and you change
581
+ it as any other file. One that you took from a repository with
582
+ `api_recipes install` is also compared with `recipes.lock`: see
583
+ [Portable recipes](#portable-recipes). The rules of the format are under
584
+ [Writing a portable recipe](#writing-a-portable-recipe), and
585
+ `api_recipes check FILE` says whether a file follows them.
586
+
587
+ A file that is missing, not valid YAML, not valid UTF-8 or not a recipe raises
588
+ `ApiRecipes::RecipeFileError`, which names the file.
589
+
590
+ 2.x loaded YAML files evaluated with ERB through `apis_files_paths`. That
591
+ option was removed: assigning it raises `ApiRecipes::Error`, and
592
+ `api_recipes convert` moves those files to recipes. See
593
+ [From a file of your project](#from-a-file-of-your-project).
522
594
 
523
595
  ### From a Hash
524
596
 
@@ -545,11 +617,11 @@ configuration stays active.
545
617
  When an API publishes an OpenAPI description, the `api_recipes` command writes
546
618
  the recipe for you:
547
619
 
548
- $ api_recipes import openapi.yml --name github --output config/apis/github.yml
620
+ $ api_recipes import openapi.yml --name github --output config/api_recipes/github.recipe.yml
549
621
 
550
622
  The description is read once, when you run the command. Your application never
551
- reads it: it loads the recipe, which is a file like the ones above, and you can
552
- read it, trim it and rename things in it. These operations
623
+ reads it: it loads the recipe, which is a file like the ones above, complete with its
624
+ `recipe` key, and you can read it, trim it and rename things in it. These operations
553
625
 
554
626
  ```
555
627
  GET /users
@@ -566,6 +638,10 @@ become this recipe:
566
638
  ```
567
639
  # Generated by api_recipes import from GitHub 1.0
568
640
  github:
641
+ recipe:
642
+ name: github
643
+ version: 1.0.0
644
+ format: 1
569
645
  host: api.github.com
570
646
  endpoints:
571
647
  users: # GET /users (users/list)
@@ -605,12 +681,24 @@ github:
605
681
 
606
682
  | Option | Meaning |
607
683
  | --- | --- |
608
- | `--name NAME` | the name of the API in the recipe. Default: the title of the description, in lower case with `_` |
684
+ | `--name NAME` | the name of the API and of the recipe: lowercase letters, digits and `_`. Default: the title of the description, in lower case with `_` |
685
+ | `--version VERSION` | the version of the recipe. Default: `1.0.0` |
609
686
  | `--only GLOB` | only the paths that match; can be repeated. `*` also crosses `/`: `--only 'repos/*'` |
610
687
  | `--except GLOB` | leaves out the paths that match; can be repeated |
611
688
  | `--server N` | which entry of `servers` gives the address, counted from 0. Default: the first |
689
+ | `--calls` | writes under each endpoint how it is called in Ruby |
612
690
  | `--output FILE` | writes the recipe in FILE, which must not exist, instead of on STDOUT |
613
691
 
692
+ With `--calls`, who reads the recipe sees the call beside the request it
693
+ sends. The call starts after the name of the API, as in `github.users.show('octocat')`:
694
+
695
+ users: # GET /users (users/list)
696
+ # .users
697
+ endpoints:
698
+ show: # GET /users/{username} (users/get-by-username)
699
+ # .users.show(username)
700
+ path: "/:username"
701
+
614
702
  The input is one local file, YAML or JSON, OpenAPI 3.0 or 3.1. Warnings and a
615
703
  summary go to STDERR, so the recipe can be redirected. The command exits with 1
616
704
  when the description cannot be imported and with 2 when the arguments are wrong.
@@ -620,6 +708,7 @@ when the description cannot be imported and with 2 when the arguments are wrong.
620
708
  | From the description | In the recipe |
621
709
  | --- | --- |
622
710
  | the address of the server | `protocol`, `host`, `port`, `base_url` |
711
+ | no server, or an address without a host | `host: '{host}'`, a variable that the application gives with `config.values` |
623
712
  | the variables of the server | `variables`, with their defaults |
624
713
  | the paths | the tree of endpoints |
625
714
  | the path parameters | `:name` placeholders |
@@ -681,30 +770,34 @@ A portable recipe is a recipe made to be shared: one API in one file, and
681
770
  nothing but data. You install it with a command that shows what it does before
682
771
  it writes anything, and you load it with `recipes_paths`.
683
772
 
684
- $ api_recipes install github
685
- github 1.0.0
686
- summary: GitHub REST API, users, repositories, issues and pull requests
773
+ $ api_recipes install github_essentials
774
+ reading github_essentials from https://github.com/madAle/api_recipes-cookbook
775
+ github_essentials 1.0.0
776
+ summary: GitHub REST API, the most used calls on users, repositories, issues and pull requests
687
777
  homepage: https://docs.github.com/en/rest
688
- from: https://github.com/madAle/api_recipes-cookbook, commit a4e8dd017d05
778
+ from: https://github.com/madAle/api_recipes-cookbook, commit cd19ca58aea1
689
779
  address: https://api.github.com
690
780
  credential: in the Authorization header
691
781
  headers: Accept: application/vnd.github+json
692
782
  calls: 14, of which 2 are not GET:
693
- POST github.repos.issues(owner, repo).create /repos/:owner/:repo/issues
694
- POST github.repos.issues(owner, repo).comments(issue_number).create /repos/:owner/:repo/issues/:issue_number/comments
695
- Install github 1.0.0 in config/api_recipes? [y/N] y
696
- github 1.0.0 is installed in config/api_recipes/github.recipe.yml
783
+ POST github_essentials.repos.issues(owner, repo).create /repos/:owner/:repo/issues
784
+ POST github_essentials.repos.issues(owner, repo).comments(issue_number).create /repos/:owner/:repo/issues/:issue_number/comments
785
+ Install github_essentials 1.0.0 in config/api_recipes? [y/N] y
786
+ github_essentials 1.0.0 is installed in config/api_recipes/github_essentials.recipe.yml
697
787
 
698
788
  ```ruby
699
789
  ApiRecipes.configure do |config|
700
790
  config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
701
791
  end
702
792
 
703
- ApiRecipes.github.users.show('octocat').run
793
+ ApiRecipes.github_essentials.users.show('octocat').run
704
794
  ```
705
795
 
796
+ `github_essentials` has the most used calls of the GitHub API; the recipe
797
+ called `github` has every operation, 1,281 calls with the same names.
798
+
706
799
  The command writes two files in `config/api_recipes`: the recipe,
707
- `github.recipe.yml`, and `recipes.lock`, which records the version, the
800
+ `github_essentials.recipe.yml`, and `recipes.lock`, which records the version, the
708
801
  repository, the commit and the checksum of each installed recipe. Commit both
709
802
  to your repository: the application downloads nothing when it starts.
710
803
 
@@ -716,7 +809,7 @@ layout.
716
809
  ### Installing, updating and checking
717
810
 
718
811
  $ api_recipes install github # the recipe at the head of the cookbook
719
- $ api_recipes install github --version 1.0.0 # the one at the tag github-1.0.0
812
+ $ api_recipes install github --version 2.0.0 # the one at the tag github-2.0.0
720
813
  $ api_recipes install billing --from https://github.com/acme/api-recipes
721
814
  $ api_recipes install ./billing.recipe.yml # a file of yours
722
815
  $ api_recipes update github
@@ -801,15 +894,21 @@ this when it runs: the installed recipes are files of your project.
801
894
 
802
895
  ### What `configure` checks
803
896
 
804
- Every file of `recipes_paths` must be called `NAME.recipe.yml` and must be in
805
- the `recipes.lock` of its folder with the checksum it has now. A file that was
806
- changed after it was installed, or that was never installed, raises
807
- `ApiRecipes::ConfigurationError` and the previous configuration stays:
897
+ Every file of `recipes_paths` must be called `NAME.recipe.yml` and follow the
898
+ rules of the format. A recipe that `recipes.lock` ties to a repository, because
899
+ `install` took it from one, must also have the checksum the lock records. When
900
+ it was changed afterwards, `configure` raises `ApiRecipes::ConfigurationError`
901
+ and the previous configuration stays:
808
902
 
809
- config/api_recipes/github.recipe.yml: was changed after it was installed: its checksum is not the one in recipes.lock. To accept the file as it is now, run `api_recipes install config/api_recipes/github.recipe.yml`
903
+ config/api_recipes/github.recipe.yml: was changed after it was installed from its repository: its checksum is not the one in recipes.lock. To keep the file as it is now, as a recipe of the project, run `api_recipes install config/api_recipes/github.recipe.yml`
810
904
 
811
- A recipe never replaces an API: a name that `apis_configs`, `apis_files_paths`
812
- or another recipe already defines is an error. Everything else works as for any
905
+ A recipe that the lock does not tie to a repository is a recipe of the project:
906
+ one you wrote, one that `convert` or `import` wrote, or one you accepted with
907
+ `install FILE` after changing it. It is loaded as it is written, with nothing
908
+ to compare it with, and `update` leaves it alone.
909
+
910
+ A recipe never replaces an API: a name that `apis_configs` or another recipe
911
+ already defines is an error. Everything else works as for any
813
912
  API: credentials, variables, `with`, and `api :github` in a class. To add a
814
913
  `verify_with`, which a recipe cannot carry, declare it in your code:
815
914
 
@@ -883,6 +982,170 @@ A repository of recipes keeps each one in `recipes/NAME.recipe.yml`. A
883
982
  published version is the tag `NAME-VERSION`, for example `billing-1.2.0`.
884
983
  `api_recipes routes billing.recipe.yml` lists the calls of a recipe.
885
984
 
985
+ ### From a file of your project
986
+
987
+ When the API is already described in a file of your project, `convert` moves
988
+ it to a portable recipe. Run it in the root of the project: a Rails
989
+ application or any other Ruby project, wherever `ApiRecipes.configure` is
990
+ called. With no file it moves every API that a file of the project describes,
991
+ one after the other, which is what a project that comes from 2.x needs:
992
+
993
+ $ bundle exec api_recipes convert
994
+
995
+ With a file it moves the API of that file:
996
+
997
+ $ bundle exec api_recipes convert config/apis/billing.yml.erb
998
+ billing 1.0.0 now comes from a portable recipe: 2 calls
999
+
1000
+ Changed in the project
1001
+ created config/api_recipes/billing.recipe.yml, with its line in config/api_recipes/recipes.lock
1002
+ edited config/initializers/api_recipes.rb: recipes_paths and config.values; apis_files_paths, which loads nothing now, is gone
1003
+ edited app/models/invoice.rb: verify_with on the declaration of the api
1004
+ deleted config/apis/billing.yml.erb
1005
+
1006
+ What changed in the description
1007
+ host: the ERB tag is now the variable {billing_host}, with the default written in the tag
1008
+ port: the ERB tag is now the variable {billing_port}, with the default written in the tag
1009
+ verify_with: left out, a recipe cannot carry it: give it where a class declares the api, as `api :billing, verify_with: ...`
1010
+ default_headers.Api-Key-Client: the ERB tag is now the variable {billing_client}; the value is not in the recipe
1011
+
1012
+ Every change is in `git status` and `git diff`. Check the recipes with `api_recipes check`, then start the application.
1013
+
1014
+ The project works as before, and the values still come from where they came:
1015
+ the code of each tag is moved, as it is written, to `config.values`.
1016
+
1017
+ ```ruby
1018
+ ApiRecipes.configure do |config|
1019
+ config.values = {
1020
+ billing: { variables: {
1021
+ billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
1022
+ billing_port: (ENV['BILLING_PORT'] || 443),
1023
+ billing_client: ENV['BILLING_CLIENT']
1024
+ }.compact }
1025
+ }
1026
+ config.recipes_paths = Dir[File.expand_path('../api_recipes/*.recipe.yml', __dir__)]
1027
+ end
1028
+ ```
1029
+
1030
+ What the command does:
1031
+
1032
+ - **It writes the recipe**, with the key `recipe` (the name of the API,
1033
+ `--version`, `1.0.0` by default, and the format), and installs it in
1034
+ `config/api_recipes` with its line in `recipes.lock`. When the file describes
1035
+ several APIs, `--name` chooses one. Anchors, aliases and merge keys are
1036
+ written out.
1037
+ - **An `<%= ... %>` tag becomes a variable**, in every place where a recipe
1038
+ can have one: the `host`, the `base_url`, a `path`, the value of a header or
1039
+ of a parameter, and the `protocol`, `port`, `timeout`, `on_bad_code`,
1040
+ `mime_type`, `verb` and `ok_code` when the tag gives the whole value. The
1041
+ variable is named after the environment variable that the tag reads, or
1042
+ after its place, and a default written in the tag as plain text or as a
1043
+ number (`ENV.fetch('NAME', 'text')`, `ENV['NAME'] || 443`) becomes the
1044
+ default of the variable. A default that a tag gave to a variable the file
1045
+ declares is given by the application from now on. Only a variable with a
1046
+ default is declared in the recipe.
1047
+ - **A word in braces stays text**, as 2.x sent it, unless the file declares a
1048
+ variable with that name: it is written `\{word}` in a `path` and in the
1049
+ `base_url`, and a header or a parameter that holds one is given by a
1050
+ variable whose default is its text.
1051
+ - **`--calls` writes beside each endpoint how it is called in Ruby**, as
1052
+ `show: # .invoices.show(id)`, after the name of the API. The recipe is the
1053
+ same with or without them.
1054
+ - **Nothing in the file is run**, and there is no option that runs it. The
1055
+ code of a tag goes to `config.values` as text, and each tag has its own
1056
+ variable, also two tags in one value. `.compact` leaves out a variable whose
1057
+ code gives `nil`: the default of the recipe applies when the tag had one
1058
+ written, and otherwise the request says which variable is missing, where
1059
+ 2.x went on with the default of the gem.
1060
+ - **A credential is never copied into the recipe.** A header or a parameter
1061
+ whose name contains `auth`, `key`, `token`, `secret`, `passw`, `credential`,
1062
+ `signature`, `cookie` or `session` becomes a variable without a default,
1063
+ unless the file already gives it with a variable it declares, and
1064
+ its value, written in the file or given by a tag, goes to `config.values`.
1065
+ The command cannot know a secret written anywhere else: read the recipe
1066
+ before you share it.
1067
+ - **The old description is removed**: the file is deleted, or only the lines
1068
+ of that API when it describes others, because a recipe never takes the place
1069
+ of an API that another source defines. With the last description of the
1070
+ project the lines of `config.apis_files_paths` are taken out, when git has
1071
+ the file as it is now or as this run of the command left it, unless they
1072
+ still name a file that describes an API, as one in `vendor` that the command
1073
+ does not look at; otherwise the command tells you to take them out, because
1074
+ 3.0 refuses the option.
1075
+ - **`verify_with`**, which a recipe cannot carry, is given to each `api :name`
1076
+ that a class or a module declares. One that a tag gave, or that an endpoint
1077
+ had, is left to you, as it is when one of the declarations gives its options
1078
+ in a way the command cannot read, such as `**options`.
1079
+ - **When it changes the project it prints names and paths**, nothing of what
1080
+ the file holds.
1081
+
1082
+ It changes your files only when that is safe, and all at once or not at all:
1083
+
1084
+ - Lines are added only to a plain file of the project that is in the last
1085
+ commit of git. A file is deleted, or lines are taken out of it, only when
1086
+ git has it committed as it is now: the command works out the id of the
1087
+ file and compares it with the one in the index and in the last commit, so
1088
+ that no filter of the repository runs. A file that differs only by CR LF
1089
+ at the end of its lines, as git writes it with `core.autocrlf`, counts as
1090
+ committed. A symbolic link, and a file that git was told to skip, are never
1091
+ changed. Outside a git repository the project is not changed.
1092
+ - Ruby is read as a tree, never matched as lines and never run, and the
1093
+ command edits only what it can read whole:
1094
+ - the call to `ApiRecipes.configure`, when the project has exactly one, with
1095
+ a block `do |config|` on several lines that has no `rescue` and cannot end
1096
+ early with `next`, `break` or `return`;
1097
+ - `config.values`, which is added, or which gets one more entry when it is
1098
+ given a Hash between braces whose keys are all written as names, none of
1099
+ them the API, and whose closing brace starts a line;
1100
+ - `config.apis_files_paths`, while other files still describe an API: it
1101
+ must be given patterns of `Dir`, or a list between brackets of names
1102
+ written as text, from which the one element that names the file is taken
1103
+ out. With the last description the whole assignment goes, unless it still
1104
+ names another file that describes an API.
1105
+
1106
+ Anything else stops it: another mention of the old file in the code, another
1107
+ file that describes the API, a `recipes_paths` it cannot read.
1108
+ - Every Ruby file it changes must still be Ruby with the change, and when a
1109
+ write fails every file is put back as it was. A file that changed after the
1110
+ command read it is not written over. The recipe is not written below a
1111
+ symbolic link.
1112
+ - When a step is not possible, nothing is written, the command says why, and
1113
+ it gives the steps to do by hand with the code to paste: the commands that
1114
+ write and install the recipe, the lines for the block of
1115
+ `ApiRecipes.configure` with the file and the line, the line that gives
1116
+ `verify_with`, and what to remove.
1117
+
1118
+ 2. In config/initializers/api_recipes.rb, inside the block of ApiRecipes.configure (line 1), add:
1119
+
1120
+ config.recipes_paths = Dir[File.expand_path('../api_recipes/*.recipe.yml', __dir__)]
1121
+ config.values = {
1122
+ billing: { variables: {
1123
+ billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
1124
+ billing_port: (ENV['BILLING_PORT'] || 443),
1125
+ billing_client: ENV['BILLING_CLIENT']
1126
+ }.compact }
1127
+ }
1128
+
1129
+ The code is what the file holds, the code of each tag and each value that
1130
+ was written, so that what you paste works; on a terminal it is shown in
1131
+ colours. Only what a terminal would obey is left out, with a comment that
1132
+ says the line to copy it from.
1133
+ - What it could not do by itself after changing the project is listed under
1134
+ `Left to you`: a `verify_with` when no class declares the API, or on an
1135
+ endpoint.
1136
+ - A tag that is not `<%= ... %>`, that gives the name of an option, or that
1137
+ gives a part of the protocol, of the port or of another such option, stops
1138
+ the command, which says where.
1139
+
1140
+ To write the recipe alone, as for the repository of the recipes, name a
1141
+ folder: the project is not touched, and the command gives the code for the
1142
+ block of `ApiRecipes.configure`.
1143
+
1144
+ $ api_recipes convert config/apis/billing.yml.erb --to ../api-recipes/recipes
1145
+
1146
+ The command does not publish: the recipe reaches a repository with `git`,
1147
+ through the review that every recipe gets.
1148
+
886
1149
  ## Where an API is available
887
1150
 
888
1151
  ### On the `ApiRecipes` module
@@ -1259,7 +1522,7 @@ body, a credential or a query string: the body is on `error.response`.
1259
1522
  | `ApiRecipes::CredentialOriginMismatch` | a request would send a credential to an origin other than the one it was set for |
1260
1523
  | `ApiRecipes::InvalidUrl` | the address that would be contacted is not the one that was validated |
1261
1524
  | `ApiRecipes::ConfigurationError` | the configuration or the options of `with` or `follow` are not valid; has `errors`, the list of problems |
1262
- | `ApiRecipes::RecipeFileError` | a configuration file cannot be read; a kind of `ConfigurationError` that names the file |
1525
+ | `ApiRecipes::RecipeFileError` | a recipe file cannot be read; a kind of `ConfigurationError` that names the file |
1263
1526
  | `ApiRecipes::ApiNameClashError` | an API is declared twice in a class hierarchy, or `api :name` names a method the class already has |
1264
1527
  | `ApiRecipes::ApiConfigIsNotAnHash` | the second argument of `api` is not a Hash |
1265
1528
  | `HTTP::TimeoutError`, `HTTP::ConnectionError` | http.rb network errors |
@@ -1416,7 +1679,7 @@ ApiRecipes does not check its origin, so prefer the levels above.
1416
1679
  ApiRecipes.configure do |config|
1417
1680
  config.logger = Rails.logger # any Logger
1418
1681
  config.print_urls = true # log the URL of every request
1419
- config.apis_files_paths = ['config/apis/github.yml']
1682
+ config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
1420
1683
  end
1421
1684
  ```
1422
1685
 
@@ -1503,8 +1766,22 @@ github.users.show(username) GET https://api.github
1503
1766
  github.repos.issues(owner, repo).create POST https://api.github.com/repos/:owner/:repo/issues
1504
1767
  ```
1505
1768
 
1506
- A file that contains ERB is read only with `--erb`, because that runs the code
1507
- in it.
1769
+ With no file, in the root of a project, it lists the calls of every API of the
1770
+ project: it finds the files that describe an API, and the installed recipes, as
1771
+ `api_recipes upgrade` does.
1772
+
1773
+ ```
1774
+ $ bundle exec api_recipes routes
1775
+ shop.orders POST https://shop.example.com/orders
1776
+ billing.invoices GET {erb}://{erb}/api/v1/invoices
1777
+ billing.invoices.show(id) GET {erb}://{erb}/api/v1/invoices/:id
1778
+ config/apis/billing.yml.erb: what an ERB tag gives is shown as {erb}; --erb evaluates the file, which runs the code in it
1779
+ ```
1780
+
1781
+ A file that contains ERB is not run: what a `<%= ... %>` tag gives is shown as
1782
+ the variable `{erb}`. `--erb` evaluates the file instead, which runs the code
1783
+ in it. A file where a tag gives a name, or where a `<% ... %>` tag decides
1784
+ which lines exist, is listed only with `--erb`.
1508
1785
 
1509
1786
  ## Recipes
1510
1787
 
@@ -1514,7 +1791,7 @@ in it.
1514
1791
  # config/initializers/api_recipes.rb
1515
1792
  ApiRecipes.configure do |config|
1516
1793
  config.logger = Rails.logger
1517
- config.apis_files_paths = Dir[Rails.root.join('config', 'apis', '*.yml').to_s]
1794
+ config.recipes_paths = Dir[Rails.root.join('config', 'api_recipes', '*.recipe.yml').to_s]
1518
1795
  config.values = { github: { authorization: "Bearer #{Rails.application.credentials.github_token}" } }
1519
1796
  end
1520
1797
  ```
@@ -1524,16 +1801,35 @@ A value that depends on the user who is signed in does not go there: see
1524
1801
 
1525
1802
  ### One host per environment
1526
1803
 
1527
- Keep one file and let ERB pick the values:
1804
+ Keep one recipe, with the address as variables, and let each environment give
1805
+ its values:
1528
1806
 
1529
1807
  ```yaml
1530
1808
  reports:
1531
- protocol: <%= ENV.fetch('REPORTS_PROTOCOL', 'https') %>
1532
- host: <%= ENV.fetch('REPORTS_HOST', 'reports.example.com') %>
1809
+ recipe:
1810
+ name: reports
1811
+ version: 1.0.0
1812
+ format: 1
1813
+ variables:
1814
+ protocol:
1815
+ default: https
1816
+ host:
1817
+ default: reports.example.com
1818
+ protocol: '{protocol}'
1819
+ host: '{host}'
1533
1820
  endpoints:
1534
1821
  monthly:
1535
1822
  ```
1536
1823
 
1824
+ ```ruby
1825
+ ApiRecipes.configure do |config|
1826
+ config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
1827
+ config.values = {
1828
+ reports: { variables: { protocol: ENV['REPORTS_PROTOCOL'], host: ENV['REPORTS_HOST'] }.compact }
1829
+ }
1830
+ end
1831
+ ```
1832
+
1537
1833
  ### Every page of a list
1538
1834
 
1539
1835
  ```ruby
@@ -1677,8 +1973,9 @@ What ApiRecipes does:
1677
1973
  evaluated with ERB and is read as plain YAML, without anchors, aliases or
1678
1974
  tags. It cannot carry `verify_with`, cannot take the name of an API you
1679
1975
  already have, and cannot let a variable decide where its host ends. Its
1680
- text, comments included, holds only visible characters. It is loaded only if
1681
- it matches the checksum in `recipes.lock`.
1976
+ text, comments included, holds only visible characters. One that `install`
1977
+ took from a repository is loaded only if it matches the checksum in
1978
+ `recipes.lock`.
1682
1979
  - **`api_recipes install` and `update`** show where the requests and the
1683
1980
  credential go, the headers and the parameters sent with every request, those
1684
1981
  that an endpoint adds, and which calls are not GET, the ones that `resource`
@@ -1704,8 +2001,8 @@ What ApiRecipes does:
1704
2001
  `Marshal.dump` and `YAML.dump`, and its JSON is the one of `to_h`.
1705
2002
  - **`fill`** calls only public writers and skips keys that are not plain
1706
2003
  attribute names.
1707
- - **Configuration files** are read with `YAML.safe_load`, and the options are
1708
- validated when they are loaded.
2004
+ - **The configuration** is validated when it is loaded, whether it comes
2005
+ from a recipe or from `apis_configs`.
1709
2006
 
1710
2007
  What stays with your application:
1711
2008
 
@@ -1719,7 +2016,12 @@ What stays with your application:
1719
2016
  the variables of one user on it. Use `with`, or an object made for the request.
1720
2017
  - `response.original_response` is the raw http.rb response, and it gives access
1721
2018
  to the request that was sent, headers included. Do not log or serialize it.
1722
- - ERB in a configuration file runs code. Load only files you trust.
2019
+ - The gem evaluates no ERB. `api_recipes upgrade --erb` and
2020
+ `api_recipes routes --erb` do, on the files of a 2.x project that you name:
2021
+ that runs the code in them.
2022
+ - A recipe of the project, which the lock ties to no repository, is loaded as
2023
+ it is written: review a recipe that someone adds to the folder as you review
2024
+ code, because it decides where the requests and the credential go.
1723
2025
  - Read what `api_recipes install` shows before you answer yes. ApiRecipes checks
1724
2026
  the form of a recipe, not whose host it names. Commit the installed recipes
1725
2027
  and `recipes.lock`, and review their changes as you review code: the lock
@@ -1736,9 +2038,8 @@ Every protection has an example in [`spec/security`](spec/security); its
1736
2038
 
1737
2039
  | Option | Meaning |
1738
2040
  | --- | --- |
1739
- | `apis_files_paths` | Array of YAML files, evaluated with ERB |
2041
+ | `recipes_paths` | Array of portable recipes (`NAME.recipe.yml`), read as data only; one that `recipes.lock` ties to a repository is compared with it |
1740
2042
  | `apis_configs` | Hash of API descriptions |
1741
- | `recipes_paths` | Array of installed portable recipes (`NAME.recipe.yml`), read as data only and checked against `recipes.lock` |
1742
2043
  | `values` | for each API, its credential and the values of its variables for the whole application, as `{ api_name: { authorization: '...', variables: { name: value } } }`; `basic_auth` in place of `authorization` |
1743
2044
  | `logger` | a Logger to use |
1744
2045
  | `log_to` | path or IO for the default logger (default: STDERR) |
@@ -1750,11 +2051,11 @@ Every protection has an example in [`spec/security`](spec/security); its
1750
2051
 
1751
2052
  | Option | Default | Meaning |
1752
2053
  | --- | --- | --- |
1753
- | `protocol` | `https` | `http` or `https` |
2054
+ | `protocol` | `https` | `http` or `https`; can be a `{variable}` |
1754
2055
  | `host` | `localhost` | host name, which can contain `{variables}` |
1755
- | `port` | port of the protocol | port number |
2056
+ | `port` | port of the protocol | port number; can be a `{variable}` |
1756
2057
  | `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}` |
1757
- | `timeout` | `3` | seconds allowed for the whole request |
2058
+ | `timeout` | `3` | seconds allowed for the whole request; can be a `{variable}`, as `on_bad_code` and `mime_type` |
1758
2059
  | `default_headers` | none | headers sent with every request; a value can contain `{variables}` |
1759
2060
  | `default_params` | none | query parameters sent with every request; a value is text, a number or a boolean, and can contain `{variables}` |
1760
2061
  | `on_bad_code` | `raise` | `raise` or `ignore` when the response is not a success |
@@ -1769,15 +2070,15 @@ Every protection has an example in [`spec/security`](spec/security); its
1769
2070
  | Option | Default | Meaning |
1770
2071
  | --- | --- | --- |
1771
2072
  | `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 |
2073
+ | `verb` | `get` | HTTP verb; can be a `{variable}`, as `ok_code` (one status), `timeout`, `on_bad_code` and `mime_type` |
1773
2074
  | `route` | `yes` | `no` makes the endpoint a group that cannot be called |
1774
2075
  | `ok_code` | any 2xx | the status, or the list of statuses, accepted as a success |
1775
2076
  | `timeout` | looked for up the tree | seconds allowed for the whole request |
1776
2077
  | `on_bad_code` | looked for up the tree | `raise` or `ignore` |
1777
2078
  | `mime_type` | looked for up the tree | content type used by `response.data` |
1778
2079
  | `verify_with` | looked for up the tree | method that decides whether a response is a success |
1779
- | `default_headers` | none | headers added to those of the API, for this endpoint and the ones below it |
1780
- | `default_params` | none | query parameters added to those of the API, for this endpoint and the ones below it |
2080
+ | `default_headers` | none | headers of this endpoint; a request finds them here when it looks up the tree, before those of the API |
2081
+ | `default_params` | none | query parameters of this endpoint; a request finds them here when it looks up the tree, before those of the API |
1781
2082
  | `resource` | `no` | `yes` adds the endpoints `show`, `create`, `update`, `replace` and `delete`; a list adds only the ones it names |
1782
2083
  | `endpoints` | none | nested endpoints |
1783
2084
 
@@ -1852,8 +2153,8 @@ Run one from the root of the repository:
1852
2153
 
1853
2154
  $ bundle exec ruby examples/simple_usage.rb
1854
2155
 
1855
- [`examples/config/github.yml.erb`](examples/config/github.yml.erb) is a
1856
- configuration file with every option commented.
2156
+ [`examples/config/github.recipe.yml`](examples/config/github.recipe.yml) is a
2157
+ recipe with every option commented.
1857
2158
 
1858
2159
  ## Compatibility
1859
2160