quando 0.0.1
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 +7 -0
- data/.editorconfig +10 -0
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +41 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/lib/quando/config.rb +84 -0
- data/lib/quando/parser.rb +90 -0
- data/lib/quando/version.rb +5 -0
- data/lib/quando.rb +27 -0
- data/quando.gemspec +24 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1ac311f1942017666bffa27bcfded9b292d59b1b80733e9eb19cf275f24ae4be
|
4
|
+
data.tar.gz: 3c1e52f8eeffba30e192aa38732055daced228721f0ae81f935314aed80cd2c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 58126069e12f3446a1f555fe2cdcc68229901fd9f8a05b0b58c1bc8f35187991a6c6d602b5028522d7e6c841afd19868cc326f5934b273a6fa91154c80427eab
|
7
|
+
data.tar.gz: 0d265149ce2f56b86281417bb312fa251ff2bee9f2ef16ea50ef44492e5a5a79e04c897d5e0b0cc4f4f4adaaab8ade5aa90af2142ab78b01c3b5533dcfdddd50
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
quando (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
method_source (0.9.0)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
rspec (3.6.0)
|
17
|
+
rspec-core (~> 3.6.0)
|
18
|
+
rspec-expectations (~> 3.6.0)
|
19
|
+
rspec-mocks (~> 3.6.0)
|
20
|
+
rspec-core (3.6.0)
|
21
|
+
rspec-support (~> 3.6.0)
|
22
|
+
rspec-expectations (3.6.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.6.0)
|
25
|
+
rspec-mocks (3.6.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.6.0)
|
28
|
+
rspec-support (3.6.0)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
bundler (~> 1.15)
|
35
|
+
pry
|
36
|
+
quando!
|
37
|
+
rake (~> 10.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.16.1
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Quando
|
2
|
+
|
3
|
+
[](https://semaphoreci.com/kinkou/quando)
|
4
|
+
[](https://codeclimate.com/github/kinkou/quando/maintainability)
|
5
|
+
|
6
|
+
Quando is a configurable date parser. Show it what's what and parse any (Gregorian calendar) date. Quando can be configured on:
|
7
|
+
|
8
|
+
#### Application-level:
|
9
|
+
```ruby
|
10
|
+
Quando.configure do |c|
|
11
|
+
c.dlm = /[ ,-]/
|
12
|
+
|
13
|
+
c.jan = /janeiro/i
|
14
|
+
c.feb = /fevereiro/i
|
15
|
+
c.mar = /março/i
|
16
|
+
c.apr = /abril/i
|
17
|
+
# …
|
18
|
+
c.unimonth!
|
19
|
+
|
20
|
+
c.formats = [
|
21
|
+
/#{c.month_num} #{c.dlm} #{c.month_txt} #{c.dlm} #{c.year}/xi,
|
22
|
+
/#{c.year} #{c.dlm} #{c.month_txt} #{c.dlm} #{c.month_num}/xi,
|
23
|
+
]
|
24
|
+
end
|
25
|
+
|
26
|
+
Quando.parse('14-abril-1965') #=> #<Date: 1965-04-14>
|
27
|
+
Quando.parse('1965, abril 14') #=> #<Date: 1965-04-14>
|
28
|
+
```
|
29
|
+
|
30
|
+
#### Instance-level:
|
31
|
+
It will not affect your application-level configuration.
|
32
|
+
```ruby
|
33
|
+
Quando.parse('14-abril-1965') #=> nil
|
34
|
+
|
35
|
+
date_parser = Quando::Parser.new.configure do |c|
|
36
|
+
# here be the options from the previous example
|
37
|
+
end
|
38
|
+
date_parser.parse('14-abril-1965') #=> #<Date: 1965-04-14>
|
39
|
+
|
40
|
+
Quando.parse('14-abril-1965') #=> nil
|
41
|
+
```
|
42
|
+
|
43
|
+
#### Or just one-time usage:
|
44
|
+
```ruby
|
45
|
+
m = /(?<year>#{Quando.config.year}) (?<day>\d\d) (?<month>[A-Z]+)/i
|
46
|
+
Quando.parse('1965 14 Apr', matcher: m) #=> #<Date: 1965-04-14>
|
47
|
+
```
|
48
|
+
|
49
|
+
Enjoy.
|
data/Rakefile
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Quando
|
5
|
+
class Config
|
6
|
+
|
7
|
+
AVAILABLE_OPTIONS = [
|
8
|
+
:dlm, :year, :day,
|
9
|
+
:jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec,
|
10
|
+
:month_num, :month_txt,
|
11
|
+
:formats
|
12
|
+
]
|
13
|
+
|
14
|
+
attr_accessor *AVAILABLE_OPTIONS
|
15
|
+
|
16
|
+
MONTHS = [:jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec]
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@dlm = /[ -.\/\\]/
|
20
|
+
@year = /(?<year>(?:\d{2})|(?:\d{4}))/i
|
21
|
+
@month_num = /(?<month>(?:1[0-2])|(?:0?[1-9]))/
|
22
|
+
@day = /(?<day>(?:[3][0-1])|(?:[1-2][0-9])|(?:0?[1-9]))/
|
23
|
+
|
24
|
+
@jan = /(?:JANUARY)|(?:JAN\.?)/i
|
25
|
+
@feb = /(?:FEBRUARY)|(?:FEB\.?)/i
|
26
|
+
@mar = /(?:MARCH)|(?:MAR\.?)/i
|
27
|
+
@apr = /(?:APRIL)|(?:APR\.?)/i
|
28
|
+
@may = /(?:MAY\.?)/i
|
29
|
+
@jun = /(?:JUNE)|(?:JUN\.?)/i
|
30
|
+
@jul = /(?:JULY)|(?:JUL\.?)/i
|
31
|
+
@aug = /(?:AUGUST)|(?:AUG\.?)/i
|
32
|
+
@sep = /(?:SEPTEMBER)|(?:SEPT?\.?)/i
|
33
|
+
@oct = /(?:OCTOBER)|(?:OCT\.?)/i
|
34
|
+
@nov = /(?:NOVEMBER)|(?:NOV\.?)/i
|
35
|
+
@dec = /(?:DECEMBER)|(?:DEC\.?)/i
|
36
|
+
|
37
|
+
uniupdate!
|
38
|
+
end
|
39
|
+
|
40
|
+
def uniformats!
|
41
|
+
@formats = [
|
42
|
+
# 14.4.1965, 14/04/1965, 13-12-05
|
43
|
+
/\A\s* #{@day} #{@dlm} #{@month_num} #{@dlm} #{@year} \s*\z/xi,
|
44
|
+
|
45
|
+
# 14-APRIL-1965, 14-apr-65, 13/Dec/05, …
|
46
|
+
/\A\s* #{@day} #{@dlm} #{@month_txt} #{@dlm} #{@year} \s*\z/xi,
|
47
|
+
|
48
|
+
# April 1965, apr.1965, DEC-05, …
|
49
|
+
/\A\s* #{@month_txt} #{@dlm} #{@year} \s*\z/xi,
|
50
|
+
|
51
|
+
# April, DECEMBER, apr., …
|
52
|
+
/\A\s* #{@month_txt} \s*\z/xi,
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
def unimonth!
|
57
|
+
all_months_txt_rxs = MONTHS.map { |m| instance_variable_get("@#{m}".to_sym) }.join('|')
|
58
|
+
@month_txt = Regexp.new("(?<month>#{all_months_txt_rxs})", true)
|
59
|
+
end
|
60
|
+
|
61
|
+
def uniupdate!
|
62
|
+
unimonth!
|
63
|
+
uniformats!
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [Quando::Config]
|
69
|
+
def self.config
|
70
|
+
@config ||= Config.new
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.configure
|
74
|
+
config unless @config
|
75
|
+
yield(config) if block_given?
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.reset!
|
79
|
+
@config = Config.new
|
80
|
+
end
|
81
|
+
|
82
|
+
configure
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Quando
|
4
|
+
|
5
|
+
class Parser
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@config = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
# @return [Quando::Parser]
|
12
|
+
def configure
|
13
|
+
yield(@config = Quando.config.dup)
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [Quando::Config]
|
18
|
+
def config
|
19
|
+
@config || Quando.config
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param text_date [String]
|
23
|
+
# @return [Date, nil]
|
24
|
+
def parse(text_date)
|
25
|
+
config.formats.each do |rx|
|
26
|
+
@date_parts = text_date.match(rx)
|
27
|
+
next unless @date_parts
|
28
|
+
|
29
|
+
@current_format = rx
|
30
|
+
year, month, day = detect_year, detect_month, detect_day
|
31
|
+
next unless (year || month || day)
|
32
|
+
|
33
|
+
return Date.new(year, month, day)
|
34
|
+
end
|
35
|
+
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# @return [Integer, nil]
|
42
|
+
def detect_year
|
43
|
+
return Time.now.year unless searched?(:year)
|
44
|
+
return unless found?(:year)
|
45
|
+
|
46
|
+
year = @date_parts[:year].to_i
|
47
|
+
year < 100 ? year + 2000 : year
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Integer, nil]
|
51
|
+
def detect_month
|
52
|
+
return 1 unless searched?(:month)
|
53
|
+
return unless found?(:month)
|
54
|
+
|
55
|
+
month = @date_parts[:month]
|
56
|
+
|
57
|
+
if config.month_num.match(month)
|
58
|
+
month.to_i
|
59
|
+
else
|
60
|
+
month_index = Quando::Config::MONTHS.find_index do |month_name|
|
61
|
+
month_name_rx = config.send(month_name)
|
62
|
+
month_name_rx.match(month)
|
63
|
+
end
|
64
|
+
|
65
|
+
month_index + 1 if month_index
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# @return [Integer, nil]
|
70
|
+
def detect_day
|
71
|
+
return 1 unless searched?(:day)
|
72
|
+
return unless found?(:day)
|
73
|
+
|
74
|
+
day = @date_parts[:day].to_i
|
75
|
+
day if (1..31).include?(day)
|
76
|
+
end
|
77
|
+
|
78
|
+
# @param date_part [Symbol]
|
79
|
+
def found?(date_part)
|
80
|
+
!@date_parts[date_part.to_s].to_s.squeeze.empty?
|
81
|
+
end
|
82
|
+
|
83
|
+
# @param date_part [Symbol]
|
84
|
+
def searched?(date_part)
|
85
|
+
!!@current_format.named_captures[date_part.to_s]
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/lib/quando.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'quando/version'
|
5
|
+
require 'quando/config'
|
6
|
+
require 'quando/parser'
|
7
|
+
|
8
|
+
module Quando
|
9
|
+
|
10
|
+
# @param opts [Hash]
|
11
|
+
# @param date [String]
|
12
|
+
# @return [Date, nil]
|
13
|
+
def self.parse(date, opts = {})
|
14
|
+
return if (date = date.to_s.strip).empty?
|
15
|
+
|
16
|
+
p = Parser.new
|
17
|
+
|
18
|
+
if opts[:matcher]
|
19
|
+
p.configure do |c|
|
20
|
+
c.formats = [opts[:matcher]]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
p.parse(date)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/quando.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'quando/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'quando'
|
7
|
+
spec.version = Quando::VERSION
|
8
|
+
spec.authors = ['Sergey Konotopov']
|
9
|
+
spec.email = ['werk@mail.ru']
|
10
|
+
|
11
|
+
spec.summary = %q{Configurable date parser}
|
12
|
+
spec.description = %q{Parse dates in any language and format by setting your own recognition patterns}
|
13
|
+
spec.homepage = 'https://github.com/kinkou/quando'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
spec.add_development_dependency 'pry'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quando
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Konotopov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Parse dates in any language and format by setting your own recognition
|
70
|
+
patterns
|
71
|
+
email:
|
72
|
+
- werk@mail.ru
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".editorconfig"
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/quando.rb
|
85
|
+
- lib/quando/config.rb
|
86
|
+
- lib/quando/parser.rb
|
87
|
+
- lib/quando/version.rb
|
88
|
+
- quando.gemspec
|
89
|
+
homepage: https://github.com/kinkou/quando
|
90
|
+
licenses: []
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.7.6
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Configurable date parser
|
112
|
+
test_files: []
|