Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ScimShady #282

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/scim_shady.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: scim_shady

on:
push:

jobs:
ruby:
uses: powerhome/github-actions-workflows/.github/workflows/ruby-gem.yml@main
with:
package: ${{ github.workflow }}
workdir: "packages/${{ github.workflow }}"
ruby: '["3.0","3.1","3.2","3.3"]'
secrets: inherit
10 changes: 10 additions & 0 deletions packages/scim_shady/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions packages/scim_shady/.standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# For available configuration options, see:
# https://github.com/testdouble/standard
ruby_version: 2.6
5 changes: 5 additions & 0 deletions packages/scim_shady/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2024-06-21

- Initial release
12 changes: 12 additions & 0 deletions packages/scim_shady/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in scim_shady.gemspec
gemspec

gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "standard", "~> 1.3"

gem "pry", "~> 0.14.2"
110 changes: 110 additions & 0 deletions packages/scim_shady/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
PATH
remote: .
specs:
scim_shady (0.1.0)
activemodel (> 6.0, < 7.2)

GEM
remote: https://rubygems.org/
specs:
activemodel (7.1.3.4)
activesupport (= 7.1.3.4)
activesupport (7.1.3.4)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
coderay (1.1.3)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
diff-lcs (1.5.1)
drb (2.2.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
method_source (1.1.0)
minitest (5.24.0)
mutex_m (0.2.0)
parallel (1.25.1)
parser (3.3.3.0)
ast (~> 2.4.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.8.0)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.2)
rexml (3.3.0)
strscan
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.64.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (1.13.0)
standard (1.37.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.64.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.4)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.4.0)
lint_roller (~> 1.1)
rubocop-performance (~> 1.21.0)
strscan (3.1.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
pry (~> 0.14.2)
rake (~> 13.0)
rspec (~> 3.0)
scim_shady!
standard (~> 1.3)

BUNDLED WITH
2.5.11
21 changes: 21 additions & 0 deletions packages/scim_shady/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Carlos Palhares

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions packages/scim_shady/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ScimShady

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/scim_shady`. To experiment with that code, run `bin/console` for an interactive prompt.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.

Install the gem and add to the application's Gemfile by executing:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG

## Usage

TODO: Write usage instructions here

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/scim_shady.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
10 changes: 10 additions & 0 deletions packages/scim_shady/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "standard/rake"

task default: %i[spec standard]
11 changes: 11 additions & 0 deletions packages/scim_shady/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "scim_shady"

# 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__)
8 changes: 8 additions & 0 deletions packages/scim_shady/bin/setup
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions packages/scim_shady/doc/dependency_decisions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- - :inherit_from
- https://raw.githubusercontent.com/powerhome/oss-guide/master/license_rules.yml
35 changes: 35 additions & 0 deletions packages/scim_shady/lib/scim_shady.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "active_model"

module ScimShady
# Client
autoload :Client, "scim_shady/client"
autoload :MockClient, "scim_shady/mock_client"

# Base model
autoload :Base, "scim_shady/base"
autoload :Persistence, "scim_shady/persistence"
autoload :ResourceQuery, "scim_shady/resource_query"
autoload :SchemaAttributes, "scim_shady/schema_attributes"

# Payloads
autoload :PatchOp, "scim_shady/patch_op"
autoload :ScimJson, "scim_shady/scim_json"

# Others
autoload :QueryBuilder, "scim_shady/query_builder"
autoload :ListResponse, "scim_shady/list_response"
autoload :Schema, "scim_shady/schema"
autoload :VERSION, "scim_shady/version"

# Errors
autoload :Error, "scim_shady/errors"
autoload :UnknownError, "scim_shady/errors"
autoload :AuthenticationError, "scim_shady/errors"
autoload :RequestError, "scim_shady/errors"
autoload :UnknownSchema, "scim_shady/errors"
autoload :ResourceNotFound, "scim_shady/errors"

mattr_accessor :client
end
24 changes: 24 additions & 0 deletions packages/scim_shady/lib/scim_shady/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module ScimShady
class Base
include ResourceQuery
include SchemaAttributes
include Persistence
include ActiveModel::Serializers::JSON

def initialize(attributes = {})
super()

assign_attributes(attributes)
end

def self.resource_path
name.pluralize
end

def resource_path
self.class.resource_path
end
end
end
76 changes: 76 additions & 0 deletions packages/scim_shady/lib/scim_shady/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

require "net/http"
require "json"
require "uri"

module ScimShady
class Client
attr_reader :uri, :headers, :logger

def initialize(uri:, headers: {}, logger: nil)
@uri = URI.parse(uri)
@headers = headers.freeze
@logger = logger
end

def get(**kwargs)
perform_request(method: :Get, **kwargs)
end

def post(**kwargs)
perform_request(method: :Post, **kwargs)
end

def put(**kwargs)
perform_request(method: :Put, **kwargs)
end

def patch(**kwargs)
perform_request(method: :Patch, **kwargs)
end

def perform_request(method:, path:, query: {}, list: nil, model: nil, body: nil)
request_uri = URI.join(uri, path.to_s)
request_uri.query = URI.encode_www_form(query)
logger&.info "Requesting #{request_uri.inspect}"
request = ::Net::HTTP.const_get(method).new(request_uri, headers)
request.body = body.to_json if body
response = http.request(request)

handle_response response, list: list, model: model
end

private

def http
@http ||= ::Net::HTTP.new(uri.host, uri.port).tap do |http|
http.use_ssl = uri.scheme == "https"
end
end

def handle_response(response, **kwargs)
case response
when Net::HTTPSuccess then handle_success(response.body, **kwargs)
when Net::HTTPNotFound then raise ScimShady::ResourceNotFound, response.body
when Net::HTTPUnauthorized then raise ScimShady::AuthenticationError, response.body
when Net::HTTPClientError then raise ScimShady::RequestError, response.body
when Net::HTTPServerError then raise ScimShady::ServerError, response.body
else
raise ScimShady::UnknownError, response.body.inspect
end
end

def handle_success(body, list: false, model: false)
parsed_body = JSON.parse(body)

if list
ScimShady::ListResponse.new(parsed_body, list)
elsif model
model.new(parsed_body)
else
parsed_body
end
end
end
end
Loading
Loading