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_addr: nil, proxy_port: 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
# 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_addr: nil,
               proxy_port: 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_addr = proxy_addr
  @proxy_port = proxy_port
  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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/squash_matrix/client.rb', line 78

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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/squash_matrix/client.rb', line 113

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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/squash_matrix/client.rb', line 95

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/squash_matrix/client.rb', line 130

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