russian_workdays 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,47 +1,36 @@
1
- require 'russian_workdays/version'
2
- require 'open-uri'
3
- require 'nokogiri'
1
+ require 'yaml'
4
2
 
5
- module RussianWorkdays
3
+ class RussianWorkdays
6
4
  class << self
7
- def get_dates year
8
- if @year == year
9
- return @dates
10
- else
11
- @year = year
5
+ %w(holiday short).each do |method|
6
+ define_method("#{method}?") do |date|
7
+ raise ArgumentError.new('Argument must be Date object') unless date.is_a?(Date)
8
+ dates[date.year][method].include?(date.to_s)
12
9
  end
13
- @dates = parse Nokogiri::HTML(open("http://www.superjob.ru/proizvodstvennyj_kalendar/#{year}"))
14
- end
15
10
 
16
- def parse page
17
- dates = {}
18
- months = [nil, 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']
19
- page.css('td.pk_container').each do |month|
20
- month.css('div.pk_cells div').each do |day|
21
- unless day.attr('class') == 'pk_other'
22
- date = Date.new @year, months.index(month.css('div.pk_header').inner_text), day.inner_text.to_i
23
- case day.attr 'class'
24
- when /pk_holiday/
25
- dates[date] = 'holiday'
26
- when /pk_preholiday/
27
- dates[date] = 'short'
28
- else
29
- dates[date] = 'work'
30
- end
31
- end
32
- end
11
+ define_method("#{method}s") do |year|
12
+ has_data_for_year?(year)
13
+ dates[year][method]
33
14
  end
34
- dates
35
15
  end
36
16
 
37
- %w(holiday short work).each do |method|
38
- define_method("#{method}?") do |date|
39
- get_dates(date.year)[date] == method
17
+ def work?(date)
18
+ raise ArgumentError unless date.is_a?(Date)
19
+ !holidays(date.year).include?(date) && !shorts(date.year).include?(date)
20
+ end
21
+
22
+ def works(year)
23
+ has_data_for_year?(year)
24
+ (Date.new(year, 1, 1)..Date.new(year, 12, 31)).to_a - holidays(year) - shorts(year)
25
+ end
26
+
27
+ private
28
+ def has_data_for_year?(year)
29
+ raise ArgumentError.new('Data missing for that year') unless dates.keys.include?(year)
40
30
  end
41
31
 
42
- define_method("#{method}s") do |year|
43
- get_dates(year).select{|key, value| value == method}.keys
32
+ def dates
33
+ @@dates ||= YAML.load_file(File.expand_path('../russian_workdays/dates.yml', __FILE__))
44
34
  end
45
- end
46
35
  end
47
- end
36
+ end
@@ -1,16 +1,15 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'russian_workdays/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = 'russian_workdays'
8
- spec.version = RussianWorkdays::VERSION
7
+ spec.version = '1.0.0'
9
8
  spec.authors = ['heckfy']
10
9
  spec.email = ['heckfyoz@gmail.com']
11
10
  spec.summary = 'Russian workdays'
12
- spec.description = ''
13
- spec.homepage = ''
11
+ spec.description = 'Производственный календарь РФ'
12
+ spec.homepage = 'https://github.com/heckfy/russian_workdays'
14
13
  spec.license = 'MIT'
15
14
 
16
15
  spec.files = `git ls-files -z`.split("\x0")
@@ -21,19 +21,19 @@ describe RussianWorkdays do
21
21
 
22
22
  it 'should return true if date is short day' do
23
23
  @shorts.each do |date|
24
- expect(RussianWorkdays.short?(Date.parse date)).to eq(true)
24
+ expect(RussianWorkdays.short?(Date.parse(date))).to eq(true)
25
25
  end
26
26
  end
27
27
 
28
28
  it 'should return true if date is holiday day' do
29
29
  @holidays.each do |date|
30
- expect(RussianWorkdays.holiday?(Date.parse date)).to eq(true)
30
+ expect(RussianWorkdays.holiday?(Date.parse(date))).to eq(true)
31
31
  end
32
32
  end
33
33
 
34
34
  it 'should return true if date is work day' do
35
35
  @works.each do |date|
36
- expect(RussianWorkdays.work?(Date.parse date)).to eq(true)
36
+ expect(RussianWorkdays.work?(Date.parse(date))).to eq(true)
37
37
  end
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russian_workdays
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - heckfy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-22 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: ''
55
+ description: "Производственный календарь РФ"
56
56
  email:
57
57
  - heckfyoz@gmail.com
58
58
  executables: []
@@ -65,11 +65,11 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/russian_workdays.rb
68
- - lib/russian_workdays/version.rb
68
+ - lib/russian_workdays/dates.yml
69
69
  - russian_workdays.gemspec
70
70
  - spec/russian_workdays_spec.rb
71
71
  - spec/spec_helper.rb
72
- homepage: ''
72
+ homepage: https://github.com/heckfy/russian_workdays
73
73
  licenses:
74
74
  - MIT
75
75
  metadata: {}
@@ -1,3 +0,0 @@
1
- module RussianWorkdays
2
- VERSION = '0.0.1'
3
- end