Class: SquashMatrix::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/squash_matrix/client.rb

Overview

Client for retrieving player and club information from squashmatrix.com. If authentication credentials are provided squash matrix will allow considerably more requests for an IP address and allow forbidden conent to be requested.

Instance Method Summary collapse

Constructor Details

#initialize(player: nil, email: nil, password: nil, suppress_errors: false, timeout: 60, user_agent: nil, cookie: nil, expires: nil, proxy_port: nil, proxy_addr: nil, proxy_user: nil, proxy_pass: nil, proxy_custom_headers: nil) ⇒ Client

Note:

If suppress_errors == false SquashMatrix::Errors::AuthorizationError will be raised if specified credentials are incorrect and squash matrix authentication returns forbidden

Returns newly created Client

Parameters:

  • opts (Hash)

    the options to create client



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/squash_matrix/client.rb', line 42

def initialize(player: nil,
               email: nil,
               password: nil,
               suppress_errors: false,
               timeout: 60,
               user_agent: nil,
               cookie: nil,
               expires: nil,
               proxy_port: nil,
               proxy_addr: nil,
               proxy_user: nil,
               proxy_pass: nil,
               proxy_custom_headers: nil)
  @user_agent = user_agent || UserAgentRandomizer::UserAgent.fetch(type: 'desktop_browser').string
  @squash_matrix_home_uri = URI::HTTP.build(host: SquashMatrix::Constants::SQUASH_MATRIX_URL)
  @suppress_errors = suppress_errors
  @timeout = timeout
  @proxy_port = proxy_port
  @proxy_addr = proxy_addr
  @proxy_user = proxy_user
  @proxy_pass = proxy_pass
  @proxy_custom_headers = proxy_custom_headers
  @request_client = @proxy_addr.nil? ? Net::HTTP::Proxy(@proxy_addr, @proxy_port&.to_i, @proxy_user, @proxy_pass) : Net::HTTP
  return unless [player || email, password].none?(&:nil?)
  @cookie_jar = HTTP::CookieJar.new
  @player = player&.to_i
  @email = email
  @password = password
  @expires = !expires.to_s.empty? && Time.parse(expires).utc
  if cookie && @expires > Time.now.utc
    cookie.split('; ').each do |v|
      @cookie_jar.parse(v, @squash_matrix_home_uri)
    end
  else
    setup_authentication
  end
end

Instance Method Details

#get_club_info(id = nil) ⇒ Hash

Note:

If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc…

Returns club info.

Parameters:

  • id (Fixnum) (defaults to: nil)

    club id found on squash matrix

Returns:

  • (Hash)

    hash object containing club information



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/squash_matrix/client.rb', line 85

def get_club_info(id = nil)
  return unless id.to_i.positive?
  uri = URI::HTTP.build(
    host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
    path: SquashMatrix::Constants::CLUB_PATH.gsub(':id', id.to_s)
  )
  success_proc = lambda do |res|
    SquashMatrix::NokogiriParser.get_club_info(res.body)
  end
  handle_http_request(uri, success_proc)
end

#get_player_info(id = nil) ⇒ Hash

Note:

If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc…

Returns player info.

Parameters:

  • id (Fixnum) (defaults to: nil)

    played id found on squash matrix

Returns:

  • (Hash)

    hash object containing player information



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/squash_matrix/client.rb', line 120

def get_player_info(id = nil)
  return unless id.to_i.positive?
  uri = URI::HTTP.build(
    host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
    path: SquashMatrix::Constants::PLAYER_HOME_PATH.gsub(':id', id.to_s)
  )
  success_proc = lambda do |res|
    SquashMatrix::NokogiriParser.get_player_info(res.body)
  end
  handle_http_request(uri, success_proc)
end

#get_player_results(id = nil) ⇒ Array<Hash>

Note:

If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc…

Returns player results.

Parameters:

  • id (Fixnum) (defaults to: nil)

    played id found on squash matrix

Returns:

  • (Array<Hash>)

    Array of player match results



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/squash_matrix/client.rb', line 102

def get_player_results(id = nil)
  return unless id.to_i.positive?
  uri = URI::HTTP.build(
    host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
    path: SquashMatrix::Constants::PLAYER_RESULTS_PATH.gsub(':id', id.to_s),
    query: SquashMatrix::Constants::PLAYER_RSULTS_QUERY
  )
  success_proc = lambda do |res|
    SquashMatrix::NokogiriParser.get_player_results(res.body)
  end
  handle_http_request(uri, success_proc)
end

#get_save_paramsHash

Returns params to create existing authenticated client

Returns:

  • (Hash)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/squash_matrix/client.rb', line 22

def get_save_params
  {
    player: @player,
    email: @email,
    password: @password,
    suppress_errors: @suppress_errors,
    timeout: @timeout,
    user_agent: @user_agent,
    cookie: get_cookie_string,
    expires: @expires.to_s,
    proxy_addr: @proxy_addr,
    proxy_port: @proxy_port
  }.delete_if { |_k, v| v.nil? }
end

#get_search_results(query = nil, squash_only: false, racquetball_only: false) ⇒ Hash

Note:

If suppress_errors == false SquashMatrix Errors will be raised upon HttpNotFound, HttpConflict, Timeout::Error, etc…

Returns get_search_results results

Parameters:

  • query (String) (defaults to: nil)

    get_search_results query

Returns:

  • (Hash)

    hash object containing get_search_results results



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/squash_matrix/client.rb', line 137

def get_search_results(query = nil,
                       squash_only: false,
                       racquetball_only: false)
  return if query.to_s.empty?
  uri = URI::HTTP.build(
    host: SquashMatrix::Constants::SQUASH_MATRIX_URL,
    path: SquashMatrix::Constants::SEARCH_RESULTS_PATH
  )
  query_params = {
    Criteria: query,
    SquashOnly: squash_only,
    RacquetballOnly: racquetball_only
  }
  success_proc = lambda do |res|
    SquashMatrix::NokogiriParser.get_search_results(res.body)
  end
  handle_http_request(uri, success_proc,
                      is_get_request: false,
                      query_params: query_params)
end