Class: Strutta::Games

Inherits:
Object
  • Object
show all
Defined in:
lib/strutta-api/games.rb

Overview

The Strutta Games object is the master of all other API objects (Rounds, Flow, Entries, Participants) The above objects are instantiated in association with a Games object, as each belong to a Game in the Strutta API The above objects also have all their API requests forwarded through this object to Strutta::API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, master) ⇒ Strutta::Games

Initializes the Strutta::Games object

Parameters:

  • id (Integer, nil) (defaults to: nil)

    Game id

  • master (Strutta::API)

    Master Strutta::API object



13
14
15
16
17
# File 'lib/strutta-api/games.rb', line 13

def initialize(id = nil, master)
  @id = id
  @root_path = 'games'
  @master = master
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/strutta-api/games.rb', line 6

def id
  @id
end

#masterObject

Returns the value of attribute master.



6
7
8
# File 'lib/strutta-api/games.rb', line 6

def master
  @master
end

Instance Method Details

#all(params = {}, resource_path = nil) ⇒ Hash

Polymorphic GET Index request

Parameters:

  • resource_path (String, nil) (defaults to: nil)

    the GET index path for the requesting object See Strutta::APIObject#all

Returns:

  • (Hash)

    Parsed body of the response



34
35
36
37
# File 'lib/strutta-api/games.rb', line 34

def all(params = {}, resource_path = nil)
  verify_no_id(@id) unless resource_path
  call('get', "#{@root_path}/#{resource_path}", params)
end

#call(method, url, params = {}) ⇒ Hash

Forwards an API call to @master

Parameters:

  • method (String)

    the required HTTP method

  • url (String)

    URL for the call

  • params (Hash) (defaults to: {})

    Parameters for the call

Returns:

  • (Hash)

    Parsed body of the response



25
26
27
# File 'lib/strutta-api/games.rb', line 25

def call(method, url, params = {})
  @master.call(method, url, params)
end

#create(params = {}, resource_path = nil) ⇒ Hash

Polymorphic POST request

Parameters:

  • resource_path (String, nil) (defaults to: nil)

    the POST path for the requesting object See Strutta::APIObject#create

Returns:

  • (Hash)

    Parsed body of the response



54
55
56
57
# File 'lib/strutta-api/games.rb', line 54

def create(params = {}, resource_path = nil)
  verify_no_id(@id) unless resource_path
  call('post', "#{@root_path}/#{resource_path}", params)
end

#delete(resource_path = nil) ⇒ Hash

Polymorphic DELETE request

Parameters:

  • resource_path (String, nil) (defaults to: nil)

    the DELETE path for the requesting object See Strutta::APIObject#delete

Returns:

  • (Hash)

    Parsed body of the response



74
75
76
77
# File 'lib/strutta-api/games.rb', line 74

def delete(resource_path = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  call('delete', "#{@root_path}/#{@id}/#{resource_path}", {})
end

#entries(id = nil) ⇒ Strutta::Entries

Instantiates a Strutta::Entries object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    the ID of the Entry

Returns:



110
111
112
113
# File 'lib/strutta-api/games.rb', line 110

def entries(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Entries.new id, self
end

#flow(id = nil) ⇒ Strutta::Flow

Instantiates a Strutta::Flow object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    Flows do not have Ids. This is here simply for consistency, and will result in error of used.

Returns:



101
102
103
104
# File 'lib/strutta-api/games.rb', line 101

def flow(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Flow.new id, self
end

#get(params = {}, resource_path = '') ⇒ Hash

Polymorphic GET single object request

Parameters:

  • resource_path (String, nil) (defaults to: '')

    the GET path for the requesting object See Strutta::APIObject#get

Returns:

  • (Hash)

    Parsed body of the response



44
45
46
47
# File 'lib/strutta-api/games.rb', line 44

def get(params = {}, resource_path = '')
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  call('get', "#{@root_path}/#{@id}/#{resource_path}", params)
end

#judging(id = nil) ⇒ Strutta::Entries

Instantiates a Strutta::Judging object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    Judging does not have Ids. This is here simply for consistency, and will result in error of used.

Returns:



137
138
139
140
# File 'lib/strutta-api/games.rb', line 137

def judging(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Judging.new id, self
end

#moderation(id = nil) ⇒ Strutta::Entries

Instantiates a Strutta::Moderation object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    Moderation do not have Ids. This is here simply for consistency, and will result in error of used.

Returns:



128
129
130
131
# File 'lib/strutta-api/games.rb', line 128

def moderation(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Moderation.new id, self
end

#participants(id = nil) ⇒ Strutta::Participants

Instantiates a Strutta::Participants object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    the ID of the Participant

Returns:



92
93
94
95
# File 'lib/strutta-api/games.rb', line 92

def participants(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Participants.new id, self
end

#points(id = nil) ⇒ Strutta::Entries

Instantiates a Strutta::Points object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    Point IDs are not used by this API. This is here simply for consistency, and will result in error of used.

Returns:



119
120
121
122
# File 'lib/strutta-api/games.rb', line 119

def points(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Points.new id, self
end

#rounds(id = nil) ⇒ Strutta::Rounds

Instantiates a Strutta::Rounds object with reference to this Game

Parameters:

  • id (Integer, nil) (defaults to: nil)

    the ID of the Round

Returns:



83
84
85
86
# File 'lib/strutta-api/games.rb', line 83

def rounds(id = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  Rounds.new id, self
end

#update(params, resource_path = nil) ⇒ Hash

Polymorphic PATCH request

Parameters:

  • resource_path (String, nil) (defaults to: nil)

    the PATCH path for the requesting object See Strutta::APIObject#update

Returns:

  • (Hash)

    Parsed body of the response



64
65
66
67
# File 'lib/strutta-api/games.rb', line 64

def update(params, resource_path = nil)
  verify_id(@id, Errors::GAME_ID_REQUIRED)
  call('patch', "#{@root_path}/#{@id}/#{resource_path}", params)
end

#verify_id(id, message) ⇒ nil

Confirm that an id exists for a request

Parameters:

  • id (Integer)

    the ID of the objec

  • message (String)

    Error message if no id exists

Returns:

  • (nil)

    nil if id exists, Error otherwise



147
148
149
# File 'lib/strutta-api/games.rb', line 147

def verify_id(id, message)
  fail Errors::ObjectIDRequired, message unless id
end

#verify_no_id(id) ⇒ nil

Confirm that no id exists for a request

Parameters:

  • id (Integer)

    the ID of the objec

Returns:

  • (nil)

    nil if no id exists, Error otherwise



155
156
157
# File 'lib/strutta-api/games.rb', line 155

def verify_no_id(id)
  fail Errors::ObjectIDNotAllowed, Errors::OBJECT_NOT_ALLOWED if id
end