Skip to content
Shared utils for Studitemp's Ruby projects.
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
lib/studitemps
spec
.codecov.yml
.gitignore
.rspec
.rubocop.yml
.travis.yml
Gemfile
Gemfile.lock
LICENSE.txt
README.md
Rakefile
studitemps-utils.gemspec

README.md

Studitemps::Utils

Shared utils for Studitemp's Ruby projects.

Status

TravisCI codecov Depfu Maintainability Inline docs Yard Docs

Installation

Add this line to your application's Gemfile:

gem 'studitemps-utils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install studitemps-utils

URI

An Studitemps Utils URI references similar to a normal URI a specific resource. It contains at least a schema but most of the time when used to reference a resource it also has a context, resource, and an id.

Example: com.example:billing:invoice:R422342

  • schema: com.example - Some kind of schema to make URI globally unique.
  • context: billing - The context the URI (and the resource) belongs to.
  • resource: invoice - The resource type.
  • id: R422342 - The resource id.

Usage

require 'studitemps/utils/uri'

MyBaseURI = Studitemps::Utils::URI.build(schema: 'com.example')
InvoiceURI = Studitemps::Utils::URI.build(from: MyBaseURI, context: 'billing', resource: 'invoice')

uri = InvoiceURI.new(id: 'R422342') # => #<InvoiceURI 'com.example:billing:invoice:R422342'>
uri.to_s # => 'com.example:billing:invoice:R422342'

InvoiceURI.build('com.example:billing:invoice:R422342') # => #<InvoiceURI 'com.example:billing:invoice:R422342'>
InvoiceURI.build(id: 'R422342') # => #<InvoiceURI 'com.example:billing:invoice:R422342'>
InvoiceURI.build(uri) # => #<InvoiceURI 'com.example:billing:invoice:R422342'>

# `resource` and `id` supports array so that `.build` will verify every value for a given string.
# If we also want to check the initializer we can use the "types" extension to do so:
require 'studitemps/utils/uri/extensions/types'

InvoiceURI = Studitemps::Utils::URI.build(
  schema: 'com.example', context: 'billing', resource: 'invoice', id: %w[final past_due]
)

InvoiceURI.build('com.example:billing:invoice:pro_forma') # => Studitemps::Utils::URI::Base::InvalidURI
InvoiceURI.new(id: 'final') # => #<InvoiceURI 'com.example:billing:invoice:final'>
InvoiceURI.new(id: 'pro_forma') # => Dry::Types::ConstraintError

# instead of enums for resources we can also use different URIs with sum types.
InvoiceDuplicateURI = Studitemps::Utils::URI.build(from: InvoiceURI, resource: 'invoice_duplicate')

InvoicesType = InvoiceURI::Types::URI | InvoiceDuplicateURI::Types::URI
InvoicesType[InvoiceURI.new(id: 'final')] # => <#InvoiceURI 'com.example:billing:invoice:final'>
InvoicesType[InvoiceDuplicateURI.new(id: 'final')] # => <#InvoiceDuplicateURI 'com.example:billing:invoice:final'>
InvoicesType[InvoiceURI.new(id: 'pro_forma')] # => Dry::Types::ConstraintError

Extensions

Extensions add additional functionality to URIs. They can be used by requiring them before building the URI classes.

require 'studitemps/utils/uri/extensions/serialization'

MyBaseURI = Studitemps::Utils::URI.build(schema: 'com.example')
uri = MyBaseURI.new(context: 'billing', resource: 'invoice', id: 'R422342')

MyBaseURI.dump(uri) # => 'com.example:billing:invoice:R422342'
MyBaseURI.load('com.example:billing:invoice:R422342') # =>  #<MyBaseURI 'com.example:billing:invoice:R422342'>

Available Extensions

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/STUDITEMPS/utils.

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.