From 0b07b09dc1a1c0e7a411a6de0e1f31139518f460 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Wed, 30 Oct 2024 16:03:01 +0800 Subject: [PATCH] feat: initial commit --- .github/workflows/rake.yml | 12 ++ .github/workflows/release.yml | 22 ++++ .gitignore | 11 ++ .rspec | 3 + .rubocop.yml | 34 +++++ .rubocop_todo.yml | 51 ++++++++ Gemfile | 13 ++ README.adoc | 171 +++++++++++++++++++++++++ Rakefile | 12 ++ ali.gemspec | 35 +++++ bin/console | 11 ++ bin/setup | 8 ++ lib/ali.rb | 14 ++ lib/ali/container.rb | 24 ++++ lib/ali/free_to_read.rb | 21 +++ lib/ali/license_ref.rb | 24 ++++ lib/ali/version.rb | 5 + sig/ali.rbs | 4 + spec/ali/container_spec.rb | 97 ++++++++++++++ spec/ali/free_to_read_spec.rb | 129 +++++++++++++++++++ spec/ali/license_ref_spec.rb | 149 +++++++++++++++++++++ spec/ali_spec.rb | 27 ++++ spec/fixtures/json/spec_example_1.json | 4 + spec/fixtures/json/spec_example_2.json | 7 + spec/fixtures/json/spec_example_3.json | 5 + spec/fixtures/json/spec_example_4.json | 8 ++ spec/fixtures/json/spec_example_5.json | 10 ++ spec/fixtures/json/spec_example_6.json | 12 ++ spec/fixtures/xml/spec_example_1.xml | 5 + spec/fixtures/xml/spec_example_2.xml | 7 + spec/spec_helper.rb | 28 ++++ 31 files changed, 963 insertions(+) create mode 100644 .github/workflows/rake.yml create mode 100644 .github/workflows/release.yml create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml create mode 100644 Gemfile create mode 100644 README.adoc create mode 100644 Rakefile create mode 100644 ali.gemspec create mode 100755 bin/console create mode 100755 bin/setup create mode 100644 lib/ali.rb create mode 100644 lib/ali/container.rb create mode 100644 lib/ali/free_to_read.rb create mode 100644 lib/ali/license_ref.rb create mode 100644 lib/ali/version.rb create mode 100644 sig/ali.rbs create mode 100644 spec/ali/container_spec.rb create mode 100644 spec/ali/free_to_read_spec.rb create mode 100644 spec/ali/license_ref_spec.rb create mode 100644 spec/ali_spec.rb create mode 100644 spec/fixtures/json/spec_example_1.json create mode 100644 spec/fixtures/json/spec_example_2.json create mode 100644 spec/fixtures/json/spec_example_3.json create mode 100644 spec/fixtures/json/spec_example_4.json create mode 100644 spec/fixtures/json/spec_example_5.json create mode 100644 spec/fixtures/json/spec_example_6.json create mode 100644 spec/fixtures/xml/spec_example_1.xml create mode 100644 spec/fixtures/xml/spec_example_2.xml create mode 100644 spec/spec_helper.rb diff --git a/.github/workflows/rake.yml b/.github/workflows/rake.yml new file mode 100644 index 0000000..23c496f --- /dev/null +++ b/.github/workflows/rake.yml @@ -0,0 +1,12 @@ +name: rake + +on: + push: + branches: [ master, main ] + tags: [ v* ] + pull_request: + workflow_dispatch: + +jobs: + rake: + uses: metanorma/ci/.github/workflows/graphviz-rake.yml@main diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..aa6ca31 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,22 @@ +name: release + +on: + workflow_dispatch: + inputs: + next_version: + description: | + Next release version. Possible values: x.y.z, major, minor, patch or pre|rc|etc + required: true + default: 'skip' + push: + tags: [ v* ] + +jobs: + release: + uses: metanorma/ci/.github/workflows/rubygems-release.yml@main + with: + next_version: ${{ github.event.inputs.next_version }} + secrets: + rubygems-api-key: ${{ secrets.LUTAML_CI_RUBYGEMS_API_KEY }} + pat_token: ${{ secrets.LUTAML_CI_PAT_TOKEN }} + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..bb8409c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,34 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + NewCops: enable + SuggestExtensions: false + TargetRubyVersion: 2.7 + +Gemspec/DevelopmentDependencies: + Enabled: false + +Gemspec/RequireMFA: + Enabled: false + +Metrics/BlockLength: + AllowedMethods: + - describe + +Style/Documentation: + Enabled: false + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + EnforcedStyle: double_quotes + +Style/TrailingCommaInArguments: + EnforcedStyleForMultiline: consistent_comma + +Style/TrailingCommaInArrayLiteral: + EnforcedStyleForMultiline: consistent_comma + +Style/TrailingCommaInHashLiteral: + EnforcedStyleForMultiline: consistent_comma diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..9aadc1b --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,51 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2024-08-27 09:46:41 UTC using RuboCop version 1.65.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches. +Lint/DuplicateBranch: + Exclude: + - 'lib/genericode/code_list.rb' + +# Offense count: 8 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 182 + +# Offense count: 2 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 34 + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne. +Metrics/ClassLength: + Max: 220 + +# Offense count: 5 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/CyclomaticComplexity: + Max: 92 + +# Offense count: 10 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +Metrics/MethodLength: + Max: 86 + +# Offense count: 3 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/PerceivedComplexity: + Max: 92 + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 121 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f4438e4 --- /dev/null +++ b/Gemfile @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# Specify your gem's dependencies in reqif.gemspec +gemspec + +gem "nokogiri" +gem "rake", "~> 13.0" +gem "rspec", "~> 3.0" +gem "rubocop", "~> 1.21" +gem "rubocop-performance" +gem "xml-c14n" diff --git a/README.adoc b/README.adoc new file mode 100644 index 0000000..d8d1460 --- /dev/null +++ b/README.adoc @@ -0,0 +1,171 @@ += ALI + +image:https://img.shields.io/gem/v/ali.svg["Gem Version", link="https://rubygems.org/gems/ali"] +image:https://github.com/lutaml/ali/workflows/rake/badge.svg["Build Status", link="https://github.com/lutaml/ali/actions?workflow=rake"] +image:https://codeclimate.com/github/lutaml/ali/badges/gpa.svg["Code Climate", link="https://codeclimate.com/github/lutaml/ali"] + +== Purpose + +This Ruby gem implements the +https://www.niso.org/schemas/ali/1.0[NISO Access and License Indicators (ALI) Schemas]. + +Specification: + +* https://www.niso.org/publications/rp-22-2021-ali[NISO RP 22-2021] + +It can be used to parse and generate ALI XML and JSON files. + +NOTE: NISO ALI is used by the NISO JATS standard to indicate the access and +license information of a document. + +NOTE: The `ali` gem is used by the `niso-jats` gem to parse and generate ALI +files in the context of JATS documents. + + +== Installation + +To install the `ali` gem, use one of the following methods. + +Add this line to your application's Gemfile: + +[source,ruby] +---- +gem 'ali' +---- + +Then execute: + +[source,sh] +---- +$ bundle install +---- + +Or install it directly using: + +[source,sh] +---- +$ gem install ali +---- + +After installation, you can start using the ali gem in your Ruby projects +or via the command-line interface. + + + +== API + +=== General + +The gem provides the following classes: + +* `Ali::FreeToRead`:: Represents the `free_to_read` element. +* `Ali::License`:: Represents the `license` element. +* `Ali::Container`:: Represents the root element of the ALI document. (Applies to JSON only) + +=== Differences between XML and JSON usage of ALI + +Notice that the XML usage and JSON usage of ALI differs: + +XML:: The `free_to_read` and `license` elements are directly incorporated into +an XML document that is defined by an external schema. ALI itself does not +define any root container. + +JSON-LD:: The `free_to_read` and `license` elements are contained in a JSON +object under the JSON-LD context defined in ALI. In the `ali` gem, the +`Ali::Container` class is used to represent the root element of the ALI +document. + +=== Usage with XML + +When parsing ALI in XML, you need to incorporate the ALI element +classes in the context of the XML document. + +[example] +==== +Usage of ALI elements in XML is not contained in a particular element. + +[source,xml] +---- + + + + + http://www.examplesite.org/open_license.html + + + +---- +==== + +.Usage with XML +[source,ruby] +---- +> require 'ali' +> xml = '' +> free_to_read = Ali::FreeToRead.from_xml(xml) +> free_to_read.start_date +# => "2014-01-01" +> free_to_read.end_date +# => "2014-12-31" +---- + +=== Usage with JSON + +When parsing ALI in JSON, you need to use the `Ali::Container` class to +parse the root element of the ALI object. + +[example] +==== +Usage of ALI in JSON-LD is contained in an JSON object. + +[source,json] +---- +{ + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "free_to_read": { + "start_date": "2014-01-01", + "end_date": "2014-12-31" + }, + "license_ref": { + "start_date": "2014-04-04", + "uri": "http://creativecommons.org/licenses/by/3.0/" + } +} +---- +==== + +.Usage with JSON +[source,ruby] +---- +> require 'ali' +> json = '{"free_to_read": {"start_date": "2014-01-01", "end_date": "2014-12-31"}}' +> container = Ali::Container.from_json(json) +> free_to_read = container.free_to_read +> free_to_read.start_date +# => "2014-01-01" +> free_to_read.end_date +# => "2014-12-31" +---- + + +== Tests + +The `spec/fixtures` folder tests the library against known examples of ALI. + +Including: + +`spec/fixtures/json`:: JSON examples from +https://www.niso.org/publications/rp-22-2021-ali[NISO RP 22-2021], A.4. + +`spec/fixtures/xml`:: XML examples from +https://www.niso.org/publications/rp-22-2021-ali[NISO RP 22-2021], A.1. + + +== License + +Copyright Ribose. + +BSD-3 license. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..cca7175 --- /dev/null +++ b/Rakefile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: %i[spec rubocop] diff --git a/ali.gemspec b/ali.gemspec new file mode 100644 index 0000000..50a8793 --- /dev/null +++ b/ali.gemspec @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require_relative "lib/ali/version" + +Gem::Specification.new do |spec| + spec.name = "ali" + spec.version = Ali::VERSION + spec.authors = ["Ribose Inc."] + spec.email = ["open.source@ribose.com'"] + + spec.summary = "NISO Access and License Indicators (ALI) Schemas library" + spec.description = "A Ruby library for parsing and generating NISO Access and License Indicators (ALI) Schemas data" + spec.homepage = "https://github.com/lutaml/ali" + spec.license = "MIT" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = spec.homepage + spec.metadata["changelog_uri"] = "https://github.com/lutaml/ali/releases" + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem + # that have been added into git. + spec.files = Dir.chdir(File.expand_path(__dir__)) do + `git ls-files -z` + .split("\x0") + .reject { |f| f.match(%r{^(test|spec|features)/}) } + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.required_ruby_version = ">= 3.0.0" + + spec.add_dependency "lutaml-model" +end diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..30b0e30 --- /dev/null +++ b/bin/console @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "ali" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/ali.rb b/lib/ali.rb new file mode 100644 index 0000000..53a9006 --- /dev/null +++ b/lib/ali.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require_relative "ali/version" +require "lutaml/model" + +module Ali + class Error < StandardError; end + + # Your code goes here... +end + +require_relative "ali/free_to_read" +require_relative "ali/license_ref" +require_relative "ali/container" diff --git a/lib/ali/container.rb b/lib/ali/container.rb new file mode 100644 index 0000000..2f2d14a --- /dev/null +++ b/lib/ali/container.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Ali + # The container is only used in the case of JSON and is not from the NISO ALI + # spec. The NISO ALI contains multiple JSON examples stored in object form + # that assumes an object container to parse. + class Container < Lutaml::Model::Serializable + attribute :free_to_read, FreeToRead, collection: true + attribute :license_ref, LicenseRef, collection: true + + xml do + root "ali-container" + namespace "http://www.niso.org/schemas/ali/1.0/" + + map_element "free_to_read", to: :free_to_read + map_element "license_ref", to: :license_ref + end + + json do + map "free_to_read", to: :free_to_read + map "license_ref", to: :license_ref + end + end +end diff --git a/lib/ali/free_to_read.rb b/lib/ali/free_to_read.rb new file mode 100644 index 0000000..7268282 --- /dev/null +++ b/lib/ali/free_to_read.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Ali + class FreeToRead < Lutaml::Model::Serializable + attribute :start_date, :date + attribute :end_date, :date + + xml do + root "free_to_read" + namespace "http://www.niso.org/schemas/ali/1.0/" + + map_attribute "start_date", to: :start_date + map_attribute "end_date", to: :end_date + end + + json do + map "start_date", to: :start_date + map "end_date", to: :end_date + end + end +end diff --git a/lib/ali/license_ref.rb b/lib/ali/license_ref.rb new file mode 100644 index 0000000..deff875 --- /dev/null +++ b/lib/ali/license_ref.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Ali + class LicenseRef < Lutaml::Model::Serializable + attribute :start_date, :date + attribute :applies_to, :string + attribute :uri, :string + + xml do + root "license_ref" + namespace "http://www.niso.org/schemas/ali/1.0/" + + map_attribute "start_date", to: :start_date + map_attribute "applies_to", to: :applies_to + map_content to: :uri + end + + json do + map "start_date", to: :start_date + map "applies_to", to: :applies_to + map "uri", to: :uri + end + end +end diff --git a/lib/ali/version.rb b/lib/ali/version.rb new file mode 100644 index 0000000..4d8a8fb --- /dev/null +++ b/lib/ali/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Ali + VERSION = "0.1.0" +end diff --git a/sig/ali.rbs b/sig/ali.rbs new file mode 100644 index 0000000..fe2bce7 --- /dev/null +++ b/sig/ali.rbs @@ -0,0 +1,4 @@ +module Ali + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides +end diff --git a/spec/ali/container_spec.rb b/spec/ali/container_spec.rb new file mode 100644 index 0000000..e4e8749 --- /dev/null +++ b/spec/ali/container_spec.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Ali::Container do + context "json" do + let(:parsed) do + described_class.from_json(content) + end + let(:generated) do + parsed.to_json(pretty: true) + end + + context "with start_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": { + "start_date": "2014-04-04", + "uri": "http://creativecommons.org/licenses/by/3.0/" + } + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "without start_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": "http://creativecommons.org/licenses/by/3.0/" + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with multiple instances" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": [ + { + "start_date": "2014-04-04", + "uri": "http://creativecommons.org/licenses/by/3.0/" + }, + { + "start_date": "2015-04-04", + "uri": "http://creativecommons.org/licenses/by/4.0/" + } + ] + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with applies_to" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": [ + { + "start_date": "2015-04-04", + "applies_to": "https://doi.org/10.15223/asf-profile", + "uri": "https://doi.org/10.15223/policy-005" + }, + { + "start_date": "2015-04-04", + "applies_to": "https://doi.org/10.15223/asf-profile", + "uri": "https://doi.org/10.15223/policy-025" + } + ] + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + end +end diff --git a/spec/ali/free_to_read_spec.rb b/spec/ali/free_to_read_spec.rb new file mode 100644 index 0000000..b0acee3 --- /dev/null +++ b/spec/ali/free_to_read_spec.rb @@ -0,0 +1,129 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Ali::FreeToRead do + context "xml" do + let(:parsed) do + described_class.from_xml(content) + end + let(:generated) do + parsed.to_xml(pretty: true, declaration: true, encoding: "utf-8") + end + + context "with start_date and no end_date" do + let(:content) do + <<~CONTENT + + CONTENT + end + + it "generates valid ALI XML" do + expect(generated).to be_analogous_with(content) + end + end + + context "with start_date and end_date" do + let(:content) do + <<~CONTENT + + CONTENT + end + + it "generates valid ALI XML" do + expect(generated).to be_analogous_with(content) + end + end + + context "with only end_date" do + let(:content) do + <<~CONTENT + + CONTENT + end + + it "generates valid ALI XML" do + expect(generated).to be_analogous_with(content) + end + end + end + + context "json" do + let(:parsed) do + described_class.from_json(content) + end + let(:generated) do + parsed.to_json(pretty: true) + end + + context "without any date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "free_to_read": true + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with start_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "free_to_read": { + "start_date": "2013-02-03" + } + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with end_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "free_to_read": { + "end_date": "2013-10-03" + } + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with start_date and end_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "free_to_read": { + "start_date": "2013-02-03", + "end_date": "2013-10-03" + } + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + end +end diff --git a/spec/ali/license_ref_spec.rb b/spec/ali/license_ref_spec.rb new file mode 100644 index 0000000..4e9e2b8 --- /dev/null +++ b/spec/ali/license_ref_spec.rb @@ -0,0 +1,149 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Ali::LicenseRef do + context "xml" do + let(:parsed) do + described_class.from_xml(content) + end + let(:generated) do + parsed.to_xml(pretty: true, declaration: true, encoding: "utf-8") + end + + context "with start_date" do + let(:content) do + <<~CONTENT + + http://www.examplesite.org/open_license.html + + CONTENT + end + + it "generates valid ALI XML" do + expect(generated).to be_analogous_with(content) + end + end + + context "without start_date" do + let(:content) do + <<~CONTENT + + http://www.examplesite.org/open_license.html + + CONTENT + end + + it "generates valid ALI XML" do + expect(generated).to be_analogous_with(content) + end + end + + context "with applies_to" do + let(:content) do + <<~CONTENT + http://www.examplesite.org/open_license.html + CONTENT + end + + it "generates valid ALI XML" do + expect(generated).to be_analogous_with(content) + end + end + end + + context "json" do + let(:parsed) do + described_class.from_json(content) + end + let(:generated) do + parsed.to_json(pretty: true) + end + + context "with start_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": { + "start_date": "2014-04-04", + "uri": "http://creativecommons.org/licenses/by/3.0/" + } + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "without start_date" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": "http://creativecommons.org/licenses/by/3.0/" + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with multiple instances" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": [ + { + "start_date": "2014-04-04", + "uri": "http://creativecommons.org/licenses/by/3.0/" + }, + { + "start_date": "2015-04-04", + "uri": "http://creativecommons.org/licenses/by/4.0/" + } + ] + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + + context "with applies_to" do + let(:content) do + <<~CONTENT + { + "@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", + "license_ref": [ + { + "start_date": "2015-04-04", + "applies_to": "https://doi.org/10.15223/asf-profile", + "uri": "https://doi.org/10.15223/policy-005" + }, + { + "start_date": "2015-04-04", + "applies_to": "https://doi.org/10.15223/asf-profile", + "uri": "https://doi.org/10.15223/policy-025" + } + ] + } + CONTENT + end + + it "generates valid ALI JSON" do + expect(generated).to eq(content) + end + end + end +end diff --git a/spec/ali_spec.rb b/spec/ali_spec.rb new file mode 100644 index 0000000..b07944b --- /dev/null +++ b/spec/ali_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Ali do + # let(:xml_file) { File.read("spec/fixtures/xml/spec_example_1.xml") } + # let(:obj) { described_class.from_xml(xml_file) } + + # describe ".from_xml" do + # it "parses ALI XML correctly" do + # expect(obj).to be_a(described_class) + # end + + # it "maintains XML namespaces" do + # xml_output = obj.to_xml + # expect(xml_output).to include('xmlns="http://www.niso.org/schemas/ali/1.0/"') + # end + # end + + # describe "#to_xml" do + # it "generates valid ALI XML" do + # xml_output = obj.to_xml + # regenerated_obj = described_class.from_xml(xml_output) + # expect(regenerated_obj.the_header.title).to eq(obj.the_header.title) + # end + # end +end diff --git a/spec/fixtures/json/spec_example_1.json b/spec/fixtures/json/spec_example_1.json new file mode 100644 index 0000000..56a7ad3 --- /dev/null +++ b/spec/fixtures/json/spec_example_1.json @@ -0,0 +1,4 @@ +{ +"@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", +"free_to_read": true +} \ No newline at end of file diff --git a/spec/fixtures/json/spec_example_2.json b/spec/fixtures/json/spec_example_2.json new file mode 100644 index 0000000..504f42f --- /dev/null +++ b/spec/fixtures/json/spec_example_2.json @@ -0,0 +1,7 @@ +{ +"@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", +"free_to_read": { +"start_date": "2013-02-03", +"end_date": "2013-10-03" +} +} \ No newline at end of file diff --git a/spec/fixtures/json/spec_example_3.json b/spec/fixtures/json/spec_example_3.json new file mode 100644 index 0000000..5841a34 --- /dev/null +++ b/spec/fixtures/json/spec_example_3.json @@ -0,0 +1,5 @@ +{ +"@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", +"free_to_read": true, +"license_ref": "http://creativecommons.org/licenses/by/3.0/" +} \ No newline at end of file diff --git a/spec/fixtures/json/spec_example_4.json b/spec/fixtures/json/spec_example_4.json new file mode 100644 index 0000000..a5fc7b7 --- /dev/null +++ b/spec/fixtures/json/spec_example_4.json @@ -0,0 +1,8 @@ +{ +"@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", +"free_to_read": true, +"license_ref": { +"start_date": "2014-04-04", +"uri": "http://creativecommons.org/licenses/by/3.0/" +} +} \ No newline at end of file diff --git a/spec/fixtures/json/spec_example_5.json b/spec/fixtures/json/spec_example_5.json new file mode 100644 index 0000000..d102a4d --- /dev/null +++ b/spec/fixtures/json/spec_example_5.json @@ -0,0 +1,10 @@ +{ +"@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", +"free_to_read": true, +"license_ref": [ +{ "start_date": "2014-04-04", +"uri": "http://creativecommons.org/licenses/by/3.0/" }, +{ "start_date": "2015-04-04", +"uri": "http://creativecommons.org/licenses/by/4.0/" } +] +} \ No newline at end of file diff --git a/spec/fixtures/json/spec_example_6.json b/spec/fixtures/json/spec_example_6.json new file mode 100644 index 0000000..85dfc73 --- /dev/null +++ b/spec/fixtures/json/spec_example_6.json @@ -0,0 +1,12 @@ +{ +"@context": "http://www.niso.org/schemas/ali/1.0.1/jsonld.json", +"free_to_read": true, +"license_ref": [ +{ "start_date": "2015-04-04", +"applies_to": "https://doi.org/10.15223/asf-profile", +"uri": "https://doi.org/10.15223/policy-005" }, +{ "start_date": "2015-04-04", +"applies_to": "https://doi.org/10.15223/asf-profile", +"uri": "https://doi.org/10.15223/policy-025" }, +] +} \ No newline at end of file diff --git a/spec/fixtures/xml/spec_example_1.xml b/spec/fixtures/xml/spec_example_1.xml new file mode 100644 index 0000000..cd2bc42 --- /dev/null +++ b/spec/fixtures/xml/spec_example_1.xml @@ -0,0 +1,5 @@ + + + http://www.examplesite.org/open_license.html + \ No newline at end of file diff --git a/spec/fixtures/xml/spec_example_2.xml b/spec/fixtures/xml/spec_example_2.xml new file mode 100644 index 0000000..12b81a9 --- /dev/null +++ b/spec/fixtures/xml/spec_example_2.xml @@ -0,0 +1,7 @@ + +http://www.examplesite.org/open_license.html \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..5cad877 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require "ali" +require "nokogiri" +require "xml-c14n" + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end + +require "lutaml/model" +require "lutaml/model/xml_adapter/nokogiri_adapter" +require "lutaml/model/json_adapter/standard_json_adapter" +require "lutaml/model/yaml_adapter/standard_yaml_adapter" + +Lutaml::Model::Config.configure do |config| + config.xml_adapter = Lutaml::Model::XmlAdapter::NokogiriAdapter + config.json_adapter = Lutaml::Model::JsonAdapter::StandardJsonAdapter + config.yaml_adapter = Lutaml::Model::YamlAdapter::StandardYamlAdapter +end