chronicle-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a168e93bd8ec5d03b056c25bdfa2b500ba88bd98869121ac427c4e6452b2c51e
4
+ data.tar.gz: 638310a4a890e3b9ece2d26a99403f78293975f257d05df065dd3107146fffea
5
+ SHA512:
6
+ metadata.gz: 8462b795ea806166e4ca538042c91c16d22e641bcae19e7228714911c8604cace59efe8bea79ca4f7e582191c51b09d8c5b73b33ac34255ab04693e89d7ce85c
7
+ data.tar.gz: b2ead2ca9b9cb9afddba562881cc9026488c4177c9da62e282bb1acb17a9e11e1ec7782269ac5093ca1143de83951d8db6906864e0803d58c858bc3ec0a7f3f4
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
+ Gemfile.lock
11
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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 andrew@hyfen.net. 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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in chronicle-core.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ watch('spec/spec_helper.rb') { "spec" }
7
+ end
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Chronicle::Core
2
+
3
+ Home of some shared code for [Chronicle](https://github.com/chronicle-app/). Currently, this means the models and a validator for Chronicle Schema.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
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 "chronicle/core"
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
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/chronicle/core/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "chronicle-core"
7
+ spec.version = Chronicle::Core::VERSION
8
+ spec.authors = ["Andrew Louis"]
9
+ spec.email = ["andrew@hyfen.net"]
10
+
11
+ spec.summary = "Core libraries for Chronicle"
12
+ spec.description = "Core libraries for Chronicle including models and schema definitions."
13
+ spec.homepage = "https://github.com/chronicle-app"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7"
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/chronicle-app/chronicle-core"
22
+ spec.metadata["changelog_uri"] = "https://github.com/chronicle-app/chronicle-core/releases"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "dry-schema", "~> 1.13"
36
+
37
+ spec.add_development_dependency "guard-rspec", "~> 4.7.3"
38
+ spec.add_development_dependency "pry-byebug", "~> 3.9"
39
+ spec.add_development_dependency "rspec", "~> 3.9"
40
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chronicle
4
+ module Core
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "core/version"
4
+
5
+ begin
6
+ require 'pry'
7
+ rescue LoadError
8
+ # Pry not available
9
+ end
@@ -0,0 +1,5 @@
1
+ module Chronicle::Schema
2
+ class Activity < Chronicle::Schema::Base
3
+ TYPE = 'activities'.freeze
4
+ end
5
+ end
@@ -0,0 +1,103 @@
1
+ require 'chronicle/utils/hash'
2
+
3
+ module Chronicle::Schema
4
+ class Base
5
+ attr_accessor(:id, :dedupe_on, :properties)
6
+
7
+ def initialize(attributes = {})
8
+ @properties = {}
9
+ @dedupe_on = []
10
+ assign_attributes(attributes) if attributes
11
+ end
12
+
13
+ def method_missing(method, *args)
14
+ if method.to_s.end_with?("=")
15
+ @properties[method.to_s.gsub("=", "").to_sym] = args.first
16
+ else
17
+ @properties[method]
18
+ end
19
+ end
20
+
21
+ def identifier_hash
22
+ {
23
+ type: self.class::TYPE,
24
+ id: @id
25
+ }.compact
26
+ end
27
+
28
+ def to_h_jsonapi
29
+ identifier_hash.merge({
30
+ attributes: attributes,
31
+ relationships: associations.map do |k, v|
32
+ if v.is_a?(Array)
33
+ [k, { data: v.map(&:to_h_jsonapi) }]
34
+ else
35
+ [k, { data: v.to_h_jsonapi }]
36
+ end
37
+ end.to_h,
38
+ }).merge(meta_hash)
39
+ .compact
40
+ end
41
+
42
+ def attributes
43
+ @properties.reject { |k, v| associations.keys.include?(k) }.compact
44
+ end
45
+
46
+ def associations
47
+ @properties.select do |k, v|
48
+ if v.is_a?(Array)
49
+ v.any? { |e| e.is_a?(Chronicle::Schema::Base) }
50
+ else
51
+ v.is_a?(Chronicle::Schema::Base)
52
+ end
53
+ end
54
+ end
55
+
56
+ def associations_hash
57
+ associations.map do |k, v|
58
+ if v.is_a?(Array)
59
+ [k, v.map(&:to_h)]
60
+ else
61
+ [k, v.to_h]
62
+ end
63
+ end.to_h
64
+ end
65
+
66
+ def meta
67
+ {
68
+ dedupe_on: @dedupe_on.map{|d| d.map(&:to_s).join(",")}
69
+ }
70
+ end
71
+
72
+ def meta_hash
73
+ {
74
+ meta: meta
75
+ }
76
+ end
77
+
78
+ def identifier_hash
79
+ {
80
+ id: @id,
81
+ type: self.class::TYPE
82
+ }.compact
83
+ end
84
+
85
+ def to_h_flattened
86
+ Chronicle::Utils::Hash.flatten_keys(to_h)
87
+ end
88
+
89
+ def to_h
90
+ identifier_hash
91
+ .merge(@properties)
92
+ .merge(meta_hash)
93
+ end
94
+
95
+ def assign_attributes(attributes)
96
+ @properties.merge!(attributes)
97
+ end
98
+ end
99
+ end
100
+
101
+ require_relative "activity"
102
+ require_relative "entity"
103
+ require_relative "raw"
@@ -0,0 +1,5 @@
1
+ module Chronicle::Schema
2
+ class Entity < Chronicle::Schema::Base
3
+ TYPE = 'entities'.freeze
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Chronicle::Schema
2
+ class Raw < Chronicle::Schema::Base
3
+ TYPE = 'raw'.freeze
4
+
5
+ def to_h
6
+ @properties
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,53 @@
1
+ require 'dry/schema' # TODO: Lazy load this
2
+ Dry::Schema.load_extensions(:hints)
3
+ Dry::Schema.load_extensions(:info)
4
+
5
+ module Chronicle::Schema
6
+ class Validator
7
+ EntitySchema = Dry::Schema.Params do
8
+ required(:type).filled(:string, eql?: 'entities')
9
+ required(:attributes).type(:hash)
10
+ end
11
+
12
+ EntityIdentifiableSchema = Dry::Schema.Params do
13
+ required(:provider).filled(:string)
14
+ end
15
+
16
+ ActorEntitySchema = Dry::Schema.Params do
17
+ required(:represents).filled(:string, eql?: 'identity')
18
+ end
19
+
20
+ ActivitySchema = Dry::Schema.Params do
21
+ required(:type).filled(:string, eql?: 'activities')
22
+ required(:attributes).schema do
23
+ required(:verb).filled(:string)
24
+ optional(:start_at).maybe(:string) # Adjust type as needed
25
+ optional(:end_at).maybe(:string) # Adjust type as needed
26
+ end
27
+ required(:relationships).schema do
28
+ required(:actor).hash do
29
+ required(:data).hash(EntitySchema) do
30
+ required(:attributes).schema(EntityIdentifiableSchema & ActorEntitySchema)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def self.validate(input)
37
+ case input
38
+ when Entity
39
+ result = EntitySchema.call(input.to_h_jsonapi)
40
+ when Activity
41
+ result = ActivitySchema.call(input.to_h_jsonapi)
42
+ else
43
+ raise ArgumentError, "Unsupported type: #{input.class}"
44
+ end
45
+
46
+ if result.success?
47
+ { success: true, data: result.to_h }
48
+ else
49
+ { success: false, errors: result.errors.to_h, hints: result.hints.to_h }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ module Chronicle
2
+ module Schema
3
+ end
4
+ end
5
+
6
+ require_relative "schema/base"
7
+ require_relative "schema/validator"
@@ -0,0 +1,15 @@
1
+ module Chronicle::Utils
2
+ module Hash
3
+ def self.flatten_keys(hash)
4
+ hash.each_with_object({}) do |(k, v), h|
5
+ if v.is_a? Hash
6
+ flatten_hash(v).map do |h_k, h_v|
7
+ h["#{k}.#{h_k}".to_sym] = h_v
8
+ end
9
+ else
10
+ h[k] = v
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module Chronicle
2
+ module Core
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chronicle-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Louis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.7.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.7.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ description: Core libraries for Chronicle including models and schema definitions.
70
+ email:
71
+ - andrew@hyfen.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Guardfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - chronicle-core.gemspec
86
+ - lib/chronicle/core.rb
87
+ - lib/chronicle/core/version.rb
88
+ - lib/chronicle/schema.rb
89
+ - lib/chronicle/schema/activity.rb
90
+ - lib/chronicle/schema/base.rb
91
+ - lib/chronicle/schema/entity.rb
92
+ - lib/chronicle/schema/raw.rb
93
+ - lib/chronicle/schema/validator.rb
94
+ - lib/chronicle/utils/hash.rb
95
+ - sig/chronicle/core.rbs
96
+ homepage: https://github.com/chronicle-app
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ allowed_push_host: https://rubygems.org
101
+ homepage_uri: https://github.com/chronicle-app
102
+ source_code_uri: https://github.com/chronicle-app/chronicle-core
103
+ changelog_uri: https://github.com/chronicle-app/chronicle-core/releases
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '2.7'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubygems_version: 3.3.3
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Core libraries for Chronicle
123
+ test_files: []