halva 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 900e5a47a7c2ffcb2528799f258b4de9245358315dfb0c7cf5c211844bfe872b
4
+ data.tar.gz: 798b4bb53e9121a805f8e0acb963609c8626b2281e80c54098e76b037c6574b9
5
+ SHA512:
6
+ metadata.gz: 447cd2a234cc0edc69b2404207731add3093e91527ffe2ce9ab34416e78fd7415f344d03cebfff9289411cc6af8f502d55bdef7dc95dd20773469f719ac0ecde
7
+ data.tar.gz: 6dde2bcef96c82921f98af028a0c6c78c47453e904f6dc9ad891cacaf001ab03100e8a43a28820c8deef92a00caa84872688526661899665005d873c6ff5f05f
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.6
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in halva.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
10
+
11
+ group :development do
12
+ gem 'rubocop', '~> 1.45', require: false
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ halva (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ parallel (1.22.1)
13
+ parser (3.2.1.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (12.3.3)
17
+ regexp_parser (2.7.0)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.45.1)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.2.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.24.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.26.0)
43
+ parser (>= 3.2.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.4.2)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ halva!
52
+ rake (~> 12.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 1.45)
55
+
56
+ BUNDLED WITH
57
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Denis Semenenko
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Halva
2
+
3
+ HAL-compliant serializer
4
+
5
+ [![Ruby](https://github.com/denblackstache/halva/actions/workflows/main.yml/badge.svg)](https://github.com/denblackstache/halva/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/halva.svg)](https://badge.fury.io/rb/halva)
6
+
7
+ Links
8
+ * [HAL - Hypertext Application Language](https://stateless.co/hal_specification.html)
9
+ * [Specification Draft](https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-08)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'halva'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install halva
26
+
27
+ ## Usage
28
+
29
+ ### Representing an object
30
+
31
+ ```ruby
32
+ order = Order.find(1)
33
+ Halva::Resource.from_model(order)
34
+ .embed(Halva::Resource.from_model(order.customer).build, :customer)
35
+ .link(Halva::Link.new('/orders/1', :self))
36
+ .link(Halva::Link.new('/orders/1/customer', :customer))
37
+ .build
38
+
39
+ ```
40
+
41
+ ### Representing a collection
42
+
43
+ ```ruby
44
+ orders = Order.find
45
+ Halva::Resource.from_empty_model
46
+ .embed(orders.map { |order| Halva::Resource.from_model(order).build })
47
+ .link(Halva::Link.new('/orders?page=3', :next))
48
+ .link(Halva::Link.new('/orders?page=2', :self))
49
+ .link(Halva::Link.new('/orders?page=1', :prev))
50
+ .build
51
+
52
+ ```
53
+
54
+ ## Development
55
+
56
+ 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.
57
+
58
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/denblackstache/halva.
63
+
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'halva'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/halva.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/halva/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'halva'
7
+ spec.version = Halva::VERSION
8
+ spec.authors = ['Denis Semenenko']
9
+ spec.email = ['hypercoderx@gmail.com']
10
+
11
+ spec.summary = 'HAL-compliant serializer'
12
+ spec.homepage = 'https://github.com/denblackstache/halva'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
15
+
16
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/denblackstache/halva'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
data/lib/halva/link.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Halva
4
+ # HAL Link Object
5
+ # https://datatracker.ietf.org/doc/html/draft-kelly-json-hal#section-5}
6
+ class Link
7
+ attr_reader :relation, :href
8
+
9
+ def initialize(href, relation, options = {})
10
+ @href = href
11
+ @relation = relation.to_sym
12
+ @options = options
13
+ end
14
+
15
+ def to_h
16
+ { href: @href, **@options }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Halva
4
+ # HAL Resource object
5
+ # https://datatracker.ietf.org/doc/html/draft-kelly-json-hal#section-4
6
+ class Resource
7
+ EMBEDDED_KEY = :_embedded
8
+ LINKS_KEY = :_links
9
+
10
+ def self.from_model(model)
11
+ new(model.to_h)
12
+ end
13
+
14
+ def self.from_empty_model
15
+ new({})
16
+ end
17
+
18
+ def initialize(model)
19
+ @model = model
20
+ @embedded = []
21
+ @links = []
22
+ end
23
+
24
+ def embed(resource, relation = :item)
25
+ @embedded << { resource: resource, relation: relation.to_sym }
26
+ self
27
+ end
28
+
29
+ def link(link)
30
+ @links << link
31
+ self
32
+ end
33
+
34
+ def build
35
+ unless @embedded.empty?
36
+ @model[EMBEDDED_KEY] = {} unless @model.key?(EMBEDDED_KEY)
37
+ @embedded.each do |emb|
38
+ @model[EMBEDDED_KEY][emb[:relation]] = emb[:resource]
39
+ end
40
+ end
41
+
42
+ unless @links.empty?
43
+ @model[LINKS_KEY] = {} unless @model.key?(LINKS_KEY)
44
+ @links.each do |link|
45
+ @model[LINKS_KEY][link.relation] = link.to_h
46
+ end
47
+ end
48
+
49
+ @model.to_h
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Halva
4
+ VERSION = '0.1.0'
5
+ end
data/lib/halva.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'halva/version'
4
+
5
+ module Halva
6
+ class Error < StandardError; end
7
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: halva
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Semenenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - hypercoderx@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".ruby-version"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - halva.gemspec
32
+ - lib/halva.rb
33
+ - lib/halva/link.rb
34
+ - lib/halva/resource.rb
35
+ - lib/halva/version.rb
36
+ homepage: https://github.com/denblackstache/halva
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ allowed_push_host: https://rubygems.org
41
+ homepage_uri: https://github.com/denblackstache/halva
42
+ source_code_uri: https://github.com/denblackstache/halva
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.7.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.1.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: HAL-compliant serializer
62
+ test_files: []