Skip to content

Commit ae18bb0

Browse files
committed
Initial commit
This is a near 1-to-1 extraction of the code we have been using in production for over 5 years now.
0 parents  commit ae18bb0

18 files changed

Lines changed: 704 additions & 0 deletions

.github/workflows/main.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Ruby
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
name: Ruby ${{ matrix.ruby }}
14+
strategy:
15+
matrix:
16+
ruby:
17+
- '3.4.7'
18+
- '3.3.10'
19+
- '3.2.9'
20+
21+
steps:
22+
- uses: actions/checkout@v4
23+
- name: Set up Ruby
24+
uses: ruby/setup-ruby@v1
25+
with:
26+
ruby-version: ${{ matrix.ruby }}
27+
bundler-cache: true
28+
- name: Run the default task
29+
run: bundle exec rake

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
/.idea/
10+
/Gemfile.lock
11+
*.gem

.rubocop.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins:
2+
- rubocop-minitest
3+
- rubocop-rake
4+
5+
AllCops:
6+
TargetRubyVersion: 3.1
7+
NewCops: enable
8+
Exclude:
9+
- 'test/**/*'
10+
- 'vendor/**/*'
11+
12+
Style/StringLiterals:
13+
EnforcedStyle: double_quotes
14+
15+
Style/StringLiteralsInInterpolation:
16+
EnforcedStyle: double_quotes

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 0.0.1 - 2025-09-20
4+
5+
Initial version

Gemfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gemspec
6+
7+
gem "debug", ">= 1.0.0"
8+
gem "irb"
9+
gem "rake", "~> 13.0"
10+
11+
gem "minitest", "~> 5.16"
12+
13+
gem "rubocop", "~> 1.21"
14+
gem "rubocop-minitest"
15+
gem "rubocop-rake"

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2025 Beequip B.V.
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.

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# JsonPathAttribute
2+
3+
JsonPathAttribute is a simple but powerful object mapper to map JSON or Hash data into Ruby objects, using [JsonPath](https://github.com/joshbuddy/jsonpath).
4+
5+
Although this gem has been released fairly recently, it was originally developed for [Beequip](http://beequip.com) and a very similar version has been used for a few years.
6+
7+
## Usage
8+
9+
Given a JSON (or Ruby hash) like this:
10+
11+
```json
12+
{
13+
"post": {
14+
"title": "How to drive on snow?",
15+
"body": "Just use a low gear and slowly build up speed",
16+
"likes": 12,
17+
"comments": [
18+
{
19+
"body": "Thank you for the tip! It is very useful.",
20+
"user": {
21+
"name": "Charles Careful"
22+
}
23+
}
24+
]
25+
}
26+
}
27+
```
28+
29+
You can include `JsonPathAttribute` to a Ruby class:
30+
31+
```ruby
32+
class Post
33+
include JsonPathAttribute
34+
35+
json_path_attribute :title, path: 'post.title'
36+
json_path_attribute :likes, path: 'post.likes', type: :integer
37+
json_path_attribute :comments, path: 'post.comments[*]', type: Comment, array: true
38+
end
39+
40+
class Comment
41+
include JsonPathAttribute
42+
43+
json_path_attribute :body, path: 'body'
44+
json_path_attribute :name, path: 'user.name'
45+
end
46+
```
47+
48+
And it gets parsed like this
49+
50+
```ruby
51+
post = Post.parse(json) # => <Post:...>
52+
post.title # => "How to drive on snow?"
53+
post.body # => "Just use a low gear and slowly build up speed"
54+
post.likes # => 12
55+
post.comments # => [#<Comment:0x000000011e4354d0 @body="Thank you for the tip! It is very useful.", @name="Charles Careful">]
56+
```
57+
58+
## Installation
59+
60+
Install the gem and add to the application's Gemfile by executing:
61+
62+
```bash
63+
bundle add json_path_attribute
64+
```
65+
66+
If bundler is not being used to manage dependencies, install the gem by executing:
67+
68+
```bash
69+
gem install json_path_attribute
70+
```
71+
72+
## Development
73+
74+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75+
76+
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).
77+
78+
## Contributing
79+
80+
Bug reports and pull requests are welcome on GitHub at https://github.com/BEEQUIP/json_path_attribute.
81+
82+
## Acknowledgements
83+
84+
Thanks to the original authors [jdongelmans](https://github.com/jdongelmans) and [jandintel](https://github.com/jandintel).
85+
86+
Also thanks to [joshbuddy](https://github.com/joshbuddy) for creating [JsonPath](https://github.com/joshbuddy/jsonpath).

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
require "minitest/test_task"
5+
6+
Minitest::TestTask.create
7+
8+
require "rubocop/rake_task"
9+
10+
RuboCop::RakeTask.new
11+
12+
task default: %i[test rubocop]

bin/console

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "path_attribute"
6+
7+
require "irb"
8+
IRB.start(__FILE__)

bin/setup

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install

0 commit comments

Comments
 (0)