halbuilder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +11 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +127 -0
- data/LICENSE.txt +21 -0
- data/README.md +252 -0
- data/Rakefile +10 -0
- data/halbuilder.gemspec +32 -0
- data/lib/halbuilder/configuration.rb +18 -0
- data/lib/halbuilder/embed.rb +40 -0
- data/lib/halbuilder/key_format.rb +46 -0
- data/lib/halbuilder/link.rb +19 -0
- data/lib/halbuilder/paginate.rb +44 -0
- data/lib/halbuilder/version.rb +5 -0
- data/lib/halbuilder/zoom.rb +30 -0
- data/lib/halbuilder.rb +39 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8aef0833d5b98f1f2be71e2734b74f2ac39e9a403bdaacd698759fc99a59ba68
|
4
|
+
data.tar.gz: d49275d798a0a73ffb68ca951ec045490e5b0d491f9e3ef86ac4c3aceb5f30cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c470bcb51e9e4c1fea988533afdf86273dc8f1ddab377809e0e402ebb385289f755887098f3adae717f472dd039d937b309afcbb3dfc4f3b1eb9866cbe1e2a02
|
7
|
+
data.tar.gz: 7ff41ec9985d74868b82c3a7b027d75856df927ea94ea449d3aad0477238d2746fc021f98a0e8cb88ccc381bbb97d51ca116e49c63c1c9594d41661e32d5193c
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
## [Unreleased]
|
2
|
+
|
3
|
+
## [0.1.0] - 2022-07-20
|
4
|
+
|
5
|
+
Initial release, including:
|
6
|
+
|
7
|
+
- Key formatting options
|
8
|
+
- `json.hal_link!` href/block support
|
9
|
+
- `json.hal_embed!` lambda/block support
|
10
|
+
- `json.hal_paginate!` total/count/links
|
11
|
+
- `?zoom=` param parsing for embedded objects/collections
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ryancavis@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
halbuilder (0.1.0)
|
5
|
+
jbuilder (>= 2.0.0)
|
6
|
+
kaminari (>= 1.0.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionview (6.1.6.1)
|
12
|
+
activesupport (= 6.1.6.1)
|
13
|
+
builder (~> 3.1)
|
14
|
+
erubi (~> 1.4)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
17
|
+
activemodel (6.1.6.1)
|
18
|
+
activesupport (= 6.1.6.1)
|
19
|
+
activerecord (6.1.6.1)
|
20
|
+
activemodel (= 6.1.6.1)
|
21
|
+
activesupport (= 6.1.6.1)
|
22
|
+
activesupport (6.1.6.1)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 1.6, < 2)
|
25
|
+
minitest (>= 5.1)
|
26
|
+
tzinfo (~> 2.0)
|
27
|
+
zeitwerk (~> 2.3)
|
28
|
+
ast (2.4.2)
|
29
|
+
builder (3.2.4)
|
30
|
+
coderay (1.1.3)
|
31
|
+
concurrent-ruby (1.1.10)
|
32
|
+
crass (1.0.6)
|
33
|
+
diff-lcs (1.5.0)
|
34
|
+
erubi (1.10.0)
|
35
|
+
i18n (1.12.0)
|
36
|
+
concurrent-ruby (~> 1.0)
|
37
|
+
jbuilder (2.11.5)
|
38
|
+
actionview (>= 5.0.0)
|
39
|
+
activesupport (>= 5.0.0)
|
40
|
+
json (2.6.2)
|
41
|
+
kaminari (1.2.2)
|
42
|
+
activesupport (>= 4.1.0)
|
43
|
+
kaminari-actionview (= 1.2.2)
|
44
|
+
kaminari-activerecord (= 1.2.2)
|
45
|
+
kaminari-core (= 1.2.2)
|
46
|
+
kaminari-actionview (1.2.2)
|
47
|
+
actionview
|
48
|
+
kaminari-core (= 1.2.2)
|
49
|
+
kaminari-activerecord (1.2.2)
|
50
|
+
activerecord
|
51
|
+
kaminari-core (= 1.2.2)
|
52
|
+
kaminari-core (1.2.2)
|
53
|
+
loofah (2.18.0)
|
54
|
+
crass (~> 1.0.2)
|
55
|
+
nokogiri (>= 1.5.9)
|
56
|
+
method_source (1.0.0)
|
57
|
+
minitest (5.16.2)
|
58
|
+
nokogiri (1.13.7-arm64-darwin)
|
59
|
+
racc (~> 1.4)
|
60
|
+
nokogiri (1.13.7-x86_64-linux)
|
61
|
+
racc (~> 1.4)
|
62
|
+
parallel (1.22.1)
|
63
|
+
parser (3.1.2.0)
|
64
|
+
ast (~> 2.4.1)
|
65
|
+
pry (0.13.1)
|
66
|
+
coderay (~> 1.1)
|
67
|
+
method_source (~> 1.0)
|
68
|
+
racc (1.6.0)
|
69
|
+
rails-dom-testing (2.0.3)
|
70
|
+
activesupport (>= 4.2.0)
|
71
|
+
nokogiri (>= 1.6)
|
72
|
+
rails-html-sanitizer (1.4.3)
|
73
|
+
loofah (~> 2.3)
|
74
|
+
rainbow (3.1.1)
|
75
|
+
rake (13.0.6)
|
76
|
+
regexp_parser (2.5.0)
|
77
|
+
rexml (3.2.5)
|
78
|
+
rspec (3.11.0)
|
79
|
+
rspec-core (~> 3.11.0)
|
80
|
+
rspec-expectations (~> 3.11.0)
|
81
|
+
rspec-mocks (~> 3.11.0)
|
82
|
+
rspec-core (3.11.0)
|
83
|
+
rspec-support (~> 3.11.0)
|
84
|
+
rspec-expectations (3.11.0)
|
85
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
86
|
+
rspec-support (~> 3.11.0)
|
87
|
+
rspec-mocks (3.11.1)
|
88
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
89
|
+
rspec-support (~> 3.11.0)
|
90
|
+
rspec-support (3.11.0)
|
91
|
+
rubocop (1.31.2)
|
92
|
+
json (~> 2.3)
|
93
|
+
parallel (~> 1.10)
|
94
|
+
parser (>= 3.1.0.0)
|
95
|
+
rainbow (>= 2.2.2, < 4.0)
|
96
|
+
regexp_parser (>= 1.8, < 3.0)
|
97
|
+
rexml (>= 3.2.5, < 4.0)
|
98
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
99
|
+
ruby-progressbar (~> 1.7)
|
100
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
101
|
+
rubocop-ast (1.19.1)
|
102
|
+
parser (>= 3.1.1.0)
|
103
|
+
rubocop-performance (1.14.3)
|
104
|
+
rubocop (>= 1.7.0, < 2.0)
|
105
|
+
rubocop-ast (>= 0.4.0)
|
106
|
+
ruby-progressbar (1.11.0)
|
107
|
+
standard (1.13.0)
|
108
|
+
rubocop (= 1.31.2)
|
109
|
+
rubocop-performance (= 1.14.3)
|
110
|
+
tzinfo (2.0.5)
|
111
|
+
concurrent-ruby (~> 1.0)
|
112
|
+
unicode-display_width (2.2.0)
|
113
|
+
zeitwerk (2.6.0)
|
114
|
+
|
115
|
+
PLATFORMS
|
116
|
+
arm64-darwin-21
|
117
|
+
x86_64-linux
|
118
|
+
|
119
|
+
DEPENDENCIES
|
120
|
+
halbuilder!
|
121
|
+
pry (~> 0.13.1)
|
122
|
+
rake (~> 13.0)
|
123
|
+
rspec (~> 3.0)
|
124
|
+
standard (~> 1.3)
|
125
|
+
|
126
|
+
BUNDLED WITH
|
127
|
+
2.3.18
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 PRX
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,252 @@
|
|
1
|
+
# Halbuilder
|
2
|
+
|
3
|
+
[![license](https://img.shields.io/github/license/PRX/halbuilder.svg)](LICENSE)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/halbuilder)](https://rubygems.org/gems/halbuilder)
|
5
|
+
[![Gem](https://img.shields.io/gem/dt/halbuilder)](https://rubygems.org/gems/halbuilder)
|
6
|
+
![RSpec](https://github.com/PRX/halbuilder/workflows/RSpec/badge.svg)
|
7
|
+
![Standard](https://github.com/PRX/halbuilder/workflows/Standard/badge.svg)
|
8
|
+
|
9
|
+
[Hypertext Application Language](https://en.wikipedia.org/wiki/Hypertext_Application_Language) (HAL)
|
10
|
+
specific extensions for [Jbuilder](https://github.com/rails/jbuilder).
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Install the gem and add to the application's Gemfile by executing:
|
15
|
+
|
16
|
+
$ bundle add halbuilder
|
17
|
+
|
18
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
19
|
+
|
20
|
+
$ gem install halbuilder
|
21
|
+
|
22
|
+
## Configuration
|
23
|
+
|
24
|
+
To customize Halbuilder, create an initializer in your Rails app at `config/initializers/halbuilder.rb`.
|
25
|
+
Or if not using Rails, you can really configure this gem from anywhere:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# config/initializers/halbuilder.rb
|
29
|
+
|
30
|
+
Halbuilder.configure do |config|
|
31
|
+
config.key_format = :camelize_lower
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Available configurations are:
|
36
|
+
|
37
|
+
- `key_format` (default `:camelize_lower`) - the key format used for [Jbuilder formatting keys](https://github.com/rails/jbuilder#formatting-keys)
|
38
|
+
- This gem always formats nested keys
|
39
|
+
- Allowed values are: `nil` `:underscore` `:dasherize` `:camelize_lower` `:camelize_upper`
|
40
|
+
- `link_namespace` (default `nil`) - allow namespacing links in a way that breaks `key_format`
|
41
|
+
- Can be any alphanumeric string
|
42
|
+
- E.g. `"hello"` would cause `json.hello_world "val"` to generate `{"hello:world":"val"}`.
|
43
|
+
- `link_format` (default `:dasherize`) - format namespaced links differently from `key_format`
|
44
|
+
- Only applies if you set a non-nil `link_namespace`
|
45
|
+
- Same allowed values as `key_format`
|
46
|
+
- E.g. `:dasherize` would cause `json.hello_world_there "val"` to generate `{"hello:world-there":"val"}`
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
This library strives to be low-to-moderately opinionated about how you format your
|
51
|
+
Jbuilder views. And there will always be many ways to build the same output. But - here are some general patterns for creating HAL JSON APIs.
|
52
|
+
|
53
|
+
### Links
|
54
|
+
|
55
|
+
The HAL `_links` object can be generated from anywhere within your `*.json.jbuilder` template,
|
56
|
+
and given either a plain string href or yield a block object:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# just a string href
|
60
|
+
json.hal_link! "hello:one", "/api/v1/hello-one"
|
61
|
+
|
62
|
+
# or a block
|
63
|
+
json.hal_link! 'second' do
|
64
|
+
json.href "/api/v1/second"
|
65
|
+
json.title "my title"
|
66
|
+
end
|
67
|
+
|
68
|
+
# or manually add a link
|
69
|
+
json._links do
|
70
|
+
json.third_link do
|
71
|
+
json.href "/api/v1/thr33"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
```json
|
77
|
+
{
|
78
|
+
"_links": {
|
79
|
+
"hello:one": {
|
80
|
+
"href": "/api/v1/hello-one"
|
81
|
+
},
|
82
|
+
"second": {
|
83
|
+
"href": "/api/v1/second",
|
84
|
+
"title": "my title"
|
85
|
+
},
|
86
|
+
"thirdLink": {
|
87
|
+
"href": "/api/v1/thr33"
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
```
|
92
|
+
|
93
|
+
### Embeds
|
94
|
+
|
95
|
+
One convention for speeding up HAL APIs is to embed linked resources into the same documents.
|
96
|
+
For this, we use the `_embedded` top level key, followed by the same `rel` as the link, and
|
97
|
+
the object or array of objects (or null) you would get back had you followed the link.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
json.hal_embed! "hello:one" do
|
101
|
+
json.id 1234
|
102
|
+
json.title "Some Resource"
|
103
|
+
end
|
104
|
+
|
105
|
+
json.hal_embed! "hello:two", 1..3 do |num|
|
106
|
+
json.id num
|
107
|
+
json.title "Thing #{num}"
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
```json
|
112
|
+
{
|
113
|
+
"_embedded": {
|
114
|
+
"hello:one": {
|
115
|
+
"id": 1234,
|
116
|
+
"title": "Some Resource"
|
117
|
+
},
|
118
|
+
"hello:two": [
|
119
|
+
{
|
120
|
+
"id": 1,
|
121
|
+
"title": "Thing 1"
|
122
|
+
},
|
123
|
+
{
|
124
|
+
"id": 2,
|
125
|
+
"title": "Thing 2"
|
126
|
+
},
|
127
|
+
{
|
128
|
+
"id": 3,
|
129
|
+
"title": "Thing 3"
|
130
|
+
}
|
131
|
+
]
|
132
|
+
}
|
133
|
+
}
|
134
|
+
```
|
135
|
+
|
136
|
+
Note that after the name of the `rel`, you can optionally pass a collection to iterate
|
137
|
+
over. You can also pass a lambda, just-in-time returning either an object or collection
|
138
|
+
to render:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
json.hal_embed! "hello:one", -> { {id: 1234, title: "Some Resource"} } do |obj|
|
142
|
+
json.id obj[:id]
|
143
|
+
json.title obj[:title]
|
144
|
+
end
|
145
|
+
|
146
|
+
json.hal_embed! "hello:two", -> { 1..3 } do |num|
|
147
|
+
json.id num
|
148
|
+
json.title "Thing #{num}"
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
This works in conjunction with the `?zoom=` query parameter. By passing `zoom: true` to
|
153
|
+
a `hal_embed!` we indicate that by default, we want to render this collection. Or passing
|
154
|
+
`zoom: false` indicates not to render it.
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
# GET /api/v1/resource
|
158
|
+
|
159
|
+
json.hal_embed! "hello:one", zoom: false do
|
160
|
+
title "This will not render"
|
161
|
+
end
|
162
|
+
|
163
|
+
json.hal_embed! "hello:two", zoom: true do
|
164
|
+
title "This WILL render"
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
Passing `?zoom=1` or `?zoom=0` will result in _ALL_ `hal_embed!`s in the view being
|
169
|
+
rendered or not rendered, regardless of their defaults.
|
170
|
+
|
171
|
+
Passing `?zoom=hello:one,hello:two` will result in just those 2 rels being rendered,
|
172
|
+
but any other `hal_embed!` with a `zoom:` will not be. This can be used in conjunction
|
173
|
+
with lambdas/blocks to only load data needed for the requested zoom level:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
# GET /api/v1/resource?zoom=hello:one,hello:two
|
177
|
+
|
178
|
+
json.hal_embed! "hello:one", zoom: false do
|
179
|
+
# this code will run and render
|
180
|
+
title some_expensive_db.get_title
|
181
|
+
end
|
182
|
+
|
183
|
+
json.hal_embed! "hello:two", -> { some_expensive_db.fetch_two }, zoom: true do |thing|
|
184
|
+
# this code will also run and render
|
185
|
+
title thing.title
|
186
|
+
end
|
187
|
+
|
188
|
+
json.hal_embed! "hello:three", -> { some_expensive_db.fetch_three }, zoom: true do |thing|
|
189
|
+
# this code will NOT run the expensive db operation, OR render
|
190
|
+
title thing.title
|
191
|
+
end
|
192
|
+
|
193
|
+
json.hal_embed! "hello:four", zoom: true do
|
194
|
+
# this code will not run or render
|
195
|
+
title some_expensive_db.get_title
|
196
|
+
end
|
197
|
+
```
|
198
|
+
|
199
|
+
### Pagination
|
200
|
+
|
201
|
+
It's often useful to parse query params and render `_links` to different collection pages,
|
202
|
+
using the gem [Kaminari](https://github.com/kaminari/kaminari). After paging the collection in
|
203
|
+
your controller, just call `hal_paginate!` on it:
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
# GET /api/v1/things?foo=bar&page=3
|
207
|
+
|
208
|
+
json.hal_paginate! @things
|
209
|
+
```
|
210
|
+
|
211
|
+
```json
|
212
|
+
{
|
213
|
+
"count": 10,
|
214
|
+
"total": 999,
|
215
|
+
"_links": {
|
216
|
+
"first": {
|
217
|
+
"href": "/api/v1/accounts?foo=bar"
|
218
|
+
},
|
219
|
+
"prev": {
|
220
|
+
"href": "/api/v1/accounts?foo=bar&page=2"
|
221
|
+
},
|
222
|
+
"next": {
|
223
|
+
"href": "/api/v1/accounts?foo=bar&page=4"
|
224
|
+
},
|
225
|
+
"last": {
|
226
|
+
"href": "/api/v1/accounts?foo=bar&page=100"
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
```
|
231
|
+
|
232
|
+
## Development
|
233
|
+
|
234
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
235
|
+
|
236
|
+
Before commiting any code, run `rake standardrb` and/or `rake standardrb --fix` to make sure your changes are compliant with the [Standard](https://github.com/testdouble/standard) style guide.
|
237
|
+
|
238
|
+
When you're ready to push changes, open a pull request against the `main branch` of the repo.
|
239
|
+
|
240
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
241
|
+
|
242
|
+
## Contributing
|
243
|
+
|
244
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/PRX/halbuilder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/PRX/halbuilder/blob/master/CODE_OF_CONDUCT.md).
|
245
|
+
|
246
|
+
## License
|
247
|
+
|
248
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
249
|
+
|
250
|
+
## Code of Conduct
|
251
|
+
|
252
|
+
Everyone interacting in the Halbuilder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/PRX/halbuilder/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/halbuilder.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/halbuilder/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "halbuilder"
|
7
|
+
spec.version = Halbuilder::VERSION
|
8
|
+
spec.authors = ["cavis"]
|
9
|
+
spec.email = ["ryan@prx.org"]
|
10
|
+
|
11
|
+
spec.summary = "HAL DSL extensions for Jbuilder."
|
12
|
+
spec.homepage = "https://github.com/PRX/halbuilder"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.6.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/PRX/halbuilder"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/PRX/halbuilder/blob/main/CHANGELOG.md"
|
19
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/PRX/halbuilder/issues"
|
20
|
+
|
21
|
+
spec.files = Dir.chdir(__dir__) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "jbuilder", ">= 2.0.0"
|
31
|
+
spec.add_dependency "kaminari", ">= 1.0.0"
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "jbuilder"
|
4
|
+
|
5
|
+
module Halbuilder
|
6
|
+
class Configuration
|
7
|
+
attr_reader :link_key, :embed_key
|
8
|
+
attr_accessor :key_format, :link_namespace, :link_format
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@link_key = "_links"
|
12
|
+
@embed_key = "_embedded"
|
13
|
+
@key_format = :camelize_lower
|
14
|
+
@link_namespace = nil
|
15
|
+
@link_format = :dasherize
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Halbuilder::Embed
|
4
|
+
def hal_embed!(rel, *args, &block)
|
5
|
+
collection = :not_provided
|
6
|
+
opts = {}
|
7
|
+
|
8
|
+
# first arg may optionally be an iterable collection
|
9
|
+
if args.count > 1
|
10
|
+
collection = args[0]
|
11
|
+
opts = args[1]
|
12
|
+
elsif args[0].is_a?(Hash)
|
13
|
+
# NOTE: we don't allow passing a _non_ options hash as the only argument
|
14
|
+
opts = args[0]
|
15
|
+
elsif args.length > 0
|
16
|
+
collection = args[0]
|
17
|
+
end
|
18
|
+
|
19
|
+
# check if we're zooming before rendering anything
|
20
|
+
if hal_zoomed?(rel, opts[:zoom])
|
21
|
+
set! Halbuilder.configuration.embed_key do
|
22
|
+
set! rel do
|
23
|
+
# only load collection from proc after deciding to zoom
|
24
|
+
collection = collection.call if collection.is_a?(Proc)
|
25
|
+
|
26
|
+
# iterate block, or render explicit nulls
|
27
|
+
if collection.respond_to?(:each) && !collection.is_a?(Hash)
|
28
|
+
array! collection do |c|
|
29
|
+
block.call(c)
|
30
|
+
end
|
31
|
+
elsif collection.present? || collection == :not_provided
|
32
|
+
block.call(collection)
|
33
|
+
else
|
34
|
+
null!
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Halbuilder::KeyFormat
|
4
|
+
class << self
|
5
|
+
def setup
|
6
|
+
::Jbuilder.deep_format_keys(true)
|
7
|
+
|
8
|
+
::Jbuilder.key_format ->(key) do
|
9
|
+
key_format = Halbuilder.configuration.key_format
|
10
|
+
link_format = Halbuilder.configuration.link_format
|
11
|
+
link_key = Halbuilder.configuration.link_key
|
12
|
+
embed_key = Halbuilder.configuration.embed_key
|
13
|
+
|
14
|
+
ns = Halbuilder.configuration.link_namespace
|
15
|
+
ns_start = "#{ns}_" if ns.present?
|
16
|
+
ns_length = ns_start.length if ns.present?
|
17
|
+
|
18
|
+
if key == link_key || key == embed_key
|
19
|
+
key
|
20
|
+
elsif ns_start && key.start_with?(ns_start)
|
21
|
+
format_key("#{ns}:#{key[ns_length..]}", link_format)
|
22
|
+
else
|
23
|
+
format_key(key, key_format)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def format_key(key, format)
|
31
|
+
if format.nil?
|
32
|
+
key
|
33
|
+
elsif format == :underscore
|
34
|
+
key.underscore
|
35
|
+
elsif format == :dasherize
|
36
|
+
key.dasherize
|
37
|
+
elsif format == :camelize_lower
|
38
|
+
key.camelize(:lower)
|
39
|
+
elsif format == :camelize_upper
|
40
|
+
key.camelize(:upper)
|
41
|
+
else
|
42
|
+
raise Halbuilder::Error.new("Unknown key format: #{format}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Halbuilder::Link
|
4
|
+
def hal_link!(rel, val = nil, &block)
|
5
|
+
set! Halbuilder.configuration.link_key do
|
6
|
+
if block.present?
|
7
|
+
set! rel do
|
8
|
+
block.call
|
9
|
+
end
|
10
|
+
elsif val.is_a?(String)
|
11
|
+
set! rel do
|
12
|
+
href val
|
13
|
+
end
|
14
|
+
else
|
15
|
+
raise Halbuilder::Error.new("Invalid hal link: #{href.inspect}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Halbuilder::Paginate
|
4
|
+
def hal_paginate!(collection)
|
5
|
+
return unless paginated?(collection)
|
6
|
+
|
7
|
+
set!("count", collection.size)
|
8
|
+
set!("total", collection.total_count)
|
9
|
+
_links do
|
10
|
+
hal_paginate_links!(collection)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def hal_paginate_links!(rel)
|
15
|
+
set!("first", page_href(1)) unless rel.first_page?
|
16
|
+
set!("prev", page_href(rel.prev_page)) unless rel.first_page?
|
17
|
+
set!("next", page_href(rel.next_page)) unless rel.last_page?
|
18
|
+
set!("last", page_href(rel.total_pages)) unless rel.last_page?
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def paginated?(rel)
|
24
|
+
if rel
|
25
|
+
rel.respond_to?(:current_page) && rel.respond_to?(:total_pages)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def page_href(num)
|
30
|
+
query =
|
31
|
+
if num == 1
|
32
|
+
@context.request.query_parameters.except("page")
|
33
|
+
else
|
34
|
+
@context.request.query_parameters.tap { |q| q["page"] = num }
|
35
|
+
end
|
36
|
+
|
37
|
+
query_str = ::URI.encode_www_form(query)
|
38
|
+
if query_str.present?
|
39
|
+
{href: "#{@context.request.path}?#{query_str}"}
|
40
|
+
else
|
41
|
+
{href: @context.request.path}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Halbuilder::Zoom
|
4
|
+
TRUTHY = [true, "true", "TRUE", 1, "1"]
|
5
|
+
FALSEY = [false, "false", "FALSE", 0, "0"]
|
6
|
+
|
7
|
+
def hal_zoomed?(rel, default_setting)
|
8
|
+
if default_setting.nil?
|
9
|
+
true
|
10
|
+
elsif zoom_param.nil?
|
11
|
+
default_setting
|
12
|
+
elsif TRUTHY.include?(zoom_param)
|
13
|
+
true
|
14
|
+
elsif FALSEY.include?(zoom_param)
|
15
|
+
false
|
16
|
+
else
|
17
|
+
zoom_rels.include?(rel)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def zoom_param
|
24
|
+
@context.params[:zoom]
|
25
|
+
end
|
26
|
+
|
27
|
+
def zoom_rels
|
28
|
+
zoom_param.is_a?(Array) ? zoom_param : zoom_param.split(",")
|
29
|
+
end
|
30
|
+
end
|
data/lib/halbuilder.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "jbuilder"
|
4
|
+
require_relative "halbuilder/configuration"
|
5
|
+
require_relative "halbuilder/embed"
|
6
|
+
require_relative "halbuilder/key_format"
|
7
|
+
require_relative "halbuilder/link"
|
8
|
+
require_relative "halbuilder/paginate"
|
9
|
+
require_relative "halbuilder/version"
|
10
|
+
require_relative "halbuilder/zoom"
|
11
|
+
|
12
|
+
module Halbuilder
|
13
|
+
class << self
|
14
|
+
def reset!
|
15
|
+
@configuration = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def configure
|
23
|
+
yield configuration
|
24
|
+
setup
|
25
|
+
end
|
26
|
+
|
27
|
+
def setup
|
28
|
+
Halbuilder::KeyFormat.setup
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Error < StandardError; end
|
33
|
+
end
|
34
|
+
|
35
|
+
Halbuilder::KeyFormat.setup
|
36
|
+
Jbuilder.include Halbuilder::Embed
|
37
|
+
Jbuilder.include Halbuilder::Link
|
38
|
+
Jbuilder.include Halbuilder::Paginate
|
39
|
+
Jbuilder.include Halbuilder::Zoom
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: halbuilder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cavis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jbuilder
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: kaminari
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- ryan@prx.org
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".ruby-version"
|
50
|
+
- ".standard.yml"
|
51
|
+
- CHANGELOG.md
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- Gemfile
|
54
|
+
- Gemfile.lock
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- halbuilder.gemspec
|
59
|
+
- lib/halbuilder.rb
|
60
|
+
- lib/halbuilder/configuration.rb
|
61
|
+
- lib/halbuilder/embed.rb
|
62
|
+
- lib/halbuilder/key_format.rb
|
63
|
+
- lib/halbuilder/link.rb
|
64
|
+
- lib/halbuilder/paginate.rb
|
65
|
+
- lib/halbuilder/version.rb
|
66
|
+
- lib/halbuilder/zoom.rb
|
67
|
+
homepage: https://github.com/PRX/halbuilder
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata:
|
71
|
+
homepage_uri: https://github.com/PRX/halbuilder
|
72
|
+
source_code_uri: https://github.com/PRX/halbuilder
|
73
|
+
changelog_uri: https://github.com/PRX/halbuilder/blob/main/CHANGELOG.md
|
74
|
+
bug_tracker_uri: https://github.com/PRX/halbuilder/issues
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 2.6.0
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubygems_version: 3.3.7
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: HAL DSL extensions for Jbuilder.
|
94
|
+
test_files: []
|