lib/lolxin/client.rb in lolxin-0.12.1 vs lib/lolxin/client.rb in lolxin-0.13.0
- old
+ new
@@ -1,92 +1,40 @@
module Lolxin
class ClientError < StandardError; end
# Client create connection clients for specific endpoints
class Client
- attr_reader :region, :api_key
+ ENDPOINTS = [
+ :champion_mastery,
+ :champion,
+ :league,
+ :lol_static_data,
+ :lol_status,
+ :match,
+ :spectator,
+ :summoner,
+ :third_party_code
+ #tournament_stub
+ #tournament
+ ]
+ attr_reader :region, :api_key, :options
+
def initialize(api_key, options = {})
@api_key = api_key
- @region = options[:region] if Region.valid?(options[:region])
- raise ClientError, "Invalid region given" unless @region
+ @region = options[:region] if Region.valid?(options[:region])
+ @options = { api_key: @api_key, region: @region }
+ raise ClientError, "Invalid API key" if @api_key.nil? || @api_key.empty?
+ raise ClientError, "Invalid region" unless @region
end
- def champion(options = {})
- options[:api_key] = api_key
- options[:region] = region
- options[:version] = ApiVersion::CHAMPION
- Champion.new(options)
- end
+ ENDPOINTS.each do |ep|
+ define_method(ep) do |options = @options|
+ options[:version] = ApiVersion.const_get(ep.to_s.upcase)
- def lol_static_data(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::LOL_STATIC_DATA
- LolStaticData.new(options)
- end
-
- def lol_status(options = {})
- LolStatus.new(options)
- end
-
- def current_game(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- CurrentGame.new(options)
- end
-
- def featured_games(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- FeaturedGames.new(options)
- end
-
- def game(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::GAME
- Game.new(options)
- end
-
- def champion_mastery(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- ChampionMastery.new(options)
- end
-
- def league(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::LEAGUE
- League.new(options)
- end
-
- def match(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::MATCH
- Match.new(options)
- end
-
- def match_list(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::MATCH_LIST
- MatchList.new(options)
- end
-
- def stats(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::STATS
- Stats.new(options)
- end
-
- def summoner(options = {})
- options[:api_key] ||= api_key
- options[:region] ||= region
- options[:version] ||= ApiVersion::SUMMONER
- Summoner.new(options)
+ name = ep.to_s.split('_').map(&:capitalize).join
+ klass = Object.const_get(name)
+ klass.new(options)
+ end
end
end
end