propertybase_id 0.2.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
+ SHA1:
3
+ metadata.gz: 8643adb6124aba399189a527653bdcd28280a778
4
+ data.tar.gz: 76bd327742cfb04ee9e29befc306b91425b32347
5
+ SHA512:
6
+ metadata.gz: 5298e1e086d89822a60397eb8c17b048e75d8ba8a9e9be46b44768105b01cefb0a8d07abb899056e3447be82b0f621472cedd1e6a6731f2cac07e878a4d4c26d
7
+ data.tar.gz: 874bdfc5ca289a7ea4c253ad9bc88ec1eab948b5e65f556d42e9fa307dadaade091a6cb00cf790b5b29cc2d8bb4607e29a02996a8779d5727afe98c1dcb4c9f1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+ cache: bundler
3
+ bundler_args: --without documentation production
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.5
7
+ - 2.2.0
8
+ - jruby-19mode
9
+ - rbx
10
+ - ruby-head
11
+ - jruby-head
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: ruby-head
15
+ - rvm: jruby-head
16
+ fast_finish: true
17
+ script:
18
+ - bundle exec rspec
19
+ before_install: gem update bundler
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers 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. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem"s dependencies in propertybase_id.gemspec
4
+ gemspec
5
+
6
+ gem "guard"
7
+ gem "guard-rspec"
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+
11
+ # Ruby files
12
+ ruby = dsl.ruby
13
+ dsl.watch_spec_files_for(ruby.lib_files)
14
+
15
+ # Standard RubyGem Project
16
+ watch(/^spec\/.+_spec\.rb/)
17
+ watch(/^lib\/(.+)\.rb/) { |m| "spec/#{m[1]}_spec.rb" }
18
+ watch("spec/spec_helper.rb") { "spec" }
19
+ end
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # PropertybaseId
2
+
3
+ This gem can be utilized to create globally uniqu Propertybase IDs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application"s Gemfile:
8
+
9
+ ```ruby
10
+ gem "propertybase_id"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install propertybase_id
20
+
21
+ ## Usage
22
+
23
+ ### Create
24
+
25
+ id = PropertybaseId.create(objectt: "team", server: 1)
26
+ => #<PropertybaseId:0x007fe855071a58 @_object_id=2, @local_random=738414242805187245, @object="team", @server=1>
27
+
28
+ For string representation do:
29
+
30
+ id.to_s
31
+ => "02015lyq044bfuel"
32
+
33
+ ### Parsing
34
+
35
+ To get a PropertybaseId object from a string representation do:
36
+
37
+ PropertybaseId.parse("02015lyq044bfuel")
38
+ => #<PropertybaseId:0x007f9943cb48a8 @_object_id=2, @local_random=738414242805187245, @object="team", @server=1>
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it ( https://github.com/[my-github-username]/propertybase_id/fork )
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am "Add some feature"`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "propertybase_id"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don"t forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ {
2
+ "user": 1,
3
+ "team": 2
4
+ }
@@ -0,0 +1,49 @@
1
+ require "digest/sha1"
2
+
3
+ class PropertybaseId
4
+ class Generator
5
+ def initialize
6
+ @counter = 0
7
+ @mutex = Mutex.new
8
+ end
9
+
10
+ def generate(object: )
11
+ @mutex.lock
12
+ begin
13
+ count = next_counter
14
+ ensure
15
+ @mutex.unlock rescue nil
16
+ end
17
+
18
+ PropertybaseId.new(
19
+ object: object,
20
+ host_id: host_id,
21
+ time: time,
22
+ process_id: process_id,
23
+ counter: count
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ def time
30
+ @_time ||= ::Time.now.to_i
31
+ end
32
+
33
+ def host_id
34
+ @_host_id ||= Digest::SHA1.hexdigest(Socket.gethostname).to_i(16) % max_value(2)
35
+ end
36
+
37
+ def next_counter
38
+ @counter = (@counter + 1) % max_value(3)
39
+ end
40
+
41
+ def process_id
42
+ "#{Process.pid}#{Thread.current.object_id}".hash % max_value(2)
43
+ end
44
+
45
+ def max_value(digits)
46
+ ("z" * digits).to_i(36) + 1
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ class PropertybaseId
2
+ class LocalRandom
3
+ MAX_INTEGER = 10000
4
+ TIME_MULTIPLIER = 10000
5
+
6
+ attr_reader :time
7
+
8
+ def initialize(time=Time.now)
9
+ @time = time
10
+ end
11
+
12
+ def to_i
13
+ @_integer ||= begin
14
+ rand_int = SecureRandom.random_number(MAX_INTEGER)
15
+ max_digits = (MAX_INTEGER - 1).to_s.size
16
+ prefix = "%0#{max_digits}d" % rand_int
17
+ time_with_miliseconds = (time.to_f * TIME_MULTIPLIER).to_i
18
+
19
+ "#{prefix}#{time_with_miliseconds}".to_i
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require "json"
2
+
3
+ class PropertybaseId
4
+ module Mappings
5
+ extend self
6
+
7
+ def objects
8
+ @_objects ||= begin
9
+ load_json_file("objects")
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def load_json_file(name)
16
+ JSON.parse(File.read(File.expand_path('../data/objects.json', __FILE__)))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ class PropertybaseId
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,91 @@
1
+ require "propertybase_id/version"
2
+ require "propertybase_id/local_random"
3
+ require "propertybase_id/generator"
4
+ require "propertybase_id/mappings"
5
+
6
+ class PropertybaseId
7
+ attr_reader :object
8
+ attr_reader :host_id
9
+ attr_reader :time
10
+ attr_reader :process_id
11
+ attr_reader :counter
12
+
13
+ def initialize(object:, host_id:, time:, process_id:, counter:)
14
+ @object = object && object.dup.freeze
15
+ @host_id = host_id
16
+ @time = time
17
+ @process_id = process_id
18
+ @counter = counter
19
+ validate!
20
+ end
21
+
22
+ def to_s
23
+ @_string ||= begin
24
+ object_str = format_number(@object_id, 3)
25
+ host_str = format_number(@host_id, 2)
26
+ time_str = format_number(@time, 6)
27
+ process_str = format_number(@process_id, 2)
28
+ counter_str = format_number(@counter, 3)
29
+
30
+ "#{object_str}#{host_str}#{time_str}#{process_str}#{counter_str}"
31
+ end
32
+ end
33
+
34
+ def ==(o)
35
+ self.class == o.class &&
36
+ self.object == o.object &&
37
+ self.host_id == o.host_id &&
38
+ self.process_id == o.process_id &&
39
+ self.time == o.time &&
40
+ self.counter == o.counter
41
+ end
42
+
43
+ alias_method :eql?, :==
44
+
45
+ def hash
46
+ [
47
+ @object_id,
48
+ @host_id,
49
+ @time,
50
+ @process_id,
51
+ @counter,
52
+ ].hash
53
+ end
54
+
55
+ def self.generate(object:)
56
+ @_generator ||= begin
57
+ PropertybaseId::Generator.new
58
+ end
59
+ @_generator.generate(object: object)
60
+ end
61
+
62
+ def self.parse(input_id)
63
+ raise ArgumentError, "invalid length (#{input_id.size})" if input_id.size != 16
64
+
65
+ _, object_id, host_id, time, process_id, counter = input_id.match(/(\w{3})(\w{2})(\w{6})(\w{2})(\w{3})/).to_a
66
+
67
+ object, _ = PropertybaseId::Mappings.objects.select{|_, v| v == object_id.to_i(36) }.first
68
+
69
+ raise ArgumentError, "No object to id #{object_id}" if object.nil?
70
+
71
+ new(
72
+ object: object,
73
+ host_id: host_id.to_i(36),
74
+ time: time.to_i(36),
75
+ process_id: process_id.to_i(36),
76
+ counter: counter.to_i(36),
77
+ )
78
+ end
79
+
80
+ private
81
+
82
+ def validate!
83
+ @object_id ||= PropertybaseId::Mappings.objects.fetch(object) do
84
+ raise ArgumentError, "Object #{object.inspect} not found"
85
+ end
86
+ end
87
+
88
+ def format_number(integer, length)
89
+ integer.to_s(36).rjust(length, "0")
90
+ end
91
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "propertybase_id/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "propertybase_id"
8
+ spec.version = PropertybaseId::VERSION
9
+ spec.authors = ["Leif Gensert"]
10
+ spec.email = ["leif@propertybase.com"]
11
+
12
+ spec.summary = %q{Gem for creating Propertybase compatible IDs}
13
+ spec.description = %q{This gem lets you create IDs in the Propertybase universe. Still under heavy development}
14
+ spec.homepage = "http://www.propertybase.com"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry"
24
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: propertybase_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Leif Gensert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This gem lets you create IDs in the Propertybase universe. Still under
56
+ heavy development
57
+ email:
58
+ - leif@propertybase.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Guardfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/propertybase_id.rb
74
+ - lib/propertybase_id/data/objects.json
75
+ - lib/propertybase_id/generator.rb
76
+ - lib/propertybase_id/local_random.rb
77
+ - lib/propertybase_id/mappings.rb
78
+ - lib/propertybase_id/version.rb
79
+ - propertybase_id.gemspec
80
+ homepage: http://www.propertybase.com
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Gem for creating Propertybase compatible IDs
103
+ test_files: []