module Strutta # 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 class Games attr_accessor :master, :id # Initializes the Strutta::Games object # # @param id [Integer, nil] Game id # @param master [Strutta::API] Master Strutta::API object # @return [Strutta::Games] instantiated Strutta::Games object def initialize(id = nil, master) @id = id @root_path = 'games' @master = master end # Forwards an API call to @master # # @param method [String] the required HTTP method # @param url [String] URL for the call # @param params [Hash] Parameters for the call # @return [Hash] Parsed body of the response def call(method, url, params = {}) @master.call(method, url, params) end # Polymorphic GET Index request # # @param resource_path [String, nil] the GET index path for the requesting object # See Strutta::APIObject#all # @return [Hash] Parsed body of the response def all(params = {}, resource_path = nil) verify_no_id(@id) unless resource_path call('get', "#{@root_path}/#{resource_path}", params) end # Polymorphic GET single object request # # @param resource_path [String, nil] the GET path for the requesting object # See Strutta::APIObject#get # @return [Hash] Parsed body of the response def get(params = {}, resource_path = '') verify_id(@id, Errors::GAME_ID_REQUIRED) call('get', "#{@root_path}/#{@id}/#{resource_path}", params) end # Polymorphic POST request # # @param resource_path [String, nil] the POST path for the requesting object # See Strutta::APIObject#create # @return [Hash] Parsed body of the response def create(params = {}, resource_path = nil) verify_no_id(@id) unless resource_path call('post', "#{@root_path}/#{resource_path}", params) end # Polymorphic PATCH request # # @param resource_path [String, nil] the PATCH path for the requesting object # See Strutta::APIObject#update # @return [Hash] Parsed body of the response def update(params, resource_path = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) call('patch', "#{@root_path}/#{@id}/#{resource_path}", params) end # Polymorphic DELETE request # # @param resource_path [String, nil] the DELETE path for the requesting object # See Strutta::APIObject#delete # @return [Hash] Parsed body of the response def delete(resource_path = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) call('delete', "#{@root_path}/#{@id}/#{resource_path}", {}) end # Instantiates a Strutta::Rounds object with reference to this Game # # @param id [Integer, nil] the ID of the Round # @return [Strutta::Rounds] The instantiated Strutta::Rounds object def rounds(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Rounds.new id, self end # Instantiates a Strutta::Participants object with reference to this Game # # @param id [Integer, nil] the ID of the Participant # @return [Strutta::Participants] The instantiated Strutta::Participants object def participants(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Participants.new id, self end # Instantiates a Strutta::Flow object with reference to this Game # # @param id [Integer, nil] Flows do not have Ids. This is here simply for consistency, and will result in error of used. # @return [Strutta::Flow] The instantiated Strutta::Flow object def flow(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Flow.new id, self end # Instantiates a Strutta::Entries object with reference to this Game # # @param id [Integer, nil] the ID of the Entry # @return [Strutta::Entries] The instantiated Strutta::Entries object def entries(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Entries.new id, self end # Instantiates a Strutta::Points object with reference to this Game # # @param id [Integer, nil] Point IDs are not used by this API. This is here simply for consistency, and will result in error of used. # @return [Strutta::Entries] The instantiated Strutta::Points object def points(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Points.new id, self end # Instantiates a Strutta::Moderation object with reference to this Game # # @param id [Integer, nil] Moderation do not have Ids. This is here simply for consistency, and will result in error of used. # @return [Strutta::Entries] The instantiated Strutta::Moderation object def moderation(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Moderation.new id, self end # Instantiates a Strutta::Judging object with reference to this Game # # @param id [Integer, nil] Judging does not have Ids. This is here simply for consistency, and will result in error of used. # @return [Strutta::Entries] The instantiated Strutta::Judging object def judging(id = nil) verify_id(@id, Errors::GAME_ID_REQUIRED) Judging.new id, self end # Confirm that an id exists for a request # # @param id [Integer] the ID of the objec # @param message [String] Error message if no id exists # @return [nil] nil if id exists, Error otherwise def verify_id(id, message) fail Errors::ObjectIDRequired, message unless id end # Confirm that no id exists for a request # # @param id [Integer] the ID of the objec # @return [nil] nil if no id exists, Error otherwise def verify_no_id(id) fail Errors::ObjectIDNotAllowed, Errors::OBJECT_NOT_ALLOWED if id end end end