Class: CoachClient::Sport

Inherits:
Resource show all
Defined in:
lib/coach_client/sport.rb

Overview

A sport resource of the CyberCoach service.

Instance Attribute Summary collapse

Attributes inherited from Resource

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#exist?, #to_h

Constructor Details

#initialize(client, sport) ⇒ CoachClient::Sport

Creates a new sport.

Parameters:



54
55
56
57
# File 'lib/coach_client/sport.rb', line 54

def initialize(client, sport)
  super(client)
  @sport = sport.downcase.to_sym
end

Instance Attribute Details

#descriptionString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/coach_client/sport.rb', line 11

def description
  @description
end

#idInteger (readonly)

Returns:

  • (Integer)


5
6
7
# File 'lib/coach_client/sport.rb', line 5

def id
  @id
end

#nameString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/coach_client/sport.rb', line 11

def name
  @name
end

#sportSymbol (readonly)

Returns:

  • (Symbol)


8
9
10
# File 'lib/coach_client/sport.rb', line 8

def sport
  @sport
end

Class Method Details

.list(client) {|sport| ... } ⇒ Array<CoachClient::Sport>

Returns a list of sports from the CyberCoach service for which the given block returns a true value.

If no block is given, the whole list is returned.

Parameters:

Yield Parameters:

Yield Returns:

  • (Boolean)

    whether the sport should be added to the list

Returns:



39
40
41
42
43
44
45
46
47
# File 'lib/coach_client/sport.rb', line 39

def self.list(client)
  sportlist  = []
  response = CoachClient::Request.get(client.url + path)
  response.to_h[:sports].each do |s|
    sport = new(client, s[:name])
    sportlist << sport if !block_given? || yield(sport)
  end
  sportlist
end

.pathString

Returns the relative path to the sport resource.

Returns:

  • (String)

    the relative path



16
17
18
# File 'lib/coach_client/sport.rb', line 16

def self.path
  'sports/'
end

.total(client) ⇒ Integer

Returns the total number of sports present on the CyberCoach service.

Parameters:

Returns:

  • (Integer)

    the total number of sports



24
25
26
27
28
# File 'lib/coach_client/sport.rb', line 24

def self.total(client)
  response = CoachClient::Request.get(client.url + path,
                                      params: { size: 0 })
  response.to_h[:available]
end

Instance Method Details

#to_sString

Returns the string representation of the sport.

Returns:

  • (String)


82
83
84
# File 'lib/coach_client/sport.rb', line 82

def to_s
  @sport.to_s
end

#updateCoachClient::Sport

Updates the sport with the data from the CyberCoach service.

Returns:

Raises:



63
64
65
66
67
68
69
70
# File 'lib/coach_client/sport.rb', line 63

def update
  response = CoachClient::Request.get(url)
  response = response.to_h
  @id = response[:id]
  @name = response[:name]
  @description = response[:description]
  self
end

#urlString

Returns the URL of the sport.

Returns:

  • (String)

    the url of the sport



75
76
77
# File 'lib/coach_client/sport.rb', line 75

def url
  @client.url + self.class.path + @sport.to_s
end