trafaret 1.5.6 → 1.5.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a287cdfb38aa84321f68a1989564a6491277ea01
4
- data.tar.gz: 66fe38066bd25cc4ebfd96136b93d7199a4200ec
3
+ metadata.gz: 6dfda67413190ab691868072c86fe3fe6c58c8e4
4
+ data.tar.gz: 3a372f85b3aca6b1229a692ff473645b61dc51ee
5
5
  SHA512:
6
- metadata.gz: ff3884c959180136768c903dd9a15051054596af68324e4086862a9527ee90116115898c08559f1bb1642a8fc9a7f0924477dc2c6c99553d548af7b1169fb338
7
- data.tar.gz: 777a037a908f5da2d94c5663be10acd750440016a4554ef43b2bc5f1e8a3a0c56180229911aa1172a613aa56b4a48a397c0583ff437881a60320804526eb1464
6
+ metadata.gz: a6b3348f3a319282df5b87e6daa52bb79e882fcd27935f6b459a31b5e1259c7b622285ba23f1f41eb483c99c6f39cb853e46cfcf48cc2d95077e931a1fdcebca
7
+ data.tar.gz: 9c3f3730bd78f27eeecb71932ec3eb82d2ebd4f2f9f9bbd43674c01f5d63d861fd22d8ce3e2f5711c2b896badf54d29f3239ae15641ca7fd3b56dced6f0431a2
data/README.rst CHANGED
@@ -60,6 +60,23 @@ If you use custom converter block, you will get `Match` instead of `String`, so
60
60
 
61
61
  T.string(regex: /\Ayear=(\d+),month=(\d+),day=(\d+)\z/).to {|m| Date.new(*m.to_a[1..3].map(&:to_i)) }.call('year=2012,month=5,day=4').to_s == '2012-05-04'
62
62
 
63
+ URI
64
+ ---
65
+
66
+ URI parses URI. Parameter `schemes`, by default == ['http', 'https']::
67
+
68
+ t = T.uri(schemes: ['ftp'])
69
+ t.call('ftp://ftp.ueaysuc.co.uk.edu') == 'ftp://ftp.ueaysuc.co.uk.edu'
70
+
71
+ Possible Errors - 'Invalid scheme', 'Invalid URI'.
72
+
73
+ Mail
74
+ ----
75
+
76
+ Now just checks simple regexp::
77
+
78
+ T.email('kuku@example.com').to { |m| m[:name] } == 'kuku'
79
+
63
80
  Array
64
81
  -----
65
82
 
@@ -98,4 +115,4 @@ Example::
98
115
 
99
116
  t = T.tuple(:integer, :string, :nil)
100
117
  t.call([1, 'a', nil]) == [1, 'a', nil]
101
- t.call([1, 'a', 3]).dump == {2 => 'Value must be nil'} # Error dumped to pure structures
118
+ t.call([1, 'a', 3]).dump == {2 => 'Value must be nil'} # Error dumped to pure structures
@@ -4,6 +4,7 @@ require 'trafaret/errors'
4
4
  require 'trafaret/validator'
5
5
  require 'trafaret/validators'
6
6
  require 'trafaret/numeric'
7
+ require 'trafaret/uri_email'
7
8
  require 'trafaret/base'
8
9
  require 'trafaret/constructor'
9
10
 
@@ -33,8 +33,11 @@ module Trafaret
33
33
  end
34
34
 
35
35
  def from_array(params)
36
- raise 'Wrong argument for array construction' if params.size != 1
37
- Trafaret::Array.new validator: Trafaret::Constructor.construct_from(params[0])
36
+ if params.size == 1
37
+ Trafaret::Array.new validator: Trafaret::Constructor.construct_from(params[0])
38
+ else params.size > 1
39
+ Trafaret::Tuple.new(*(params.map { |p| Trafaret::Constructor.construct_from(p) }))
40
+ end
38
41
  end
39
42
  end
40
43
  end
@@ -0,0 +1,38 @@
1
+ require 'uri'
2
+
3
+ module Trafaret
4
+ class URI < Validator
5
+ def prepare
6
+ @schemes = @options.delete(:schemes) || []
7
+ end
8
+
9
+ def validate(data)
10
+ uri = ::URI.parse(data)
11
+ unless @schemes.empty? || @schemes.include?(uri.scheme)
12
+ failure('Invalid scheme')
13
+ else
14
+ uri
15
+ end
16
+ rescue ::URI::InvalidURIError
17
+ failure('Invalid URI')
18
+ end
19
+
20
+ def convert(uri)
21
+ uri.to_s
22
+ end
23
+ end
24
+
25
+ Uri = URI
26
+
27
+ class Email < Trafaret::String
28
+ REGEX = /\A(?<name>^[-!#$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+\/=?^_`{}|~0-9A-Z]+)* # dot-atom
29
+ |^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])* # quoted-string
30
+ )@(?<domain>(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$) # domain
31
+ |\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]\z/xi # literal form, ipv4 address (SMTP 4.1.3)
32
+
33
+ def prepare
34
+ super
35
+ @regex = REGEX
36
+ end
37
+ end
38
+ end
@@ -1,4 +1,6 @@
1
1
  class Trafaret::Validator
2
+ attr_accessor :converters, :options
3
+
2
4
  def initialize(*args, &blk)
3
5
  @options = (args.pop if args.last.is_a? Hash) || {}
4
6
  @args = args
@@ -51,13 +51,17 @@ module Trafaret
51
51
  end
52
52
 
53
53
  class String < Validator
54
+ def prepare
55
+ @regex = ::Regexp.compile @options[:regex] if @options[:regex]
56
+ end
57
+
54
58
  def validate(data)
55
59
  return failure('Not a String') unless data.is_a? ::String
56
60
  return failure('Should not be blank') if !@options[:allow_blank] && data.empty?
57
61
  return failure('Too short') if @options[:min_length] && data.size < @options[:min_length]
58
62
  return failure('Too long') if @options[:max_length] && data.size > @options[:max_length]
59
- if @options[:regex]
60
- match = @options[:regex].match(data)
63
+ if @regex
64
+ match = @regex.match(data)
61
65
  return failure('Does not match') unless match
62
66
  return match
63
67
  end
@@ -1,3 +1,3 @@
1
1
  module Trafaret
2
- VERSION = "1.5.6"
2
+ VERSION = "1.5.7"
3
3
  end
@@ -39,10 +39,11 @@ describe Trafaret::Base do
39
39
  karma: :integer
40
40
  },
41
41
  array: [{id: :integer}],
42
+ tuple: [:integer, :nil],
42
43
  proc_: proc { |d| d },
43
44
  just_trafaret: T.nil
44
45
  })
45
- res = t.call({kuku: 123, krkr: 'karma', hash: {karma: 234}, array: [{id: 123}, {id: 234}], proc_: 123, just_trafaret: nil})
46
+ res = t.call({kuku: 123, krkr: 'karma', hash: {karma: 234}, array: [{id: 123}, {id: 234}], proc_: 123, just_trafaret: nil, tuple: [123, nil]})
46
47
  res[:id].should == 'karma'
47
48
  res[:hash][:karma].should == 234
48
49
  res[:proc_].should == 123
@@ -163,7 +164,7 @@ describe Trafaret::Validator do
163
164
  end
164
165
 
165
166
  describe Trafaret::Case do
166
- it 'must work' do
167
+ it 'must case options' do
167
168
  cs = T.case do |c|
168
169
  c.when(T.integer) { |r| :int }
169
170
  c.when(T.string) { |r| :string }
@@ -177,9 +178,23 @@ describe Trafaret::Case do
177
178
  end
178
179
 
179
180
  describe Trafaret::Tuple do
180
- it 'must work' do
181
+ it 'must match tuple' do
181
182
  t = T.tuple(:integer, :string, :nil)
182
183
  t.call([1, 'a', nil]).should == [1, 'a', nil]
183
184
  t.call([1, 'a', 3]).dump.should == {2 => 'Value must be nil'}
184
185
  end
186
+ end
187
+
188
+ describe Trafaret::URI do
189
+ it 'must match uri' do
190
+ t = T.uri(schemes: ['http', 'https'])
191
+ t.call('http://ya.ru:80').should == 'http://ya.ru'
192
+ end
193
+ end
194
+
195
+ describe Trafaret::Email do
196
+ it 'must parse email' do
197
+ e = T.email.to { |m| m[:name] }
198
+ e.call('kuku@gmail.com').should == 'kuku'
199
+ end
185
200
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trafaret
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.6
4
+ version: 1.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikhail Krivushin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-20 00:00:00.000000000 Z
11
+ date: 2014-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -71,6 +71,7 @@ files:
71
71
  - lib/trafaret/constructor.rb
72
72
  - lib/trafaret/errors.rb
73
73
  - lib/trafaret/numeric.rb
74
+ - lib/trafaret/uri_email.rb
74
75
  - lib/trafaret/validator.rb
75
76
  - lib/trafaret/validators.rb
76
77
  - lib/trafaret/version.rb