dtv_tournaments 0.0.1 → 0.0.3

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDg1MmVkMDExYTUzMTRlMjdiMGFlYzc5YjYzYmFhZjk4YjA5YTU0Mg==
4
+ OTBlYWMyZjliMTAzNTQwNzViZTdjODQ5NmU3ZmU2NjE5NTY2MzA4Ng==
5
5
  data.tar.gz: !binary |-
6
- ZThiODk3ZDc2ZTY1NGIzN2U1YWE2MWNhMjY4M2RiZDMxMjdmMzdiYg==
6
+ Yzk5ZDEyZDIwODI1Zjc1ZTg4NDQ3YWY2YzBjOTBkZGFmMDdlYTI4Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjI5YjIyNmRhNWRlOTNlMGI5OGM1ZDIwZmFjM2FmZTk1NjgyMWZmZTJiNzIz
10
- NzJiOTdkZWQ4ZTRkMzc1MTRiMzMzZmJlNmNlZmZlMWVmNGNjYmI5OWE1OWJh
11
- MjM3NDcxZTgwODMzOTU1NDcyZmUxY2E3ODQzNGU3YjA4NGNkODU=
9
+ MDgyZTViNTYzZWU4OTU3Mzg0NTczMDVkOGJjZjAwOTUxNzMyMzgwYjdmYThk
10
+ ZmE5MGU3MDFiOGYxOGZjYjNhODkxMGI5MWViYmE4NWQ0YmY3YTc0ZDgyNmJi
11
+ MDNhOGMzZDBhMDYwNmU4MzliYzQ0NTYxZGQwZTRlNDE4NjUyNmE=
12
12
  data.tar.gz: !binary |-
13
- OTc4ZjA5NTUzZWFhNDczMzYxNGFiMjE5Mzc3ZjgwZjQ5ZGEyYmM1ZWRkOWEx
14
- MDM0ZTRhMzMxNzIyMTEzNzE4OGUyZGQ1NzcxOTk2OWFjNDlkODkyN2M2YTAw
15
- MDZkOWM3MzdmN2MwMjJiODE2ZjE5N2UxMGE5MGU1ZTc4Mjk1MGM=
13
+ MzA1NTZkM2FjNWE4NGY3YmQ2ZWEyNGQ3ODk3MDUwMDNmY2Y2ZGE1OTA0NDY5
14
+ ZmE3ZWI1Mzk5MmFkOWZlNDU0NjkzYjU1NWQyZjZmNmQ3OGYwYWUyMWFhZWY4
15
+ ODM4YTU0ZWViZGViZDM5MzY4ZDMwN2FkZWM5OTIxY2MzMTI0Zjc=
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ [![Build Status](https://drone.io/github.com/DanielMSchmidt/dtv_tournaments/status.png)](https://drone.io/github.com/DanielMSchmidt/dtv_tournaments/latest)
2
+ [![Gem Version](https://badge.fury.io/rb/dtv_tournaments.svg)](http://badge.fury.io/rb/dtv_tournaments)
3
+ [![Code Climate](https://codeclimate.com/github/DanielMSchmidt/dtv_tournaments.png)](https://codeclimate.com/github/DanielMSchmidt/dtv_tournaments)
4
+
1
5
  # DtvTournaments
2
6
 
3
7
  A ruby gem for fetching tournaments from the dtv tournaments portal. The gem only works with rails, because it's using the rails caching methods. Later on, they may be configured in a config file, so rails won't be necessary anymore.
@@ -18,31 +22,24 @@ Or install it yourself as:
18
22
 
19
23
  ## Usage
20
24
 
21
- Just call ``DtvTournaments.get_by_number(number, options={})`` and you will get a hash like this returned
25
+ Call ``DtvTournaments::Tournament.new(number)`` to get an tournament instance with the the attributes
22
26
 
23
- {
24
- zip: ...,
25
- city: ...,
26
- street: ...,
27
- number: ...,
28
- time: ...,
29
- date: ...,
30
- datetime: ...,
31
- kind: ...,
32
- ageset: ...
33
- }
27
+ - number
28
+ - date / time / datetime
29
+ - street
30
+ - zip
31
+ - city
32
+ - kind
33
+ - notes
34
34
 
35
- The default options are
35
+ It provides also the methods
36
36
 
37
- {
38
- cached: false, # If you want to get an uncached version but don't override the cached version
39
- rerun: false # If you want to override the cache
40
- }
37
+ - rerun (to rerun the fetching process and update the cache)
41
38
 
42
39
 
43
40
  ## Todos
44
- - migrate old code to project
45
- - test old code
41
+ - fix that street and city is found by searching at the all tournaments page for the date and search there for the given number
42
+ - add tournament specific methods (is placing, get points, ...)
46
43
 
47
44
 
48
45
  ## Contributing
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
-
2
+ task :c => :console
3
3
  task :console do
4
4
  require 'irb'
5
5
  require 'irb/completion'
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "bundler", ">= 1.0"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "codeclimate-test-reporter"
24
25
 
25
- spec.add_runtime_dependency "mechanize", "~> 2.7.2"
26
- spec.add_runtime_dependency "rails"
26
+ spec.add_runtime_dependency "mechanize", ">= 2.0"
27
27
  end
@@ -0,0 +1,104 @@
1
+ require 'mechanize'
2
+
3
+ module DTVTournaments
4
+ class Tournament
5
+ attr_accessor :number, :notes, :date, :time, :datetime, :street, :zip, :city, :kind, :page
6
+ def initialize number
7
+ @number = number
8
+ call
9
+ end
10
+
11
+ def rerun
12
+ call false
13
+ end
14
+
15
+ def call cached= true
16
+ # TODO: Support cached option later
17
+ get_result_page
18
+ extract_results
19
+ end
20
+
21
+ def get_result_page
22
+ mechanize = Mechanize.new { |agent|
23
+ agent.read_timeout = 3
24
+ }
25
+
26
+ mechanize.get(DTVTournaments::FETCHINGURL)
27
+ search_form = mechanize.page.forms.last
28
+
29
+ search_form.nr = @number
30
+ search_form.submit
31
+
32
+ @page = mechanize.page
33
+ end
34
+
35
+ def extract_results
36
+ extract_kind
37
+ extract_notes
38
+ extract_location
39
+ extract_time
40
+ end
41
+
42
+ def extract_kind
43
+ @kind = DTVTournaments::convertToText(page.search(".markierung .turnier"))
44
+ end
45
+
46
+ def extract_notes
47
+ @notes = page.search(".turniere tr .bemerkung").to_a.collect(&:text).reject{|x| x.nil? || x.empty?}.join("\n")
48
+ end
49
+
50
+ def extract_location
51
+ # TODO
52
+ end
53
+
54
+ def extract_time
55
+ if page.search(".markierung .uhrzeit").first.text.empty?
56
+ # This tournament is a big one
57
+ time = get_time_from_big_tournament(page.search(".turniere tr"))
58
+ else
59
+ time = page.search(".markierung .uhrzeit").text.scan(/\d{1,2}:\d{2}/).first
60
+ end
61
+ date = page.search(".kategorie").text.scan(/^\d{1,2}.\d{1,2}.\d{4}/).first
62
+ parse_time date, time
63
+ end
64
+
65
+ def parse_time date, time
66
+ @datetime = DateTime.parse("#{date} #{time}")
67
+ @date = Date.parse(date)
68
+ @time = Time.parse("#{date} #{time}")
69
+ end
70
+
71
+ def get_index_of_marked_tournament tournaments
72
+ tournaments.each_with_index do |single_tournament, index|
73
+ return index if single_tournament.attributes().has_key?('class')
74
+ end
75
+ end
76
+
77
+ def get_first_time_until times, index
78
+ for i in (0..index).to_a.reverse
79
+ return times[i] unless times[i].nil? || times[i].empty?
80
+ end
81
+ end
82
+
83
+ def get_time_from_big_tournament tournaments
84
+
85
+ times = tournaments.map do |single_tournament|
86
+ next_time = DTVTournaments::get_subelement_if_available(single_tournament, ".uhrzeit")
87
+
88
+ if next_time.nil? || next_time.empty?
89
+ nil
90
+ else
91
+ next_time.scan(/\d{1,2}:\d{2}/).first
92
+ end
93
+ end
94
+
95
+ index = get_index_of_marked_tournament tournaments
96
+ get_first_time_until(times, index)
97
+ end
98
+ end
99
+ end
100
+
101
+
102
+ __END__
103
+
104
+ DTVTournaments.Tournament.new 38542
@@ -0,0 +1,20 @@
1
+ # Constants and helper
2
+ module DTVTournaments
3
+ FETCHINGURL = "http://appsrv.tanzsport.de/td/db/turnier/einzel/suche"
4
+
5
+ def self.convertToText content
6
+ if content.class == String
7
+ content
8
+ else
9
+ content.text
10
+ end
11
+ end
12
+
13
+ def self.get_subelement_if_available(element, selector)
14
+ unless element.search(selector).first.nil?
15
+ element.search(selector).first.text
16
+ else
17
+ nil
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module DtvTournaments
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,9 +1,3 @@
1
1
  require "dtv_tournaments/version"
2
- require "dtv_tournaments/number_fetcher"
3
-
4
- # Proxy object
5
- module DtvTournaments
6
- def self.get_by_number number, options={}
7
- NumberFetcher.get_by_number number, options
8
- end
9
- end
2
+ require "dtv_tournaments/utilities"
3
+ require "dtv_tournaments/tournament"
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require 'bundler/setup'
2
5
  Bundler.setup
3
6
 
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ describe DTVTournaments::Tournament do
3
+ describe "results" do
4
+ describe "large tournaments" do
5
+ before(:all) do
6
+ @t = DTVTournaments::Tournament.new(40472)
7
+ end
8
+
9
+ it "should have the right date" do
10
+ expect(@t.date.to_s).to eq(Date.parse('20.04.2014').to_s)
11
+ expect(@t.time.hour).to eq(Time.parse('09:00').hour)
12
+ expect(@t.time.min).to eq(Time.parse('09:00').min)
13
+ expect(@t.datetime.to_s).to eq(DateTime.parse('20.04.2014 09:00').to_s)
14
+ end
15
+
16
+ it "should have the right address"
17
+ it "should have the right kind" do
18
+ expect(@t.kind).to eq('HGR C LAT')
19
+ end
20
+ end
21
+
22
+ describe "small tournaments" do
23
+ before(:all) do
24
+ @t = DTVTournaments::Tournament.new(38542)
25
+ end
26
+
27
+ it "should have the right date" do
28
+ expect(@t.date.to_s).to eq(Date.parse('29.03.2014').to_s)
29
+ expect(@t.time.hour).to eq(Time.parse('15:30').hour)
30
+ expect(@t.time.min).to eq(Time.parse('15:30').min)
31
+ expect(@t.datetime.to_s).to eq(DateTime.parse('29.03.2014 15:30').to_s)
32
+ end
33
+
34
+ it "should have the right address"
35
+ it "should have the right kind" do
36
+ expect(@t.kind).to eq('HGR D ST')
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "options" do
42
+ it "should not take the cache if rerun is passed"
43
+ it "should update the cache if rerun is passed"
44
+ it "should not take cached if cached isn't passed"
45
+ it "should take the cached if cached is"
46
+ end
47
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dtv_tournaments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schmidt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-14 00:00:00.000000000 Z
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '1.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -53,33 +53,33 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: mechanize
56
+ name: codeclimate-test-reporter
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 2.7.2
62
- type: :runtime
61
+ version: '0'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
- version: 2.7.2
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rails
70
+ name: mechanize
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ! '>='
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '2.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ! '>='
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '2.0'
83
83
  description: This gem fetches the appsrv.tanzsport.de/dtv-webdbs/turnier/suche.spf
84
84
  portal and gives all available informations about the tournaments
85
85
  email:
@@ -96,10 +96,11 @@ files:
96
96
  - Rakefile
97
97
  - dtv_tournaments.gemspec
98
98
  - lib/dtv_tournaments.rb
99
- - lib/dtv_tournaments/number_fetcher.rb
99
+ - lib/dtv_tournaments/tournament.rb
100
+ - lib/dtv_tournaments/utilities.rb
100
101
  - lib/dtv_tournaments/version.rb
101
- - spec/number_fetcher_spec.rb
102
102
  - spec/spec_helper.rb
103
+ - spec/tournament_spec.rb
103
104
  homepage: https://github.com/DanielMSchmidt/dtv_tournaments
104
105
  licenses:
105
106
  - MIT
@@ -125,5 +126,5 @@ signing_key:
125
126
  specification_version: 4
126
127
  summary: A ruby gem for fetching tournaments from the dtv tournaments portal
127
128
  test_files:
128
- - spec/number_fetcher_spec.rb
129
129
  - spec/spec_helper.rb
130
+ - spec/tournament_spec.rb
@@ -1,141 +0,0 @@
1
- require 'mechanize'
2
-
3
- module NumberFetcher
4
- attr_accessor :number, :rerun, :cached
5
- PREFIX = "tr"
6
- FETCHINGURL = "http://appsrv.tanzsport.de/td/db/turnier/einzel/suche"
7
-
8
- def self.get_by_number number, options={}
9
- @number = number
10
- @rerun = options[:rerun] || false
11
- @cached = options[:cached] || false
12
-
13
- # Rails.cache.delete("#{NumberFetcher::PREFIX}-#{@number}") if @rerun
14
-
15
- if @cached
16
- NumberFetcher::get_cached_tournament_data
17
- else
18
- get_tournament_data
19
- end
20
- end
21
-
22
- def self.get_cached_tournament_data
23
- #return Rails.cache.fetch("#{NumberFetcher::PREFIX}-#{@number}") do
24
- self.get_tournament_data
25
- #end
26
- end
27
-
28
- def self.get_tournament_data
29
- puts "Tournament data is fetching for #{@number}"
30
- hash = NumberFetcher::extract_results(NumberFetcher::get_result_page(@number))
31
- hash[:number] = @number
32
- hash
33
- end
34
-
35
- def self.get_result_page(number)
36
- mechanize = Mechanize.new { |agent|
37
- agent.read_timeout = 3
38
- }
39
-
40
- mechanize.get(NumberFetcher::FETCHINGURL)
41
- search_form = mechanize.page.forms.last
42
-
43
- search_form.nr = number
44
- search_form.submit
45
-
46
- mechanize.page
47
- end
48
-
49
- def self.extract_results page
50
- raw_data = NumberFetcher::get_raw_data(page)
51
- extracted_results = {}
52
- raw_data.each do |key, value|
53
- extracted_results[key] = send("extract_#{key}", value) unless value.nil?
54
- end
55
- create_time_and_date extracted_results
56
- end
57
-
58
- def self.create_time_and_date hash
59
- time = hash.delete(:time)
60
- date = hash.delete(:date)
61
- hash[:datetime] = DateTime.parse("#{date} #{time}")
62
- hash[:date] = Date.parse(date)
63
- hash[:time] = Time.parse("#{date} #{time}")
64
- hash
65
- end
66
-
67
- def self.extract_kind(content)
68
- NumberFetcher::convert_to_text(content)
69
- end
70
-
71
- def self.extract_date(content)
72
- NumberFetcher::convert_to_text(content).scan(/^\d{1,2}.\d{1,2}.\d{4}/).first
73
- end
74
-
75
- def self.extract_time(content)
76
- NumberFetcher::convert_to_text(content).scan(/\d{1,2}:\d{2}/).first
77
- end
78
-
79
- def self.extract_notes(content)
80
- NumberFetcher::convert_to_text(content)
81
- end
82
-
83
- def self.extract_street(content)
84
- NumberFetcher::convert_to_text(content).split("\t").join("").split("\n").delete_if{|x| x.empty? }.join(" ")
85
- end
86
-
87
- def self.extract_zip(content)
88
- content.text.gsub(/(\W)/, "").gsub(/(\D)/, "")
89
- end
90
-
91
- def self.extract_city(content)
92
- content.text.gsub(/(\W)/, "").gsub(/(\d)/, "")
93
- end
94
-
95
- def self.convert_to_text(content)
96
- if content.class == String
97
- content
98
- else
99
- content.text
100
- end
101
- end
102
-
103
- def self.get_raw_data(page)
104
- #Test id its a multiline tournament
105
- if page.search(".markierung .uhrzeit").first.text.empty?
106
- puts "This Tournament seems to be a big one: #{@number}"
107
-
108
- page.search(".turniere tr").each do |single_tournament|
109
- next_kind = NumberFetcher::get_subelement_if_available(single_tournament, ".turnier")
110
- kind = next_kind unless next_kind.nil? || next_kind.empty?
111
-
112
- next_time = NumberFetcher::get_subelement_if_available(single_tournament, ".uhrzeit")
113
- time = next_time unless next_time.nil? || next_time.empty?
114
-
115
- break if single_tournament.attributes().has_key?('class')
116
- end
117
- else
118
- kind = page.search(".markierung .turnier")
119
- time = page.search(".markierung .uhrzeit")
120
- end
121
- notes = page.search(".turniere tr .bemerkung").to_a.collect(&:text).reject{|x| x.nil? || x.empty?}.join("\n")
122
- date = page.search(".kategorie")
123
- street = page.search(".ort")
124
- street.search("strong").remove # remove zip and city from field
125
- zip = page.search(".ort strong")
126
- city = page.search(".ort strong")
127
- { kind: kind, date: date, time: time, notes: notes, street: street, zip: zip, city: city }
128
- end
129
-
130
- def self.get_subelement_if_available(element, selector)
131
- unless element.search(selector).first.nil?
132
- element.search(selector).first.text
133
- else
134
- nil
135
- end
136
- end
137
- end
138
-
139
- __END__
140
-
141
- DtvTournaments.get_by_number(38543)
@@ -1,4 +0,0 @@
1
- require 'spec_helper'
2
- describe NumberFetcher do
3
- pending "write it"
4
- end