Skip to content

Commit

Permalink
Update gemspec, add final @q methods, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-levan committed Nov 22, 2024
1 parent 0dcae7f commit d2cd767
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
69 changes: 55 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,76 @@
# Aura::Rb
# aura-rb

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/aura/rb`. To experiment with that code, run `bin/console` for an interactive prompt.
Parsing, conversion, and validation for Urbit date, phonetic name, and number
auras.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_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_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
$ bundle add urbit-aura

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

$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
$ gem install urbit-aura

## Usage

TODO: Write usage instructions here
```ruby
require 'aura'

Aura.version # => "0.1.0"

# @p
Aura::P.patp(0) # => "~zod"
Aura::P.patp("0") # => "~zod"
Aura::P.hex2patp("0x12345678") # => "~milbyt-wacmeg"
Aura::P.patp2hex("~zod") # => "00"
Aura::P.patp2dec("~zod") # => 0
Aura::P.clan("~zod") # => "galaxy"
Aura::P.clan("~mastyr-bottec") # => "planet"
Aura::P.sein("~mastyr-bottec") # => "~wanzod"
Aura::P.valid_pat?("~zod") # => true
Aura::P.valid_patp?("invalid") # => false
Aura::P.pre_sig("zod") # => "~zod"
Aura::P.de_sig("~zod") # => "zod"
Aura::P.cite("~mastyr-bottec") # => "~mastyr-bottec"

# @q
Aura::Q.patq(123456) # => "~doznec-fitrys"
Aura::Q.patq("123456") # => "~doznec-fitrys"
Aura::Q.hex2patq("1e240") # => "~doznec-fitrys"
Aura::Q.hex2patq("0x1e240") # => "~doznec-fitrys"
Aura::Q.patq2hex("~doznec-fitrys") # => "0001e240"
Aura::Q.patq2dec("~doznec-fitrys") # => 123456
Aura::Q.valid_patq?("~doznec-fitrys") # => true
Aura::Q.valid_patq?("invalid") # => false
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
TODO:

- [ ] Tests
- [ ] Dates (`@da`)
- [ ] More numbers (`@uv`, `@ud`, etc...)

After checking out the repo, run `bin/setup` to install dependencies. Then, run
`rake test` 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).
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]/aura-rb.
Bug reports and pull requests are welcome on GitHub at
https://github.com/urbit/aura-rb.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
The gem is available as open source under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion aura-rb.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "lib/aura/version"
require_relative "lib/aura"

Gem::Specification.new do |spec|
spec.name = "urbit-aura"
Expand Down
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require "bundler/setup"
require "aura/rb"
require "aura"

# 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.
Expand Down
39 changes: 34 additions & 5 deletions lib/aura.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require("ob")
require_relative("ob")

# Functionality for wrangling Urbit `@da`, `@p`, `@q`, `@ux`, etc.
module Aura
Expand Down Expand Up @@ -70,7 +70,7 @@ def patp2syls(name)
name.gsub(/[\^~-]/, "").scan(/.{1,3}/)
end

def self.valid_pat?(name)
def valid_pat?(name)
raise ArgumentError, "valid_pat?: non-string input" unless name.is_a? String

leading_tilde = name.start_with?("~")
Expand Down Expand Up @@ -233,7 +233,8 @@ module Q
# Convert a number to a @q-encoded string.
def self.patq(arg)
n = arg.to_i
buf = n.to_s(16).scan(/../).map(&:hex)
hex = n.to_s(16)
buf = hex.rjust((hex.length + 1) & ~1, "0").scan(/../).map(&:hex)
buf2patq(buf)
end

Expand Down Expand Up @@ -271,7 +272,7 @@ def self.patq2dec(name)
#
# Note that this preservers leading zero bytes.
def self.patq2hex(name)
raise ArgumentError, "patq2hex: not a valid @q" unless P.valid_pat?(name)
raise ArgumentError, "patq2hex: not a valid @q" unless valid_pat?(name)

chunks = name.delete_prefix("~").split("-")
dec2hex = ->(dec) { format("%02x", dec) }
Expand All @@ -288,10 +289,38 @@ def self.patq2hex(name)
end

# Validate a @q-encoded string.
# TODO: FINISH ME
def self.valid_patq?(str)
valid_pat?(str) && eq_patq(str, patq(patq2dec(str)))
end

# Validate @q-encoded string equality.
def self.eq_patq(p, q)
# Convert p to hex, raise an exception if not valid
phex = begin
Aura::Q.patq2hex(p)
rescue ArgumentError
raise ArgumentError, "eq_patq: not a valid @q"
end

# Convert q to hex, raise an exception if not valid
qhex = begin
Aura::Q.patq2hex(q)
rescue ArgumentError
raise ArgumentError, "eq_patq: not a valid @q"
end

# Assuming eq_mod_leading_zero_bytes is defined elsewhere in your Ruby code
eq_mod_leading_zero_bytes(phex, qhex)
end

# Helper method to compare hex strings ignoring leading zero bytes
def self.eq_mod_leading_zero_bytes(hex1, hex2)
# Remove any leading zeros and compare
hex1.gsub(/^0+/, "") == hex2.gsub(/^0+/, "")
end

module_function

def prefix_name(byts)
byts[1].nil? ? PREFIXES[0] + SUFFIXES[byts[0]] : PREFIXES[byts[0]] + SUFFIXES[byts[1]]
end
Expand Down

0 comments on commit d2cd767

Please sign in to comment.