organic_hash 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ require 'digest/sha1'
2
+ require 'securerandom'
3
+
4
+ class OrganicHash
5
+
6
+ VERSION = "1.0.0"
7
+ DATA_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data'))
8
+ NOUN = File.read(File.join(DATA_DIR, "noun.dat")).split("\n")
9
+ ADJ = File.read(File.join(DATA_DIR, "adj.dat")).split("\n")
10
+ ADV = File.read(File.join(DATA_DIR, "adv.dat")).split("\n")
11
+
12
+ def initialize(length = 3, delimiter = "-", hasher = Digest::SHA1, noun = NOUN, adj = ADJ, adv = ADV)
13
+ @length = [1, [5, length].min].max
14
+ @delimiter = delimiter
15
+ @hasher = hasher
16
+ @noun = noun
17
+ @adj = adj
18
+ @adv = adv
19
+ end
20
+
21
+ def hash(str, array = false)
22
+ indexes = str2indexes str
23
+
24
+ noun_idx = indexes.pop
25
+ adjs, advs = indexes.partition.with_index { |_, i| i.even? }
26
+ adjs = adjs.map { |i| @adj[i % @adj.length] }
27
+ advs = advs.map { |i| @adv[i % @adv.length] }
28
+
29
+ output = adjs.zip(advs).map(&:reverse).flatten.reject(&:nil?) + [@noun[noun_idx % @noun.length]]
30
+
31
+ array ? output : output.join(@delimiter)
32
+ end
33
+
34
+ def rand(array = false)
35
+ hash SecureRandom.hex, array
36
+ end
37
+
38
+ def self.hash(str, array = false)
39
+ OrganicHash.new.hash str, array
40
+ end
41
+
42
+ def self.rand(array = false)
43
+ OrganicHash.new.rand array
44
+ end
45
+
46
+ private
47
+ def str2indexes(hex)
48
+ sha1 = @hasher.hexdigest hex
49
+ seg = sha1.length / @length
50
+ rem = sha1.length % @length
51
+
52
+ 0.upto(@length - 1).map { |i| sha1[seg*i...seg*(i+1)].to_i(16) }
53
+ end
54
+
55
+ end
56
+
@@ -0,0 +1,26 @@
1
+ require './lib/organic_hash'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "organic_hash"
5
+ s.version = OrganicHash::VERSION
6
+
7
+ s.authors = ["Joseph Chee Chang", "Zero Cho"]
8
+ s.email = ["josephcc.cmu@gmail.com", "itszero@gmail.com"]
9
+ s.license = 'Apache License, Version 2.0'
10
+
11
+ s.description = "Organic Hash hashes strings (user ID, hashes) to a human-readable, scifi-themed representation."
12
+ s.summary = "Converts strings to awesome scifi objects!!"
13
+ s.homepage = "https://github.com/josephcc/organic_hash"
14
+
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+
19
+ s.files = `git ls-files`.split($/).reject { |path|
20
+ (path.start_with? "data/") or (path.start_with? "parser/")
21
+ } + Dir.glob("data/*.dat")
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_development_dependency "bundler", "~> 1.3"
25
+ s.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: organic_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joseph Chee Chang
9
+ - Zero Cho
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-01-31 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Organic Hash hashes strings (user ID, hashes) to a human-readable, scifi-themed
48
+ representation.
49
+ email:
50
+ - josephcc.cmu@gmail.com
51
+ - itszero@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files:
55
+ - README.md
56
+ files:
57
+ - .gitignore
58
+ - Gemfile
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - lib/organic_hash.rb
63
+ - organic_hash.gemspec
64
+ - data/adj.dat
65
+ - data/adv.dat
66
+ - data/noun.dat
67
+ homepage: https://github.com/josephcc/organic_hash
68
+ licenses:
69
+ - Apache License, Version 2.0
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.23
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Converts strings to awesome scifi objects!!
92
+ test_files: []