crabfarm 0.0.19 → 0.1.0

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: 90b7726fe962f1aa743078e2a50f6a477aab9e4d
4
- data.tar.gz: ed95f4776e57812c16f10785cb587d2a02a7bd9a
3
+ metadata.gz: f93ecc74ca65159efaed6eeeb3ff85abbb13fe78
4
+ data.tar.gz: 1b1690612566628e7a9d3a8a8d8adef8e8c9291b
5
5
  SHA512:
6
- metadata.gz: bf81855a24468d1ac95a0c8ce452385117c02e5fc3c115b7cb74ce51bd0cc66e8e441fece4db041f41f718ba808444115b2f032aa1e131558c05e8b816cd8912
7
- data.tar.gz: 19f2b2f80c599b39a3fabb76065f1eacac2449e6d8f468ff595d9ee871ccdd7731d1902cf2f05668a7bd3c8f642a0b76a081987bb009dcf04e60767ea34812d3
6
+ metadata.gz: 130fee56339b38ccead67d986354622e6608c5cdc5f703ff6a5791d3b0995e88750163888cfdb09017522b67e369745b2a5e5949048905b8a81ac1b8098f82fb
7
+ data.tar.gz: 550b31026059c8a31795eab00f0928d22d773adeab91ab16b5ae9b2b83534677292a60bb1cfd6b85f3fe23d2a19214a7424a8ca30ebd418e9f9ab02c449645a5
@@ -0,0 +1,13 @@
1
+ require "crabfarm/assertion/wrapper"
2
+
3
+ module Crabfarm
4
+ module Assertion
5
+ module Context
6
+
7
+ def assert(_value)
8
+ Wrapper.new _value, self
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,105 @@
1
+ module Crabfarm
2
+ module Assertion
3
+ module Parsers
4
+
5
+ def parse_integer _value, _options={}
6
+ parse_number :to_i, _value, _options
7
+ end
8
+
9
+ def parse_float _value, _options={}
10
+ parse_number :to_f, _value, _options
11
+ end
12
+
13
+ def parse_phrase _value, _options={}
14
+ fail_with_nil if _value.nil?
15
+
16
+ if _value.is_a? String
17
+ clean_string _value, _options
18
+ else
19
+ if _value.respond_to? :to_s
20
+ clean_string _value.to_s, _options
21
+ else
22
+ fail_with "Value cannot be transformed to string"
23
+ end
24
+ end
25
+ end
26
+
27
+ def parse_boolean _value, _options={}
28
+ truthy = Array(_options.fetch(:truthy, ['true']))
29
+ falsy = Array(_options.fetch(:falsy, ['false']))
30
+
31
+ mapping = { :empty => false, true => true, false => false }
32
+ truthy.each { |k| mapping[k] = true }
33
+ falsy.each { |k| mapping[k] = false }
34
+
35
+ parse_by_map _value, mapping
36
+ end
37
+
38
+ def parse_by_map _value, _map={}
39
+ fail_with_nil if _value.nil?
40
+
41
+ case _value
42
+ when String, Symbol
43
+ _value = _value.strip if _value.is_a? String
44
+ return parse_by_map :empty, _map if _value.empty?
45
+ fail_with "#'{_value.to_s}' is and invalid keyword" unless _map.key? _value
46
+ _map[_value]
47
+ else
48
+ fail_with "'#{_value}' cannot be mapped"
49
+ end
50
+ end
51
+
52
+ def parse_number _to_fun, _value, _options={}
53
+ fail_with_nil if _value.nil?
54
+
55
+ if _value.is_a? String
56
+ _value.strip!
57
+ if _value.empty?
58
+ return_default _options
59
+ else
60
+ extract_number_from_string(_value, _options).send _to_fun
61
+ end
62
+ elsif _value.respond_to? _to_fun
63
+ _value.send _to_fun
64
+ else
65
+ fail_with "'#{_value}' cannot be transformed to number"
66
+ end
67
+ end
68
+
69
+ def clean_string _string, _options
70
+ normalized = _string.strip.gsub(/\s+/, ' ')
71
+ if normalized.empty?
72
+ return_default _options
73
+ else normalized end
74
+ end
75
+
76
+ def fail_with_nil
77
+ fail_with "'nil' cant be parsed"
78
+ end
79
+
80
+ def return_default _options
81
+ fail_with "Value is an empty" unless _options.key? :default
82
+ _options[:default]
83
+ end
84
+
85
+ def extract_number_from_string _value, _options
86
+ decimal_mark = _options.fetch(:decimal_mark, '.') # TODO: make default decimal mark configurable
87
+ thousand_mark = _options.fetch(:thousand_mark, infer_thousand_separator(decimal_mark))
88
+ num_rgx = /(?:\.\d+|\d{1,3}(?:\,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?)/
89
+ num_rgx = Regexp.new "(?:\\#{decimal_mark}\\d+|\\d{1,3}(?:\\#{thousand_mark}\\d{3})+(?:\\#{decimal_mark}\\d+)?|\\d+(?:\\#{decimal_mark}\\d+)?)"
90
+ matches = _value.scan num_rgx
91
+ fail_with "'#{_value}' has an ambiguous numeric format" if matches.count > 1
92
+ fail_with "'#{_value}' does not contain any number" if matches.count < 1
93
+ matches.first.gsub(thousand_mark,'').gsub(decimal_mark, '.')
94
+ end
95
+
96
+ def infer_thousand_separator _decimal_mark
97
+ case _decimal_mark
98
+ when '.' then ','
99
+ when ',' then '.'
100
+ else '' end
101
+ end
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,28 @@
1
+ module Crabfarm
2
+ module Assertion
3
+ module Validations
4
+
5
+ def validate_number _value, _options={}
6
+ fail_with "#{_value} out of range" if _options.key? :greater_than and _value <= _options[:greater_than]
7
+ fail_with "#{_value} out of range" if _options.key? :greater_or_equal_to and _value < _options[:greater_or_equal_to]
8
+ fail_with "#{_value} out of range" if _options.key? :less_than and _value >= _options[:less_than]
9
+ fail_with "#{_value} out of range" if _options.key? :less_or_equal_to and _value > _options[:less_or_equal_to]
10
+ fail_with "#{_value} out of range" if _options.key? :between and not _options[:between].include? _value
11
+ end
12
+
13
+ def validate_word _value, _options={}
14
+ fail_with "'#{_value}' is not a single word" if /\s/ === _value
15
+ validate_string _value, _options
16
+ end
17
+
18
+ def validate_string _value, _options={}
19
+ fail_with "#{_value} does not match expression" if _options.key? :matches and not _options[:matches] === _value
20
+ end
21
+
22
+ def validate_general _value, _options={}
23
+ fail_with "#{_value} is not recognized" if _options.key? :in and not _options[:in].include? _value
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,101 @@
1
+ require "crabfarm/assertion/parsers"
2
+ require "crabfarm/assertion/validations"
3
+
4
+ module Crabfarm
5
+ module Assertion
6
+ class Wrapper
7
+ include Parsers
8
+ include Validations
9
+
10
+ def initialize(_value, _context=nil)
11
+ @value = if _value.respond_to? :text
12
+ _value.text
13
+ else _value end
14
+
15
+ @context = _context
16
+ end
17
+
18
+ def is_integer(_options={})
19
+ perform_assertion(_options) {
20
+ @value = parse_integer @value, _options
21
+ validate_number @value, _options
22
+ validate_general @value, _options
23
+ }
24
+ end
25
+
26
+ alias :is_i :is_integer
27
+
28
+ def is_float(_options={})
29
+ perform_assertion(_options) {
30
+ @value = parse_float @value, _options
31
+ validate_number @value, _options
32
+ validate_general @value, _options
33
+ }
34
+ end
35
+
36
+ alias :is_f :is_float
37
+
38
+ def is_word(_options={})
39
+ perform_assertion(_options) {
40
+ @value = parse_phrase @value, _options
41
+ validate_word @value, _options
42
+ validate_general @value, _options
43
+ }
44
+ end
45
+
46
+ alias :is_w :is_word
47
+ def is_string(_options={})
48
+ perform_assertion(_options) {
49
+ @value = parse_phrase @value, _options
50
+ validate_string @value, _options
51
+ validate_general @value, _options
52
+ }
53
+ end
54
+
55
+ alias :is_s :is_string
56
+
57
+ def is_boolean(_options={})
58
+ perform_assertion(_options) {
59
+ @value = parse_boolean @value, _options
60
+ }
61
+ end
62
+
63
+ alias :is_b :is_boolean
64
+
65
+ def matches(_rgx, _options={})
66
+ perform_assertion(_options) {
67
+ match = _rgx.match @value
68
+ fail_with "#{@value} does not match #{_rgx.to_s}" if match.nil?
69
+ match
70
+ }
71
+ end
72
+
73
+ def method_missing(_method, *_args)
74
+ if @value.respond_to? :method
75
+ @value = @value.send(*_args)
76
+ else super end
77
+ end
78
+
79
+ def respond_to?(*args)
80
+ @value.respond_to?(*args)
81
+ end
82
+
83
+ private
84
+
85
+ def perform_assertion(_options)
86
+ begin
87
+ yield
88
+ @value
89
+ rescue AssertionError # => exc
90
+ # for now just raise, in the future event could be hooked here...
91
+ raise
92
+ end
93
+ end
94
+
95
+ def fail_with(_message)
96
+ raise AssertionError.new _message
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -1,5 +1,8 @@
1
+ require "crabfarm/assertion/context"
2
+
1
3
  module Crabfarm
2
4
  class BaseParser < Delegator
5
+ include Assertion::Context
3
6
 
4
7
  attr_reader :params, :document
5
8
 
@@ -1,8 +1,10 @@
1
1
  require 'thwait'
2
2
  require 'crabfarm/forked_state'
3
+ require "crabfarm/assertion/context"
3
4
 
4
5
  module Crabfarm
5
6
  class BaseState
7
+ include Assertion::Context
6
8
  extend Forwardable
7
9
 
8
10
  PARSE_METHOD_RX = /^parse_(.*)$/
@@ -75,9 +75,12 @@ module Crabfarm
75
75
 
76
76
  c.desc "Generates a new crabfarm application"
77
77
  c.command :app do |app|
78
+ app.desc "Set the remote used by the crawler"
79
+ app.flag [:r, :remote]
80
+
78
81
  app.action do |global_options,options,args|
79
82
  require "crabfarm/modes/generator"
80
- Crabfarm::Modes::Generator.new.generate_app(args[0], Dir.pwd)
83
+ Crabfarm::Modes::Generator.generate_app(Dir.pwd, args[0], options[:remote])
81
84
  end
82
85
  end
83
86
 
@@ -85,7 +88,7 @@ module Crabfarm
85
88
  c.command :parser do |parser|
86
89
  parser.action do |global_options,options,args|
87
90
  require "crabfarm/modes/generator"
88
- Crabfarm::Modes::Generator.new.generate_parser(args[0])
91
+ Crabfarm::Modes::Generator.generate_parser(args[0])
89
92
  end
90
93
  end
91
94
 
@@ -93,7 +96,7 @@ module Crabfarm
93
96
  c.command :state do |parser|
94
97
  parser.action do |global_options,options,args|
95
98
  require "crabfarm/modes/generator"
96
- Crabfarm::Modes::Generator.new.generate_state(args[0])
99
+ Crabfarm::Modes::Generator.generate_state(args[0])
97
100
  end
98
101
  end
99
102
  end
@@ -108,7 +111,7 @@ module Crabfarm
108
111
  end
109
112
  end
110
113
 
111
- desc "Publish the crawler to a crabfarm cloud"
114
+ desc "Publish the crawler to the crabfarm.io cloud"
112
115
  command :publish do |c|
113
116
  c.desc "Just list the files that are beign packaged"
114
117
  c.switch :dry, :default_value => false
@@ -119,6 +122,8 @@ module Crabfarm
119
122
  c.action do |global_options,options,args|
120
123
  next puts "This command can only be run inside a crabfarm application" unless defined? CF_PATH
121
124
 
125
+ options[:remote] = args[0]
126
+
122
127
  require "crabfarm/modes/publisher"
123
128
  Crabfarm::Modes::Publisher.publish CF_PATH, options
124
129
  end
@@ -4,6 +4,8 @@ module Crabfarm
4
4
 
5
5
  class ConfigurationError < Error; end
6
6
 
7
+ class AssertionError < Error; end
8
+
7
9
  class EntityNotFoundError < Error
8
10
  attr_accessor :role, :name
9
11
 
@@ -6,12 +6,13 @@ require 'ostruct'
6
6
 
7
7
  module Crabfarm
8
8
  module Modes
9
- class Generator
9
+ module Generator
10
10
 
11
- def generate_app(_name, _target)
11
+ def generate_app(_target, _name, _default_remote=nil)
12
12
  with_external_path _target do
13
13
  binding = {
14
14
  name: _name,
15
+ remote: _default_remote,
15
16
  version: Crabfarm::VERSION
16
17
  }
17
18
 
@@ -83,7 +84,7 @@ module Crabfarm
83
84
  self
84
85
  end
85
86
 
86
- private
87
+ private
87
88
 
88
89
  def generate_dir(_path, _silent)
89
90
  path = File.join(*_path)
@@ -123,6 +124,8 @@ module Crabfarm
123
124
  def render_op(_op, _message, _color)
124
125
  puts _op.rjust(10).color(_color) + ' ' + _message
125
126
  end
127
+
128
+ extend self
126
129
  end
127
130
  end
128
131
  end
@@ -1,8 +1,10 @@
1
1
  require 'yaml'
2
+ require 'json'
2
3
  require 'git'
3
4
  require 'zlib'
5
+ require 'inquirer'
4
6
  require 'rubygems/package'
5
- require 'net/http/post/multipart'
7
+ require 'base64'
6
8
  require 'rainbow'
7
9
  require 'rainbow/ext/string'
8
10
  require 'digest/sha1'
@@ -20,7 +22,7 @@ module Crabfarm
20
22
  @options = _options
21
23
 
22
24
  load_config
23
- return unless dry_run or authenticated?
25
+ return unless dry_run or check_credentials
24
26
  detect_git_repo
25
27
 
26
28
  if inside_git_repo?
@@ -37,7 +39,7 @@ module Crabfarm
37
39
  compress_package
38
40
  generate_signature
39
41
 
40
- send_package unless dry_run
42
+ send_package if not dry_run and ensure_valid_remote
41
43
  end
42
44
 
43
45
  private
@@ -59,27 +61,76 @@ module Crabfarm
59
61
  end
60
62
 
61
63
  def load_config
62
- config = YAML.load_file config_path
64
+ @local_config = YAML.load_file config_path
63
65
 
64
- if File.exists? home_config_path
65
- home_config = YAML.load_file home_config_path
66
- config = home_config.merge config
67
- end
66
+ @home_config = if File.exists? home_config_path
67
+ YAML.load_file home_config_path
68
+ else {} end
69
+
70
+ config = @home_config.merge @local_config
68
71
 
69
72
  @token = config['token']
70
- @name = config['name']
73
+ @url = @options[:remote] || config['remote']
71
74
  @host = config['host'] || DEFAULT_HOST
72
75
  @include = config['files']
73
76
  end
74
77
 
75
- def authenticated?
76
- # TODO: if no token, ask for credentials and fetch token
77
- if @token.nil? or @token.empty?
78
- puts "No crabfarm API token has been provided".color(:red)
79
- return false
78
+ def ensure_valid_remote
79
+ if @url.nil?
80
+ @url = Ask.input 'Enter default remote for crawler'
81
+ return false unless validate_remote @url
82
+ @local_config['remote'] = @url
83
+ save_local_config
84
+ return true
85
+ else
86
+ validate_remote @url
80
87
  end
88
+ end
89
+
90
+ def validate_remote(_url)
91
+ return true if /^\w+\/\w+$/i === _url
92
+ puts "Invalid remote syntax: #{_url}".color :red
93
+ return false
94
+ end
95
+
96
+ def check_credentials
97
+ if @token.nil?
98
+ puts 'No credential data found, please identify yourself'
99
+ email = Ask.input 'Enter your crabfarm.io email'
100
+ password = Ask.input 'Enter your crabfarm.io password'
101
+
102
+ resp = send_request Net::HTTP::Post, 'api/tokens', {
103
+ 'email' => email,
104
+ 'password' => password
105
+ }
106
+
107
+ case resp
108
+ when Net::HTTPCreated
109
+ @token = JSON.parse(resp.body)['token']
110
+ @home_config['token'] = @token
111
+ save_home_config
112
+ when Net::HTTPUnauthorized
113
+ puts "The provided credentials are invalid!".color(:red)
114
+ else
115
+ puts "Unknown error when asking for token!".color(:red)
116
+ end
117
+ end
118
+
119
+ not @token.nil?
120
+ end
121
+
122
+ def save_local_config
123
+ save_config config_path, @local_config
124
+ end
81
125
 
82
- true
126
+ def save_home_config
127
+ save_config home_config_path, @home_config
128
+ end
129
+
130
+ def save_config(_path, _config)
131
+ data = YAML.dump _config
132
+ data = data.split("\n", 2).last # remove first line to make it more readable
133
+ File.open(_path, 'w') { |f| f.write data }
83
134
  end
84
135
 
85
136
  def is_tree_dirty?
@@ -167,21 +218,35 @@ module Crabfarm
167
218
  end
168
219
 
169
220
  def send_package
170
- url = URI.join(@host, 'api/crawlers/', @name)
171
-
172
- req = Net::HTTP::Put::Multipart.new(url.path, {
173
- "repo" => UploadIO.new(StringIO.new(@cpackage.string), "application/x-gzip", "tree.tar.gz"),
221
+ resp = send_request(Net::HTTP::Put, "api/bots/#{@url}", {
222
+ "repo" => Base64.encode64(@cpackage.string),
174
223
  "sha" => @signature,
175
224
  "ref" => @ref
176
- }, {
177
- 'X-Api-Token' => @token
178
225
  })
179
226
 
180
- res = Net::HTTP.start(url.host, url.port) do |http|
181
- http.request(req)
227
+ case resp
228
+ when Net::HTTPSuccess
229
+ sha = JSON.parse(resp.body)['sha']
230
+ puts "#{@url} updated!"
231
+ when Net::HTTPUnauthorized
232
+ puts "You are not authorized to update crawler: #{@url}".color(:red)
233
+ when Net::HTTPNotFound
234
+ puts "Crawler not found: #{@url}".color(:red)
235
+ else
236
+ puts "Unknown error when updating crawler information!".color(:red)
182
237
  end
238
+ end
239
+
240
+ def send_request(_class, _path, _data=nil)
241
+ uri = URI.join(@host, _path)
183
242
 
184
- puts res.body
243
+ req = req = _class.new uri.path
244
+ req.set_form_data _data
245
+ req['X-User-Token'] = @token unless @token.nil?
246
+
247
+ Net::HTTP.start(uri.host, uri.port) do |http|
248
+ http.request(req)
249
+ end
185
250
  end
186
251
 
187
252
  end
@@ -1,9 +1,9 @@
1
- host: 'http://www.crabfarm.io'
2
- name: '<%= name %>'
1
+ host: http://api.crabfarm.io
2
+ <% unless remote.nil? %>remote: <%= remote %><% end %>
3
3
  files:
4
4
  - Crabfile
5
5
  - Gemfile
6
6
  - Gemfile.lock
7
7
  - boot.rb
8
8
  - app/**/*.*
9
- - bin/**/*.*
9
+ - bin/**/*.*
@@ -1,3 +1,3 @@
1
1
  module Crabfarm
2
- VERSION = "0.0.19"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crabfarm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Baixas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: selenium-webdriver
@@ -72,6 +72,20 @@ dependencies:
72
72
  - - ~>
73
73
  - !ruby/object:Gem::Version
74
74
  version: 2.12.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: inquirer
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: 0.2.0
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: 0.2.0
75
89
  - !ruby/object:Gem::Dependency
76
90
  name: rainbow
77
91
  requirement: !ruby/object:Gem::Requirement
@@ -128,20 +142,6 @@ dependencies:
128
142
  - - '>='
129
143
  - !ruby/object:Gem::Version
130
144
  version: '0'
131
- - !ruby/object:Gem::Dependency
132
- name: multipart-post
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - '>='
136
- - !ruby/object:Gem::Version
137
- version: '0'
138
- type: :runtime
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - '>='
143
- - !ruby/object:Gem::Version
144
- version: '0'
145
145
  - !ruby/object:Gem::Dependency
146
146
  name: bundler
147
147
  requirement: !ruby/object:Gem::Requirement
@@ -338,6 +338,20 @@ dependencies:
338
338
  - - ~>
339
339
  - !ruby/object:Gem::Version
340
340
  version: 2.2.0
341
+ - !ruby/object:Gem::Dependency
342
+ name: fakefs
343
+ requirement: !ruby/object:Gem::Requirement
344
+ requirements:
345
+ - - ~>
346
+ - !ruby/object:Gem::Version
347
+ version: 0.6.7
348
+ type: :development
349
+ prerelease: false
350
+ version_requirements: !ruby/object:Gem::Requirement
351
+ requirements:
352
+ - - ~>
353
+ - !ruby/object:Gem::Version
354
+ version: 0.6.7
341
355
  description:
342
356
  email:
343
357
  - ignacio@platan.us
@@ -354,6 +368,10 @@ files:
354
368
  - lib/crabfarm/adapters/output/ostruct.rb
355
369
  - lib/crabfarm/adapters/parser/nokogiri.rb
356
370
  - lib/crabfarm/adapters/parser/pdf_reader.rb
371
+ - lib/crabfarm/assertion/context.rb
372
+ - lib/crabfarm/assertion/parsers.rb
373
+ - lib/crabfarm/assertion/validations.rb
374
+ - lib/crabfarm/assertion/wrapper.rb
357
375
  - lib/crabfarm/base_parser.rb
358
376
  - lib/crabfarm/base_state.rb
359
377
  - lib/crabfarm/cli.rb
@@ -369,9 +387,7 @@ files:
369
387
  - lib/crabfarm/dsl/surfer.rb
370
388
  - lib/crabfarm/engines/safe_state_loop.rb
371
389
  - lib/crabfarm/errors.rb
372
- - lib/crabfarm/event_store.rb
373
390
  - lib/crabfarm/forked_state.rb
374
- - lib/crabfarm/helpers/value_helper.rb
375
391
  - lib/crabfarm/http_client.rb
376
392
  - lib/crabfarm/mocks/noop_driver.rb
377
393
  - lib/crabfarm/modes/console.rb
@@ -1,20 +0,0 @@
1
- module Crabfarm
2
- class EventStore
3
-
4
- def initialize
5
- @events = []
6
- @mutex = Mutex.new
7
- end
8
-
9
- def event(_category, _message)
10
- @mutex.synchronize do
11
- @events << {
12
- created_at: Time.current,
13
- category: _category,
14
- msg: _message
15
- }
16
- end
17
- end
18
-
19
- end
20
- end
@@ -1,131 +0,0 @@
1
- module Crabfarm
2
- module AssertionHelper
3
-
4
-
5
- class Assert
6
-
7
- assert_int('20', min: -10, max: 4, in: [1,2,3,4] )
8
- assert_float()
9
- assert_boolean()
10
- assert val, is: :integer|float, between: xxx, default: nil
11
- assert val, is: :boolean
12
-
13
- test = assert(val).is_i between: 1..2, empty: 20, in: [1, 2, 3], default: nil # default when empty
14
- assert(val).is_b empty: 20 # trims, downcase
15
- assert(val).trimmed.downcased.is_s in: ['test', 'peo', 'hola'], matches: /asadad/, empty: :nil|false|true
16
- assert(products.count).is_i greater_than: 20
17
-
18
-
19
-
20
-
21
- matches
22
- assert(val).is_i between: 1..2, default: 20
23
- assert(val).is_f
24
- assert(val).is_s default: 'hello', in: ['servicentro', nil]
25
- assert(val).is_boolean default: true, truthy: 'servicentro', falsy: ['falso', 'caca']
26
- assert(val).downcased.is_one_of 'hola'
27
- assert(val).is_not_nil
28
- assert(val).is_of_size
29
- assert(val).is_date 'YYYY-MM-dd'
30
-
31
- class AssertionWrapper
32
-
33
- def stripped
34
- @value = @value.strip if @value.is_a? String
35
- self
36
- end
37
-
38
- def trimmed
39
- stripped
40
- @value = @value.gsub(/\s+/, ' ') if @value.is_a? String
41
- self
42
- end
43
-
44
- def upcased
45
- @value = @value.upcase if @value.is_a? String
46
- self
47
- end
48
-
49
- def downcased
50
- @value = @value.downcase if @value.is_a? String
51
- self
52
- end
53
-
54
- def is_integer(_options={})
55
- perform_assertion {
56
- @value = parse_integer @value, _options
57
- validate_numeric @value, _options
58
- validate_general @value, _options
59
- }
60
- end
61
-
62
- alias :is_integer :is_i
63
-
64
- def is_float(_options={})
65
- perform_assertion {
66
- @value = parse_float @value, _options
67
- validate_numeric @value, _options
68
- validate_general @value, _options
69
- }
70
- end
71
-
72
- alias :is_float :is_f
73
-
74
- def is_string(_options={})
75
- perform_assertion {
76
- @value = parse_string @value, _options
77
- validate_general @value, _options
78
- }
79
- end
80
-
81
- alias :is_string :is_s
82
-
83
- def is_boolean(_options={})
84
- perform_assertion {
85
- @value = parse_boolean @value
86
- }
87
- end
88
-
89
- alias :is_string :is_b
90
-
91
- def matches(_rgx, _)
92
- perform_assertion {
93
-
94
- }
95
- end
96
-
97
- private
98
-
99
- def perform_assertion
100
- end
101
-
102
- def parse_integer _val, _options
103
- end
104
-
105
- def parse_float _val, _options
106
- end
107
-
108
- def parse_string _val, _options
109
- end
110
-
111
- def parse_boolean _val, _options
112
- end
113
-
114
- def is_numeric(_options)
115
- end
116
-
117
- def is_
118
-
119
- def handle_error(_assertion)
120
- end
121
-
122
- end
123
-
124
-
125
- assert_date()
126
- assert_not_nil()
127
- assert_count( between: 0..2, nil: true, halt: true)
128
- assert_regex( )
129
-
130
- end
131
- end