squash_matrix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c24a61db8c036d8b4dbf10aed040446a6d3f2eb
4
+ data.tar.gz: ecfdb5e030bcedd8ee643a9f1caacb33d21b5872
5
+ SHA512:
6
+ metadata.gz: 31ab76413209e60a0cdd66effad75366d04209db305b9650ac58334412664a8518fa1b053a146f1924e30d0adcbc749769b4d1b71502827f93f7dba419a4b849
7
+ data.tar.gz: 2f41bf2fa14b77c71b997810d6a8e0502d7f663da4f95ad364d2173176bd761b7c8669f616810e1ede857e707205f297578daf8610f93ac95f193582f6e19cb2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at joshua@wilkosz.com.au. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in squash_matrix.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ squash_matrix (0.1.0)
5
+ nokogiri (~> 1.8.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.8.2)
13
+ mini_portile2 (2.3.0)
14
+ nokogiri (1.8.4)
15
+ mini_portile2 (~> 2.3.0)
16
+ pry (0.10.4)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.8.1)
19
+ slop (~> 3.4)
20
+ rake (10.5.0)
21
+ rspec (3.7.0)
22
+ rspec-core (~> 3.7.0)
23
+ rspec-expectations (~> 3.7.0)
24
+ rspec-mocks (~> 3.7.0)
25
+ rspec-core (3.7.1)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-expectations (3.7.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-mocks (3.7.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-support (3.7.1)
34
+ slop (3.6.0)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.16)
41
+ pry (~> 0.10.3)
42
+ rake (~> 10.0)
43
+ rspec (~> 3.0)
44
+ squash_matrix!
45
+
46
+ BUNDLED WITH
47
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Joshua Wilkosz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # SquashMatrix
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/squash_matrix`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'squash_matrix'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install squash_matrix
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/squash_matrix. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the SquashMatrix project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/wilkosz/squash_matrix/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "squash_matrix"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+ require "pry"
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require 'squash_matrix/version'
2
+ require_relative 'squash_matrix/client'
3
+
4
+ module SquashMatrix; end
@@ -0,0 +1,166 @@
1
+ require 'net/http'
2
+ require 'date'
3
+ require 'timeout'
4
+ require_relative 'constants'
5
+ require_relative 'nokogiri-parser'
6
+ require_relative 'errors'
7
+
8
+ module SquashMatrix
9
+
10
+ # Client for http interactions with squashmatrix.com website.
11
+ # If authentication credentials are provided squash matrix will allow
12
+ # considerably more requests for an IP address and allow forbidden conent
13
+ # to be requested.
14
+
15
+ class Client
16
+
17
+ # Returns newly created Client for making club and player requests
18
+ # @note If suppress_errors == false SquashMatrix::Errors::AuthorizationError will be raised if specified credentials are incorrect and squash matrix authentication returns forbidden
19
+ # @param [Hash] opts the options to create client
20
+ # @return [Client]
21
+
22
+ def initialize(player: nil, email: nil, password: nil, suppress_errors: false, timeout: 60)
23
+ @suppress_errors = suppress_errors
24
+ @timeout = timeout
25
+ if ![player || email, password].any?(&:nil?)
26
+ @authenticated = {
27
+ valid: false,
28
+ authenticated_at: nil,
29
+ updated_at: nil,
30
+ cookie: nil,
31
+ player: player,
32
+ email: email,
33
+ password: password
34
+ }
35
+ authenticate
36
+ end
37
+ end
38
+
39
+ # Returns club information.
40
+ # @note If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc...
41
+ # @param id [Numeric] club id found on squash matrix
42
+ # @return [Hash] hash object containing club information
43
+
44
+ def club_info(id=nil)
45
+ uri = URI::HTTP.build({
46
+ host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
47
+ path: SquashMatrix::Constants::CLUB_PATH.gsub(':id', id.to_s)
48
+ })
49
+ success_proc = lambda {|res| SquashMatrix::NokogiriParser.club_info(res.body)}
50
+ handle_http_request(uri, success_proc)
51
+ end
52
+
53
+ # Returns player information.
54
+ # @note If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc...
55
+ # @param id [Numeric] played id found on squash matrix
56
+ # @return [Hash] hash object containing player information
57
+
58
+ def player_info(id=nil)
59
+ return if id.nil?
60
+ uri = URI::HTTP.build({
61
+ host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
62
+ path: SquashMatrix::Constants::PLAYER_PATH.gsub(':id', id.to_s),
63
+ query: SquashMatrix::Constants::PLAYER_RSULTS_QUERY
64
+ })
65
+ success_proc = lambda {|res| SquashMatrix::NokogiriParser.player_info(res.body)}
66
+ handle_http_request(uri, success_proc)
67
+ end
68
+
69
+ # Returns search results
70
+ # @note If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc...
71
+ # @param query [String] search query
72
+ # @return [Hash] hash object containing search results
73
+
74
+ def search(query=nil, squash_only: false, racquetball_only: false)
75
+ return if query.nil? || query.empty?
76
+ uri = URI::HTTP.build({
77
+ host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
78
+ path: SquashMatrix::Constants::SEARCH_PATH})
79
+ query_params = {
80
+ Criteria: query,
81
+ SquashOnly: false,
82
+ RacquetballOnly: false}
83
+ success_proc = lambda {|res| SquashMatrix::NokogiriParser.search_results(res.body)}
84
+ handle_http_request(uri, success_proc,
85
+ {
86
+ is_get_request: false,
87
+ query_params: query_params
88
+ })
89
+ end
90
+
91
+ private
92
+
93
+ def handle_http_request(uri, success_proc, is_get_request: true, query_params: nil, headers: nil)
94
+ begin
95
+ Timeout.timeout(@timeout) do
96
+ if is_get_request
97
+ req = Net::HTTP::Get.new(uri)
98
+ set_headers(req, headers: headers)
99
+ res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req)}
100
+ else
101
+ res = Net::HTTP.post_form(uri, query_params)
102
+ end
103
+ case res
104
+ when Net::HTTPSuccess
105
+ return success_proc && success_proc.call(res) || res
106
+ when Net::HTTPConflict
107
+ raise SquashMatrix::Errors::ForbiddenError.new(res) unless @suppress_errors
108
+ else
109
+ raise SquashMatrix::Errors::UnknownError.new(res) unless @suppress_errors
110
+ end
111
+ end
112
+ rescue Timeout::Error => e
113
+ raise e unless @suppress_errors
114
+ end
115
+ end
116
+
117
+ def authenticate
118
+ return unless @authenticated
119
+ uri = URI::HTTP.build({
120
+ host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
121
+ path: SquashMatrix::Constants::LOGIN_PATH})
122
+ headers = {
123
+ SquashMatrix::Constants::CONTENT_TYPE_HEADER.to_sym => SquashMatrix::Constants::X_WWW__FROM_URL_ENCODED
124
+ }
125
+ query_params = {
126
+ UserName: @authenticated[:player] || @authenticated[:email],
127
+ Password: @authenticated[:password],
128
+ RememberMe: false
129
+ }
130
+ res = handle_http_request(uri, nil,
131
+ {
132
+ is_get_request: false,
133
+ query_params: query_params,
134
+ headers: headers
135
+ })
136
+ return unless res
137
+ @authenticated[:cookie] = res.response[SquashMatrix::Constants::SET_COOKIE_HEADER]
138
+ if auth_token_from_cookie(@authenticated[:cookie])
139
+ @authenticated[:authenticated_at] = Time.now.utc
140
+ @authenticated[:updated_at] = Time.now.utc
141
+ @authenticated[:valid] = true
142
+ @authenticated[:player] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(res.response[SquashMatrix::Constants::LOCATION_HEADER])[1] if @authenticated[:email] && res.response[SquashMatrix::Constants::LOCATION_HEADER]
143
+ elsif !@suppress_errors
144
+ error_string = SquashMatrix::NokogiriParser.log_on_error(res.body).join(', ')
145
+ raise SquashMatrix::Errors::AuthorizationError.new(error_string)
146
+ end
147
+ end
148
+
149
+ def set_headers(req=nil, headers: nil)
150
+ return unless req
151
+ headers_to_add = {}
152
+ headers_to_add.merge(headers) if headers
153
+ headers_to_add.merge({
154
+ Cookie: @authenticated[:cookie],
155
+ Referer: SquashMatrix::Constants::REFERER.gsub(':id', @authenticated[:player]),
156
+ }) if @authenticated
157
+ headers_to_add.each {|key, val| req[key] = val}
158
+ end
159
+
160
+ def auth_token_from_cookie(cookie=nil)
161
+ return unless !cookie.nil? && !cookie.empty?
162
+ rtn = SquashMatrix::Constants::ASPXAUTH_TOKEN_FROM_COOKIE_REGEX.match(cookie)
163
+ rtn[1] if rtn
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,24 @@
1
+
2
+ module SquashMatrix
3
+ module Constants
4
+ SQUASH_MATRIX_URL = "www.squashmatrix.com"
5
+ LOGIN_PATH = "/Account/LogOn"
6
+ PLAYER_PATH = "/Home/PlayerResults/:id"
7
+ CLUB_PATH = "/Home/Club/:id"
8
+ SEARCH_PATH = "/Home/Search"
9
+ PLAYER_RSULTS_QUERY = "max=0&X-Requested-With=XMLHttpRequest"
10
+ SET_COOKIE_HEADER = 'set-cookie'
11
+ LOCATION_HEADER = 'location'
12
+ X_WWW__FROM_URL_ENCODED = 'application/x-www-form-urlencoded'
13
+ MULTIPART_FORM_DATA = 'multipart/form-data'
14
+ CONTENT_TYPE_HEADER = 'Content-Type'
15
+ REFERER = "Home/Player/:id"
16
+
17
+ PLAYER_FROM_PATH_REGEX = /\/Home\/Player\/(.*)/
18
+ TEAM_FROM_PATH_REGEX = /\/Home\/Team\/(.*)/
19
+ CLUB_FROM_PATH_REGEX = /\/Home\/Club\/(.*)/
20
+ MATCH_FROM_PATH_REGEX = /\/Home\/Match\/(.*)/
21
+ CLUB_FROM_TITLE_REGEX = /Club - (.*)/
22
+ ASPXAUTH_TOKEN_FROM_COOKIE_REGEX = /.ASPXAUTH=([a-zA-Z0-9]*);/
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module SquashMatrix
2
+ module Errors
3
+ class AuthorizationError < StandardError; end
4
+ class ForbiddenError < StandardError; end
5
+ class UnknownError < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,125 @@
1
+ require 'nokogiri'
2
+ require_relative 'constants'
3
+
4
+ module SquashMatrix
5
+ class NokogiriParser
6
+ def self.player_info(body)
7
+ Nokogiri::HTML(body)&.xpath('//table[@id="results"]//tbody//tr')&.map do |r|
8
+ date = r.at_css('td[1]')&.content
9
+ opponent_id = r.at_css('td[10]//a')&.attribute('href')&.content
10
+ match_id = r.at_css('td[12]//a')&.attribute('href')&.content
11
+ rtn = {
12
+ event: r.at_css('td[2]')&.content,
13
+ division: r.at_css('td[3]')&.content,
14
+ round: r.at_css('td[4]')&.content,
15
+ position: r.at_css('td[5]')&.content,
16
+ games: r.at_css('td[6]')&.content,
17
+ points: r.at_css('td[7]')&.content,
18
+ rating_adjustment: r.at_css('td[8]')&.content,
19
+ rating: r.at_css('td[9]')&.content,
20
+ opponent_rating: r.at_css('td[11]')&.content,
21
+ opponent_name: r.at_css('td[10]//a')&.content
22
+ }
23
+ rtn[:date] = Date.parse(date) if date
24
+ rtn[:opponent_id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(opponent_id)[1] if opponent_id
25
+ rtn[:match_id] = SquashMatrix::Constants::MATCH_FROM_PATH_REGEX.match(match_id)[1] if match_id
26
+ rtn.values.any?(&:nil?) ? nil : rtn
27
+ end.compact
28
+ end
29
+
30
+ def self.club_info(body)
31
+ html = Nokogiri::HTML(body)
32
+ name = SquashMatrix::Constants::CLUB_FROM_TITLE_REGEX.match(html.css('title').text)[1]
33
+ players = html.xpath('//div[@id="Rankings"]//div[@class="columnmain"]//table[@class="alternaterows"]//tbody//tr')&.map do |r|
34
+ player_path = r.css('td[2]//a').attribute('href').value
35
+ rank = r.css('td[1]').text
36
+ rtn = {
37
+ name: r.css('td[2]').text,
38
+ rating: r.css('td[3]').text.to_f
39
+ }
40
+ rtn[:rank] = rank.to_i if rank
41
+ rtn[:id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(player_path)[1] if player_path
42
+ rtn
43
+ end.compact
44
+ juniors = html.xpath('//div[@id="Rankings"]//div[@class="columnside"]//table[@class="alternaterows"]//tbody//tr')&.map do |r|
45
+ player_path = r.css('td[2]//a').attribute('href').value
46
+ rank = r.css('td[1]').text
47
+ rtn = {
48
+ name: r.css('td[2]').text,
49
+ rating: r.css('td[3]').text.to_f
50
+ }
51
+ rtn[:rank] = rank.to_i if rank
52
+ rtn[:id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(player_path)[1] if player_path
53
+ rtn
54
+ end.compact
55
+ {
56
+ name: name,
57
+ players: players,
58
+ juniors: juniors
59
+ }
60
+ end
61
+
62
+ def self.search_results(body)
63
+ bc = Nokogiri::HTML.parse(body).at_xpath('//div[@id="bodycontent"]')&.children
64
+ return unless bc&.length
65
+ rtn = {}
66
+ bc&.each_with_index do |c, i|
67
+ if c.name == "h2"
68
+ case c.text
69
+ when "Players"
70
+ rtn[:players] = players_from_search(bc[i+2]) if bc[i+2].children.length
71
+ when "Teams"
72
+ rtn[:teams] = teams_from_search(bc[i+2]) if bc[i+2].children.length
73
+ when "Clubs"
74
+ rtn[:clubs] = clubs_from_search(bc[i+2]) if bc[i+2].children.length
75
+ end
76
+ end
77
+ end
78
+ rtn
79
+ end
80
+
81
+ def self.log_on_error(body)
82
+ Nokogiri::HTML(body)&.xpath('//div[@class="validation-summary-errors"]//ul//li')&.map(&:content)
83
+ end
84
+
85
+ def self.players_from_search(node)
86
+ node.css('tbody').css('tr')&.map do |tr|
87
+ id = tr.css('td[1]//a')&.attribute('href')&.value
88
+ rating = tr.css('td[3]')&.text
89
+ rtn = {
90
+ name: tr.css('td[1]')&.text,
91
+ club_name: tr.css('td[2]')&.text
92
+ }
93
+ rtn[:id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(id)[1].to_i if id && !id.empty?
94
+ rtn[:rating] = rating.to_f if rating
95
+ rtn
96
+ end
97
+ end
98
+
99
+ def self.teams_from_search(node)
100
+ node.css('tbody').css('tr')&.map do |tr|
101
+ id = tr.css('td[1]//a')&.attribute('href')&.value
102
+ rtn = {
103
+ name: tr.css('td[1]')&.text,
104
+ division_name: tr.css('td[2]')&.text,
105
+ event_name: tr.css('td[3]')&.text
106
+ }
107
+ rtn[:id] = SquashMatrix::Constants::TEAM_FROM_PATH_REGEX.match(id)[1].to_i if id
108
+ rtn
109
+ end
110
+ end
111
+
112
+ def self.clubs_from_search(node)
113
+ node.css('tbody').css('tr')&.map do |tr|
114
+ puts tr
115
+ id = tr.css('td[1]//a')&.attribute('href')&.value
116
+ rtn = {
117
+ name: tr.css('td[1]')&.text,
118
+ state: tr.css('td[2]')&.text
119
+ }
120
+ rtn[:id] = SquashMatrix::Constants::CLUB_FROM_PATH_REGEX.match(id)[1].to_i if id
121
+ rtn
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,3 @@
1
+ module SquashMatrix
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "squash_matrix/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "squash_matrix"
8
+ spec.version = SquashMatrix::VERSION
9
+ spec.authors = ["Joshua Wilkosz"]
10
+ spec.email = ["joshua@wilkosz.com.au"]
11
+
12
+ spec.summary = %q{Client for squashmatrix.com http request/response}
13
+ spec.description = %q{User for retrieving information on squashmatrix.com}
14
+ spec.homepage = "https://github.com/wilkosz/squash_matrix"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "pry", "~> 0.10.3"
39
+
40
+ spec.add_runtime_dependency 'nokogiri', '~> 1.8', '>= 1.8.4'
41
+ spec.required_ruby_version = '>= 2.3.0'
42
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: squash_matrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Wilkosz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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.10.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 1.8.4
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '1.8'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.8.4
89
+ description: User for retrieving information on squashmatrix.com
90
+ email:
91
+ - joshua@wilkosz.com.au
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".travis.yml"
99
+ - CODE_OF_CONDUCT.md
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - bin/console
106
+ - bin/setup
107
+ - lib/squash_matrix.rb
108
+ - lib/squash_matrix/client.rb
109
+ - lib/squash_matrix/constants.rb
110
+ - lib/squash_matrix/errors.rb
111
+ - lib/squash_matrix/nokogiri-parser.rb
112
+ - lib/squash_matrix/version.rb
113
+ - squash_matrix.gemspec
114
+ homepage: https://github.com/wilkosz/squash_matrix
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ allowed_push_host: https://rubygems.org
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: 2.3.0
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.6.14.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Client for squashmatrix.com http request/response
139
+ test_files: []