youyouaidi 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0d8a7918c5ccc52c7b51c259aef312e9c0c4b74
4
- data.tar.gz: 4e280e52b10d17e1adebe93986e353078d90de18
3
+ metadata.gz: 1348322fab3186ecf944dd41ca64cae5316b4648
4
+ data.tar.gz: f469604e1089be795b4d49bb533af942be2af1d2
5
5
  SHA512:
6
- metadata.gz: 31b9dbba81a25d1923a44ca05b731d4b7762c3fbde742f36259269d781e10e45d9911042d385891f828fae2469f09d8913d0dfd74022b05d67f12a742d77950f
7
- data.tar.gz: e162eb97d5f4609ebd91389ba0907134a0e5f1761764139730f9cfcedb4562561ea14edc6b2dc895a7d1a0a584d43b3fec32837d55b63e803ebce72c2f8471cd
6
+ metadata.gz: 577327867e66034cf00973254f133b173c8799545916bb4403e7df4adcaf96c7f0f68570ebe620ef7b852c0110598857e667266b8cf5264ff4246ca3625065de
7
+ data.tar.gz: 90418860a463cba9a14a5cfad83ae40fed522ca6fce33ecdd611c2bfbfc7f8c2e6ce1a06f6544426d4dcba3d0f0f41346270afee0b8369e03de3581efddb1058
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Youyouaidi
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/youyouaidi.svg)](https://rubygems.org/gems/youyouaidi)
4
+ [![Build Status](https://travis-ci.org/nicolas-fricke/youyouaidi.svg)](https://travis-ci.org/nicolas-fricke/youyouaidi)
5
+
6
+
3
7
  Ruby Gem `Youyouaidi` offers a UUID class for parsing, validating and converting UUIDs into / from shorter representations.
4
8
 
5
9
  ## Installation
@@ -22,9 +26,9 @@ Or install it yourself as:
22
26
  uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
23
27
  uuid_short = '_oGOAbD9fsFFEHWSMal1v' # Same UUID in its short format
24
28
 
25
- Youyouaidi::UUID.valid_uuid? uuid_string # => true
29
+ Youyouaidi::UUID.valid? uuid_string # => true
26
30
 
27
- uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.new uuid_string into kernel.
31
+ uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.parse uuid_string into kernel.
28
32
  # => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
29
33
  # or alternatively a short UUID can be passed
30
34
  uuid = UUID uuid_short # creates similar Youyouaidi::UUID object
data/lib/kernel_patch.rb CHANGED
@@ -2,6 +2,6 @@ require 'youyouaidi'
2
2
 
3
3
  module Kernel
4
4
  def UUID(uuid_param)
5
- Youyouaidi::UUID.new uuid_param
5
+ Youyouaidi::UUID.parse uuid_param
6
6
  end
7
7
  end
data/lib/youyouaidi.rb CHANGED
@@ -4,5 +4,6 @@ require 'kernel_patch'
4
4
  module Youyouaidi
5
5
  # Your code goes here...
6
6
  require 'youyouaidi/uuid'
7
+ require 'youyouaidi/invalid_uuid'
7
8
  require 'youyouaidi/converter'
8
9
  end
@@ -7,14 +7,14 @@ class Youyouaidi::Converter
7
7
  end
8
8
 
9
9
  def decode(encoded_uuid)
10
- base_decode encoded_uuid
10
+ Youyouaidi::UUID.new convert_bignum_to_uuid_string base_decode encoded_uuid
11
11
  end
12
12
 
13
13
  private
14
14
  BASE = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a + %w(_ - +)
15
15
 
16
16
  def base_encode(numeric)
17
- raise ArgumentError, "`#{numeric}' needs to be a Numeric" unless numeric.is_a? Numeric
17
+ raise Youyouaidi::InvalidUUIDError.new "`#{numeric}' needs to be a Numeric" unless numeric.is_a? Numeric
18
18
 
19
19
  return '0' if numeric == 0
20
20
  s = ''
@@ -34,10 +34,15 @@ class Youyouaidi::Converter
34
34
  if ord = BASE.index(char)
35
35
  total += ord * (BASE.size ** index)
36
36
  else
37
- raise ArgumentError, "`#{encoded_numeric}' has `#{char}' which is not valid"
37
+ raise Youyouaidi::InvalidUUIDError.new "`#{encoded_numeric}' has `#{char}' which is not valid"
38
38
  end
39
39
  end
40
40
  total
41
41
  end
42
+
43
+ def convert_bignum_to_uuid_string(short_uuid_string)
44
+ uuid = short_uuid_string.to_i.to_s(16)
45
+ "#{uuid[0,8]}-#{uuid[8,4]}-#{uuid[12,4]}-#{uuid[16,4]}-#{uuid[20,12]}"
46
+ end
42
47
  end
43
- end
48
+ end
@@ -0,0 +1,2 @@
1
+ class Youyouaidi::InvalidUUIDError < TypeError
2
+ end
@@ -1,14 +1,10 @@
1
1
  class Youyouaidi::UUID
2
2
  attr_reader :uuid
3
3
 
4
- def initialize(uuid_param, options = {})
4
+ def initialize(uuid_string, options = {})
5
5
  @converter = options[:converter] || Youyouaidi::Converter
6
- if self.class.valid_uuid? uuid_param
7
- @uuid = uuid_param.to_s
8
- else
9
- @uuid = decode_short_string uuid_param
10
- raise ArgumentError.new "`#{uuid_param}' could not be converted to valid UUID" unless self.class.valid_uuid? @uuid
11
- end
6
+ raise Youyouaidi::InvalidUUIDError.new "`#{uuid_string}' could not be converted to valid UUID" unless self.class.valid? uuid_string
7
+ @uuid = uuid_string.to_s
12
8
  end
13
9
 
14
10
  def to_i
@@ -25,14 +21,21 @@ class Youyouaidi::UUID
25
21
  alias_method :to_param, :to_short_string
26
22
 
27
23
  private
28
- def decode_short_string(short_uuid_string)
29
- uuid = @converter.decode(short_uuid_string).to_i.to_s(16)
30
- "#{uuid[0,8]}-#{uuid[8,4]}-#{uuid[12,4]}-#{uuid[16,4]}-#{uuid[20,12]}"
31
- end
32
24
 
33
25
  class << self
34
- def valid_uuid?(uuid_canidate)
35
- (uuid_canidate.to_s =~ /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i) == 0
26
+ def parse(uuid_param, options = {})
27
+ @converter = options[:converter] || Youyouaidi::Converter
28
+ if valid? uuid_param
29
+ self.new uuid_param.to_s, options
30
+ else
31
+ uuid_obj = @converter.decode uuid_param
32
+ raise Youyouaidi::InvalidUUIDError.new "`#{uuid_param}' could not be converted to valid UUID" unless valid? uuid_obj.to_s
33
+ uuid_obj
34
+ end
35
+ end
36
+
37
+ def valid?(uuid_canidate)
38
+ (uuid_canidate.to_s =~ /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i) == 0
36
39
  end
37
40
  end
38
- end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module Youyouaidi
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -27,7 +27,7 @@ describe Kernel do
27
27
  context 'initialized with invalid param' do
28
28
  let(:param) { 'KEKSE' }
29
29
  subject { -> { action } }
30
- it { should raise_error ArgumentError }
30
+ it { should raise_error Youyouaidi::InvalidUUIDError }
31
31
  end
32
32
  end
33
- end
33
+ end
@@ -1,18 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Youyouaidi::Converter do
4
+ let(:uuid) { Youyouaidi::UUID.new uuid_string }
4
5
  let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
5
- let(:uuid_bignum) { 113059749145936325402354257176981405696 }
6
6
  let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
7
7
 
8
8
  describe 'use case' do
9
- subject { described_class.decode described_class.encode(uuid_bignum) }
9
+ subject { described_class.decode described_class.encode(uuid) }
10
10
 
11
- it { should eq uuid_bignum }
11
+ it { should be_a Youyouaidi::UUID }
12
+ its(:to_s) { should eq uuid_string }
12
13
  end
13
14
 
14
15
  describe 'methods' do
15
- let(:uuid) { Youyouaidi::UUID.new uuid_string }
16
16
 
17
17
  describe '.encode' do
18
18
  subject { described_class.encode uuid }
@@ -23,7 +23,16 @@ describe Youyouaidi::Converter do
23
23
  describe '.decode' do
24
24
  subject { described_class.decode encoded_uuid }
25
25
 
26
- it { should eq uuid_bignum }
26
+ it { should be_a Youyouaidi::UUID }
27
+ its(:to_s) { should eq uuid_string }
28
+
29
+ context 'with invalid characters' do
30
+ let(:encoded_uuid) { ' ' }
31
+
32
+ it 'raises error' do
33
+ expect { subject }.to raise_error Youyouaidi::InvalidUUIDError
34
+ end
35
+ end
27
36
  end
28
37
  end
29
- end
38
+ end
@@ -4,7 +4,36 @@ describe Youyouaidi do
4
4
  describe Youyouaidi::UUID do
5
5
  describe '.new' do
6
6
  let(:param) { '' }
7
- subject { Youyouaidi::UUID.new param }
7
+ let(:action) { Youyouaidi::UUID.new param }
8
+ subject { -> { action } }
9
+
10
+ context 'with valid uuid string' do
11
+ let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
12
+ subject { action }
13
+
14
+ it { should be_a Youyouaidi::UUID }
15
+ end
16
+
17
+ context 'with uuid in short format' do
18
+ let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
19
+ it { should raise_error Youyouaidi::InvalidUUIDError }
20
+ end
21
+
22
+ context 'with invalid uuid string' do
23
+ let(:param) { 'Kekse' }
24
+ it { should raise_error Youyouaidi::InvalidUUIDError }
25
+ end
26
+
27
+ context 'with non-uuid-string input' do
28
+ let(:param) { 1234 }
29
+ it { should raise_error Youyouaidi::InvalidUUIDError }
30
+ end
31
+ end
32
+
33
+ describe '.parse' do
34
+ let(:param) { '' }
35
+ let(:action) { Youyouaidi::UUID.parse param }
36
+ subject { action }
8
37
 
9
38
  context 'with valid uuid string' do
10
39
  let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
@@ -24,7 +53,7 @@ describe Youyouaidi do
24
53
  let(:param) { 'Kekse' }
25
54
 
26
55
  it 'raises error' do
27
- expect { Youyouaidi::UUID.new param }.to raise_error ArgumentError
56
+ expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
28
57
  end
29
58
  end
30
59
 
@@ -32,7 +61,7 @@ describe Youyouaidi do
32
61
  let(:param) { 1234 }
33
62
 
34
63
  it 'raises error' do
35
- expect { Youyouaidi::UUID.new param }.to raise_error ArgumentError
64
+ expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
36
65
  end
37
66
  end
38
67
  end
@@ -75,9 +104,9 @@ describe Youyouaidi do
75
104
  it_behaves_like 'method for short format'
76
105
  end
77
106
 
78
- describe '.valid_uuid?' do
107
+ describe '.valid?' do
79
108
  let(:param) { '' }
80
- subject { Youyouaidi::UUID.valid_uuid? param }
109
+ subject { Youyouaidi::UUID.valid? param }
81
110
 
82
111
  context 'with valid uuid' do
83
112
  let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
@@ -86,14 +115,17 @@ describe Youyouaidi do
86
115
  end
87
116
 
88
117
  context 'with invalid uuid' do
89
- invalid_uuids = ['Kekse', 'aa550e8400-e29b-41d4-a716-446655440000']
118
+ uuid_string = '550e8400-e29b-41d4-a716-446655440000'
119
+ encoded_uuid = '_oGOAbD9fsFFEHWSMal1v'
120
+ invalid_uuids = ['Kekse', "aa#{uuid_string}", "#{uuid_string}bb",
121
+ "#{encoded_uuid}" ]
90
122
 
91
123
  invalid_uuids.each do |invalid_uuid|
92
124
  it "should return false for `#{invalid_uuid}`" do
93
- expect(Youyouaidi::UUID.valid_uuid? invalid_uuid).to eq false
125
+ expect(Youyouaidi::UUID.valid? invalid_uuid).to eq false
94
126
  end
95
127
  end
96
128
  end
97
129
  end
98
130
  end
99
- end
131
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youyouaidi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Fricke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-06 00:00:00.000000000 Z
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,7 @@ files:
55
55
  - lib/kernel_patch.rb
56
56
  - lib/youyouaidi.rb
57
57
  - lib/youyouaidi/converter.rb
58
+ - lib/youyouaidi/invalid_uuid.rb
58
59
  - lib/youyouaidi/uuid.rb
59
60
  - lib/youyouaidi/version.rb
60
61
  - spec/kernel_patch_spec.rb