├── .gitignore ├── README.md ├── Rakefile ├── challonge-api.gemspec └── lib ├── challonge-api.rb └── challonge ├── api.rb ├── match.rb ├── participant.rb └── tournament.rb /.gitignore: -------------------------------------------------------------------------------- 1 | pkg/* 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Challonge Ruby Gem 2 | 3 | Utilize the CHALLONGE! API with preconfigured ActiveResource classes. 4 | 5 | 6 | ## Install and Configure 7 | 8 | Add ```challonge-api``` to your Gemfile: 9 | 10 | ``` 11 | gem 'challonge-api' 12 | ``` 13 | 14 | Add challonge-api as a dependency for your project and set your username and API key on startup. 15 | 16 | ``` 17 | Challonge::API.username = 'username' 18 | Challonge::API.key = '123keyayaonge' 19 | ``` 20 | 21 | ## Basic Usage 22 | 23 | Get all your tournaments: 24 | 25 | ``` 26 | Challonge::Tournament.find(:all) 27 | ``` 28 | 29 | Get a filtered set of tournaments: 30 | 31 | ``` 32 | Challonge::Tournament.find(:all, :params => {:state => 'in_progress'}) 33 | ``` 34 | 35 | Run a basic tournament from start to finish: 36 | 37 | ``` 38 | t = Challonge::Tournament.new 39 | t.name = 'Basic Single Elim Tournament' 40 | t.url = 'unique_url_123' 41 | t.tournament_type = 'single elimination' 42 | t.save 43 | # if save returns false, view validation errors with t.errors.full_messages 44 | 45 | # add some participants 46 | Challonge::Participant.create(:name => 'Joe', :tournament => t) 47 | Challonge::Participant.create(:name => 'Bob', :tournament => t) 48 | 49 | t.start! 50 | # at this point, t.live_image_url and t.full_challonge_url are available (t.reload to refetch) 51 | 52 | m = t.matches(:first) 53 | m.scores_csv = '3-1,3-2' 54 | m.winner_id = m.player1_id 55 | m.save 56 | # congratulations, Joe 57 | 58 | t.finalize! 59 | # Finalize a tournament, rendering its results permanent. 60 | ``` 61 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "rubygems" 2 | require "rubygems/package_task" 3 | require "rdoc/task" 4 | 5 | task :default => :package do 6 | puts "Don't forget to write some tests!" 7 | end 8 | 9 | # This builds the actual gem. For details of what all these options 10 | # mean, and other ones you can add, check the documentation here: 11 | # 12 | # http://rubygems.org/read/chapter/20 13 | # 14 | spec = Gem::Specification.new do |s| 15 | 16 | # Change these as appropriate 17 | s.name = "challonge-api" 18 | s.version = "0.2.0" 19 | s.summary = "Preconfigured ActiveResource classes for integrating with CHALLONGE!'s API - http://challonge.com/api" 20 | s.author = "CHALLONGE! LLC" 21 | s.email = "oss@challonge.com" 22 | s.homepage = "http://challonge.com" 23 | s.license = "MIT" 24 | 25 | s.has_rdoc = true 26 | # You should probably have a README of some kind. Change the filename 27 | # as appropriate 28 | # s.extra_rdoc_files = %w(README) 29 | # s.rdoc_options = %w(--main README) 30 | 31 | # Add any extra files to include in the gem (like your README) 32 | s.files = %w() + Dir.glob("{lib}/**/*") 33 | s.require_paths = ["lib"] 34 | 35 | # If you want to depend on other gems, add them here, along with any 36 | # relevant versions 37 | s.add_dependency("activeresource") 38 | 39 | # If your tests use any gems, include them here 40 | # s.add_development_dependency("mocha") # for example 41 | end 42 | 43 | # This task actually builds the gem. We also regenerate a static 44 | # .gemspec file, which is useful if something (i.e. GitHub) will 45 | # be automatically building a gem for this project. If you're not 46 | # using GitHub, edit as appropriate. 47 | Gem::PackageTask.new(spec) do |pkg| 48 | pkg.gem_spec = spec 49 | end 50 | 51 | desc "Build the gemspec file #{spec.name}.gemspec" 52 | task :gemspec do 53 | file = File.dirname(__FILE__) + "/#{spec.name}.gemspec" 54 | File.open(file, "w") {|f| f << spec.to_ruby } 55 | end 56 | 57 | # If you don't want to generate the .gemspec file, just remove this line. Reasons 58 | # why you might want to generate a gemspec: 59 | # - using bundler with a git source 60 | # - building the gem without rake (i.e. gem build blah.gemspec) 61 | # - maybe others? 62 | task :package => :gemspec 63 | 64 | # Generate documentation 65 | Rake::RDocTask.new do |rd| 66 | rd.rdoc_files.include("lib/**/*.rb") 67 | rd.rdoc_dir = "rdoc" 68 | end 69 | 70 | desc 'Clear out RDoc and generated packages' 71 | task :clean => [:clobber_rdoc, :clobber_package] do 72 | rm "#{spec.name}.gemspec" 73 | end 74 | -------------------------------------------------------------------------------- /challonge-api.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | Gem::Specification.new do |s| 4 | s.name = "challonge-api" 5 | s.version = "0.2.0" 6 | 7 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 8 | s.authors = ["CHALLONGE! LLC"] 9 | s.date = "2012-11-26" 10 | s.email = "oss@challonge.com" 11 | s.files = ["lib/challonge", "lib/challonge/participant.rb", "lib/challonge/tournament.rb", "lib/challonge/match.rb", "lib/challonge/api.rb", "lib/challonge-api.rb"] 12 | s.homepage = "http://challonge.com" 13 | s.licenses = ["MIT"] 14 | s.require_paths = ["lib"] 15 | s.rubygems_version = "2.0.2" 16 | s.summary = "Preconfigured ActiveResource classes for integrating with CHALLONGE!'s API - http://challonge.com/api" 17 | 18 | if s.respond_to? :specification_version then 19 | s.specification_version = 4 20 | 21 | if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then 22 | s.add_runtime_dependency(%q, [">= 0"]) 23 | else 24 | s.add_dependency(%q, [">= 0"]) 25 | end 26 | else 27 | s.add_dependency(%q, [">= 0"]) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/challonge-api.rb: -------------------------------------------------------------------------------- 1 | require 'active_resource' 2 | require 'challonge/api' 3 | require 'challonge/tournament' 4 | require 'challonge/participant' 5 | require 'challonge/match' 6 | -------------------------------------------------------------------------------- /lib/challonge/api.rb: -------------------------------------------------------------------------------- 1 | class Challonge 2 | class API < ::ActiveResource::Base 3 | self.user = 'your_challonge_username' 4 | self.password = 'your_challonge_api_key' 5 | self.format = :json 6 | self.include_root_in_json = true 7 | 8 | @readonly_attributes = [] 9 | 10 | def self.username=(uname) 11 | self.user = uname 12 | end 13 | 14 | def self.username 15 | self.user 16 | end 17 | 18 | def self.key=(key) 19 | self.password = key 20 | end 21 | 22 | def self.key 23 | self.password 24 | end 25 | 26 | protected 27 | 28 | def validated_post(action) 29 | post(action) 30 | true 31 | rescue ::ActiveResource::ResourceInvalid => error 32 | errors.from_xml(error.response.body) 33 | false 34 | end 35 | 36 | def encode 37 | {self.class.element_name => writable_attribute_hash}.to_json 38 | end 39 | 40 | def readonly_attributes 41 | [] 42 | end 43 | 44 | def writable_attribute_hash 45 | attr_h = {} 46 | (attributes.keys - readonly_attributes).each do |key| 47 | attr_h[key] = attributes[key] 48 | end 49 | attr_h 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/challonge/match.rb: -------------------------------------------------------------------------------- 1 | # the only attributes that will save are: scores_csv, winner_id 2 | 3 | class Challonge::Match < Challonge::API 4 | self.site = "https://challonge.com/api/tournaments/:tournament_id" 5 | 6 | def tournament 7 | Challonge::Tournament.find(self.prefix_options[:tournament_id]) 8 | end 9 | 10 | def tournament=(tournament) 11 | self.prefix_options[:tournament_id] = tournament.id 12 | end 13 | 14 | def player1 15 | _find_player(:player1_id) 16 | end 17 | 18 | def player2 19 | _find_player(:player2_id) 20 | end 21 | 22 | def player_winner? (participant) 23 | (participant.id != self.winner_id) 24 | end 25 | 26 | # not implemented by API 27 | def create; end 28 | 29 | # not implemented by API 30 | def destroy; end 31 | 32 | protected 33 | 34 | def readonly_attributes 35 | %w/prerequisite_match_ids_csv/ 36 | end 37 | 38 | private 39 | def _find_player(player) 40 | if self.attributes[player] != nil 41 | Challonge::Participant.find(self.attributes[player], :params => {:tournament_id => self.prefix_options[:tournament_id]}) 42 | else 43 | Challonge::Participant.new({:name => 'TBD'}) 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/challonge/participant.rb: -------------------------------------------------------------------------------- 1 | # the only attributes that will save are: name, seed, challonge_username, email, misc 2 | 3 | class Challonge::Participant < Challonge::API 4 | self.site = "https://challonge.com/api/tournaments/:tournament_id" 5 | 6 | def initialize(attributes = {}, persisted = false) 7 | @attributes = {} 8 | @prefix_options = {} 9 | @persisted = persisted 10 | 11 | # allow new and create to be passed a tournament or tournament_id 12 | real_attributes = attributes.slice!(:tournament, :tournament_id, "tournament", "tournament_id") 13 | load(real_attributes) 14 | attributes.each_pair do |attr, val| 15 | self.send("#{attr}=", val) 16 | end 17 | end 18 | 19 | def tournament 20 | Challonge::Tournament.find(self.prefix_options[:tournament_id]) 21 | end 22 | 23 | def tournament=(tournament) 24 | self.prefix_options[:tournament_id] = tournament.id 25 | end 26 | 27 | def randomize! 28 | validated_post(:randomize) 29 | end 30 | 31 | def name 32 | super ? super : @attributes["challonge_username"] 33 | end 34 | 35 | protected 36 | 37 | def readonly_attributes 38 | %w/challonge_email_address_verified/ 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/challonge/tournament.rb: -------------------------------------------------------------------------------- 1 | class Challonge::Tournament < Challonge::API 2 | self.site = "https://challonge.com/api" 3 | 4 | def description 5 | if self.attributes.include?('description_source') 6 | self.description_source 7 | end 8 | end 9 | 10 | def description=(val) 11 | self.description_source = val 12 | end 13 | 14 | def start! 15 | validated_post(:start) 16 | end 17 | 18 | def finalize! 19 | validated_post(:finalize) 20 | end 21 | 22 | def reset! 23 | validated_post(:reset) 24 | end 25 | 26 | def participants(scope = :all) 27 | Challonge::Participant.find(scope, :params => {:tournament_id => self.id}) 28 | end 29 | 30 | def matches(scope = :all) 31 | Challonge::Match.find(scope, :params => {:tournament_id => self.id}) 32 | end 33 | 34 | protected 35 | 36 | def readonly_attributes 37 | %w/description_source subdomain full_challonge_url live_image_url sign_up_url/ 38 | end 39 | 40 | def writable_attribute_hash 41 | attr_h = super 42 | attr_h['description'] = description 43 | attr_h 44 | end 45 | end 46 | --------------------------------------------------------------------------------