hyperion-api 0.0.1.alpha1 → 0.0.1.alpha2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ require 'base64'
2
+ require 'uuidtools'
3
+
4
+ module Hyperion
5
+ class Key
6
+ class << self
7
+
8
+ def encode_key(value)
9
+ normalize(Base64.encode64(value))
10
+ end
11
+
12
+ def decode_key(value)
13
+ Base64.decode64(denormalize(value))
14
+ end
15
+
16
+ def compose_key(kind, id=nil)
17
+ _id = id.nil? || id.to_s.strip == "" ? generate_id : id.to_s
18
+ encode_key("#{encode_key(kind.to_s)}:#{encode_key(_id)}")
19
+ end
20
+
21
+ def decompose_key(key)
22
+ decode_key(key).split(/:/).map {|part| decode_key(part)}
23
+ end
24
+
25
+ private
26
+
27
+ def generate_id
28
+ UUIDTools::UUID.random_create.to_s.gsub(/-/, '')
29
+ end
30
+
31
+ def normalize(value)
32
+ value.gsub(/=/, '').chomp
33
+ end
34
+
35
+ def denormalize(value)
36
+ case value.length % 4
37
+ when 3
38
+ "#{value}="
39
+ when 2
40
+ "#{value}=="
41
+ when 1
42
+ "#{value}==="
43
+ else
44
+ value
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ require 'hyperion/key'
2
+
3
+ describe Hyperion::Key do
4
+
5
+ def encode_key(value)
6
+ Hyperion::Key.encode_key(value)
7
+ end
8
+
9
+ def decode_key(value)
10
+ Hyperion::Key.decode_key(value)
11
+ end
12
+
13
+ specify 'encoding' do
14
+ encode_key('a').should == 'YQ'
15
+ encode_key('ab').should == 'YWI'
16
+ encode_key('abc').should == 'YWJj'
17
+ encode_key('abcd').should == 'YWJjZA'
18
+ encode_key('abcde').should == 'YWJjZGU'
19
+ encode_key('abcdef').should == 'YWJjZGVm'
20
+ end
21
+
22
+ specify 'decoding' do
23
+ decode_key('YQ').should == 'a'
24
+ decode_key('YWI').should == 'ab'
25
+ decode_key('YWJj').should == 'abc'
26
+ decode_key('YWJjZA').should == 'abcd'
27
+ decode_key('YWJjZGU').should == 'abcde'
28
+ decode_key('YWJjZGVm').should == 'abcdef'
29
+ end
30
+
31
+ it 'composes unique keys' do
32
+ keys = (0...100).map {Hyperion::Key.compose_key('foo')}
33
+ Set.new(keys).length.should == 100
34
+ keys = (0...100).map {Hyperion::Key.compose_key(:foo)}
35
+ Set.new(keys).length.should == 100
36
+ Hyperion::Key.compose_key(:foo, 1).should == Hyperion::Key.compose_key(:foo, 1)
37
+ end
38
+
39
+ it 'decomposes keys' do
40
+ key = Hyperion::Key.compose_key(:thing, 1)
41
+ Hyperion::Key.decompose_key(key).should == ['thing', '1']
42
+ end
43
+
44
+ it 'decomposes keys with a :' do
45
+ key = Hyperion::Key.compose_key(:thing, 'my:key')
46
+ Hyperion::Key.decompose_key(key).should == ['thing', 'my:key']
47
+ end
48
+
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha1
4
+ version: 0.0.1.alpha2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 2.11.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: uuidtools
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.1.3
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.1.3
30
46
  description: A Generic Persistence API for Ruby
31
47
  email:
32
48
  - myles@8thlight.com
@@ -38,9 +54,11 @@ files:
38
54
  - lib/hyperion/dev/ds_spec.rb
39
55
  - lib/hyperion/filter.rb
40
56
  - lib/hyperion/api.rb
57
+ - lib/hyperion/key.rb
41
58
  - lib/hyperion/sort.rb
42
59
  - lib/hyperion/query.rb
43
60
  - spec/hyperion/dev/memory_spec.rb
61
+ - spec/hyperion/key_spec.rb
44
62
  - spec/hyperion/shared_examples.rb
45
63
  - spec/hyperion/api_spec.rb
46
64
  - spec/hyperion/fake_ds.rb
@@ -71,6 +89,7 @@ specification_version: 3
71
89
  summary: A Generic Persistence API for Ruby
72
90
  test_files:
73
91
  - spec/hyperion/dev/memory_spec.rb
92
+ - spec/hyperion/key_spec.rb
74
93
  - spec/hyperion/shared_examples.rb
75
94
  - spec/hyperion/api_spec.rb
76
95
  - spec/hyperion/fake_ds.rb