kisaweb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Jarkko Laine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,27 @@
1
+ Kisaweb
2
+ =======
3
+
4
+ Kisaweb on Ruby-kirjasto yhteydenpitoon Suomen Suunnistusliiton Kisawebin API:n kanssa.
5
+
6
+ Esimerkki:
7
+
8
+ Kisaweb::OnlineRanki.find("D16")
9
+
10
+ hakee D16-sarjan tämänhetkisen online-rankilistan.
11
+
12
+ include Kisaweb
13
+
14
+ OnlineRanki.find("D16").first.runner.first_name
15
+ => "Emmi"
16
+
17
+ OnlineRanki.find("D16").first.runs.map do |run|
18
+ [run.contest.title, run.points.to_f]
19
+ end
20
+ => [["SM-keskimatka R1", 76.45],
21
+ ["SM-pitkä matka R1", 73.22],
22
+ ["Oravatonni", 70.83],
23
+ ["SM-pitkä matka R1", 70.63],
24
+ ["Häme-AM", 70.51]]
25
+
26
+
27
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "kisaweb"
5
+ gemspec.summary = "Ruby client for SSL Kisaweb API"
6
+ gemspec.description = "Ruby Gem for interfacing with the SSL Kisaweb API"
7
+ gemspec.email = "jarkko@jlaine.net"
8
+ gemspec.homepage = "http://github.com/jarkko/kisaweb"
9
+ gemspec.authors = ["Jarkko Laine"]
10
+
11
+ gemspec.add_dependency('pauldix-typhoeus', '>=0.1.2')
12
+ gemspec.add_dependency('fastercsv', '>=1.5.0')
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
16
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/kisaweb.gemspec ADDED
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{kisaweb}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jarkko Laine"]
12
+ s.date = %q{2009-09-30}
13
+ s.description = %q{Ruby Gem for interfacing with the SSL Kisaweb API}
14
+ s.email = %q{jarkko@jlaine.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "kisaweb.gemspec",
26
+ "lib/kisaweb.rb",
27
+ "lib/kisaweb/base.rb",
28
+ "lib/kisaweb/contest.rb",
29
+ "lib/kisaweb/list_item.rb",
30
+ "lib/kisaweb/online_ranki.rb",
31
+ "lib/kisaweb/run.rb",
32
+ "lib/kisaweb/runner.rb",
33
+ "spec/contest_spec.rb",
34
+ "spec/list_item_spec.rb",
35
+ "spec/run_spec.rb",
36
+ "spec/runner_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/jarkko/kisaweb}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.5}
43
+ s.summary = %q{Ruby client for SSL Kisaweb API}
44
+ s.test_files = [
45
+ "spec/contest_spec.rb",
46
+ "spec/list_item_spec.rb",
47
+ "spec/run_spec.rb",
48
+ "spec/runner_spec.rb",
49
+ "spec/spec_helper.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_runtime_dependency(%q<pauldix-typhoeus>, [">= 0.1.2"])
58
+ s.add_runtime_dependency(%q<fastercsv>, [">= 1.5.0"])
59
+ else
60
+ s.add_dependency(%q<pauldix-typhoeus>, [">= 0.1.2"])
61
+ s.add_dependency(%q<fastercsv>, [">= 1.5.0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<pauldix-typhoeus>, [">= 0.1.2"])
65
+ s.add_dependency(%q<fastercsv>, [">= 1.5.0"])
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Kisaweb
2
+ class Base
3
+ include ::Typhoeus
4
+
5
+ remote_defaults :base_uri => "http://kisaweb.ssl.fi/base"
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ module Kisaweb
2
+ class Contest < Kisaweb::Base
3
+ attr_accessor :title, :date, :organizer, :code
4
+
5
+ def self.find(code)
6
+ all.select do |contest|
7
+ contest.code == code
8
+ end.first
9
+ end
10
+
11
+ def self.all
12
+ @@all ||= find_all
13
+ end
14
+
15
+ def self.from_csv_array(arr)
16
+ contest = new
17
+ contest.title = arr[1]
18
+
19
+ contest.date = parse_date(arr[0])
20
+
21
+ contest.organizer = arr.last.strip
22
+ contest.code = parse_code(arr[0])
23
+
24
+ contest
25
+ end
26
+
27
+ private
28
+
29
+ def self.find_all
30
+ res = fetch_all
31
+ res_arr = FasterCSV.parse(res.body)
32
+ res_arr.map do |row|
33
+ Contest.from_csv_array(row)
34
+ end
35
+ end
36
+
37
+ def self.parse_date(str)
38
+ Date.parse(str.strip.split(" ").last) rescue nil
39
+ end
40
+
41
+ def self.parse_code(str)
42
+ str.match(/\(\d+\)|\- \d+/)[0].gsub(/[()-]|\s/, "")
43
+ end
44
+
45
+ define_remote_method :fetch_all, :path => "/ranki/rankilla.txt"
46
+ end
47
+ end
@@ -0,0 +1,21 @@
1
+ module Kisaweb
2
+ class ListItem
3
+ attr_accessor :runner, :position, :points, :runs
4
+
5
+ def self.from_array(arr)
6
+ item = new
7
+ item.position = arr.first.to_i
8
+ item.points = BigDecimal.new(arr.last)
9
+ item.runner = Runner.new(*arr[1, 4])
10
+ item.runs = []
11
+
12
+ 5.step(13, 2) do |i|
13
+ next unless arr[i]
14
+
15
+ item.runs << Run.new(arr[i], arr[i + 1])
16
+ end
17
+
18
+ item
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module Kisaweb
2
+ class OnlineRanki < Kisaweb::Base
3
+ def self.find(klass)
4
+ res = fetch_class(:klass => klass)
5
+ res_arr = FasterCSV.parse(res.body)
6
+ res_arr.shift
7
+ res_arr.map do |row|
8
+ ListItem.from_array(row)
9
+ end
10
+ end
11
+
12
+ define_remote_method :fetch_class, :path => "/ranki/:klass.txt"
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Kisaweb
2
+ class Run
3
+ attr_accessor :contest_id, :points
4
+
5
+ def initialize(contest_id, points)
6
+ self.contest_id = contest_id
7
+ self.points = BigDecimal.new(points)
8
+ end
9
+
10
+ def contest
11
+ @contest ||= Kisaweb::Contest.find(contest_id)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Kisaweb
2
+ class Runner
3
+ attr_accessor :first_name, :last_name, :club, :license_number
4
+
5
+ def initialize(*args)
6
+ self.last_name, self.first_name, self.club, self.license_number = *args
7
+ end
8
+ end
9
+ end
data/lib/kisaweb.rb ADDED
@@ -0,0 +1,21 @@
1
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
2
+
3
+ $KCODE = "u"
4
+
5
+ # stdlib
6
+ require "bigdecimal"
7
+
8
+ # third party
9
+ require 'rubygems'
10
+ gem 'pauldix-typhoeus', ">=0.1.2"
11
+ gem 'fastercsv', ">=1.5.0"
12
+ require 'faster_csv'
13
+ require 'typhoeus'
14
+
15
+ # internal requires
16
+ require 'kisaweb/base'
17
+ require 'kisaweb/online_ranki'
18
+ require 'kisaweb/list_item'
19
+ require 'kisaweb/runner'
20
+ require 'kisaweb/run'
21
+ require 'kisaweb/contest'
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Kisaweb::Contest do
4
+ describe "from_csv_array" do
5
+ context "normal lines" do
6
+ before(:each) do
7
+ @line = ["(9399) 12.4.2009 ", "Saaristorastit", " Pargas IF "]
8
+ @contest = Kisaweb::Contest.from_csv_array(@line)
9
+ end
10
+
11
+ it "should set title correctly" do
12
+ @contest.title.should == "Saaristorastit"
13
+ end
14
+
15
+ it "should set the date correctly" do
16
+ @contest.date.should == Date.new(2009,4,12)
17
+ end
18
+
19
+ it "should set the organizer correctly" do
20
+ @contest.organizer.should == "Pargas IF"
21
+ end
22
+
23
+ it "should set the code correctly" do
24
+ @contest.code.should == "9399"
25
+ end
26
+ end
27
+
28
+ context "weirdo lines" do
29
+ before(:each) do
30
+ @line = ["- 9589", "EP am-keskimatka", "RasKu "]
31
+ @contest = Kisaweb::Contest.from_csv_array(@line)
32
+ end
33
+
34
+ it "should set title correctly" do
35
+ @contest.title.should == "EP am-keskimatka"
36
+ end
37
+
38
+ it "should set the date correctly" do
39
+ @contest.date.should be_nil
40
+ end
41
+
42
+ it "should set the organizer correctly" do
43
+ @contest.organizer.should == "RasKu"
44
+ end
45
+
46
+ it "should set the code correctly" do
47
+ @contest.code.should == "9589"
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Kisaweb::ListItem do
4
+ context "from_array" do
5
+ before(:each) do
6
+ @arr = ["189", "Paavola", "Niko",
7
+ "HimU", "1223", "9444",
8
+ "35.65", "9568", "28.61",
9
+ nil, nil, nil, nil, nil, nil,
10
+ "70"]
11
+ @item = Kisaweb::ListItem.from_array(@arr)
12
+ end
13
+
14
+ it "should create runner with correct details" do
15
+ @item.runner.first_name.should == "Niko"
16
+ @item.runner.last_name.should == "Paavola"
17
+ @item.runner.club.should == "HimU"
18
+ @item.runner.license_number.should == "1223"
19
+ end
20
+
21
+ it "should set position correctly" do
22
+ @item.position.should == 189
23
+ end
24
+
25
+ it "should set points correctly" do
26
+ @item.points.should == BigDecimal.new("70")
27
+ @item.points.to_i.should == 70
28
+ end
29
+
30
+ it "should create runs correctly" do
31
+ @item = Kisaweb::ListItem.from_array(@arr)
32
+ @item.runs.map{|i| [i.contest_id, i.points]}.should ==
33
+ [["9444", BigDecimal.new("35.65")],
34
+ ["9568", BigDecimal.new("28.61")]]
35
+ end
36
+ end
37
+ end
data/spec/run_spec.rb ADDED
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Kisaweb::Run do
4
+ context "new" do
5
+ before(:each) do
6
+ @run = Kisaweb::Run.new("9444", "25.65")
7
+ end
8
+
9
+ it "should set the contest_id and points" do
10
+ @run.contest_id.should == "9444"
11
+ @run.points.should == BigDecimal.new("25.65")
12
+ end
13
+
14
+ describe "contest" do
15
+ context "when @contest is nil" do
16
+ it "should fetch contest by the contest_id" do
17
+ Kisaweb::Contest.should_receive(:find).with("9444").and_return(:contest)
18
+ @run.contest.should == :contest
19
+ end
20
+ end
21
+
22
+ context "when @contest is set" do
23
+ before(:each) do
24
+ @run.instance_variable_set("@contest", :contest)
25
+ end
26
+
27
+ it "should use the memoized contest" do
28
+ Kisaweb::Contest.should_not_receive(:find)
29
+ @run.contest.should == :contest
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Kisaweb::Runner do
4
+ context "new" do
5
+ before(:each) do
6
+ @runner = Kisaweb::Runner.new("last", "first", "club", "12345")
7
+ end
8
+
9
+ it "should set the attributes correctly" do
10
+ @runner.first_name.should == "first"
11
+ @runner.last_name.should == "last"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + '/../kisaweb'
2
+ gem 'rspec'
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kisaweb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jarkko Laine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-30 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: pauldix-typhoeus
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: fastercsv
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.0
34
+ version:
35
+ description: Ruby Gem for interfacing with the SSL Kisaweb API
36
+ email: jarkko@jlaine.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.markdown
44
+ files:
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.markdown
48
+ - Rakefile
49
+ - VERSION
50
+ - kisaweb.gemspec
51
+ - lib/kisaweb.rb
52
+ - lib/kisaweb/base.rb
53
+ - lib/kisaweb/contest.rb
54
+ - lib/kisaweb/list_item.rb
55
+ - lib/kisaweb/online_ranki.rb
56
+ - lib/kisaweb/run.rb
57
+ - lib/kisaweb/runner.rb
58
+ - spec/contest_spec.rb
59
+ - spec/list_item_spec.rb
60
+ - spec/run_spec.rb
61
+ - spec/runner_spec.rb
62
+ - spec/spec_helper.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/jarkko/kisaweb
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.5
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Ruby client for SSL Kisaweb API
91
+ test_files:
92
+ - spec/contest_spec.rb
93
+ - spec/list_item_spec.rb
94
+ - spec/run_spec.rb
95
+ - spec/runner_spec.rb
96
+ - spec/spec_helper.rb