lib/ika3/schedule.rb in ika3-0.8.1 vs lib/ika3/schedule.rb in ika3-0.9.0

- old
+ new

@@ -1,20 +1,7 @@ # frozen_string_literal: true -class Hash - def respond_to_missing?(sym) - key? name ? true : super - end - - def method_missing(name) - return self[name] if key? name - - each { |k, v| return v if k.to_s.to_sym == name } - super.method_missing(name) - end -end - module Ika3 class Schedule def initialize(contact) @contact = contact end @@ -23,39 +10,108 @@ schedules = %w[now next] schedules.each do |schedule| modes.each do |mode| define_method(:"#{mode}_#{schedule}") do - if instance_variable_defined?("@#{mode}_#{schedule}_obj") - return instance_variable_get("@#{mode}_#{schedule}_obj") - end + return instance_variable_get("@#{mode}_#{schedule}") if instance_variable_defined?("@#{mode}_#{schedule}") - instance_variable_set("@#{mode}_#{schedule}_obj", - send_request(:get, "/api/#{mode.dasherize}/#{schedule}").body.results[0]) + instance_variable_set( + "@#{mode}_#{schedule}", + Battle.new(send_request(:get, "/api/#{mode.dasherize}/#{schedule}").body['results'][0]) + ) end end define_method(:"salmon_run_#{schedule}") do - if instance_variable_defined?("@salmon_run_#{schedule}_obj") - return instance_variable_get("@salmon_run_#{schedule}_obj") - end + return instance_variable_get("@salmon_run_#{schedule}") if instance_variable_defined?("@salmon_run_#{schedule}") - instance_variable_set("@salmon_run_#{schedule}_obj", - send_request(:get, "/api/coop-grouping/#{schedule}").body.results[0]) + instance_variable_set( + "@salmon_run_#{schedule}", Salmon.new(send_request(:get, "/api/coop-grouping/#{schedule}").body['results'][0]) + ) end def salmon_run_team_contest - return @salmon_run_team_contest_obj unless @salmon_run_team_contest_obj.nil? + return @salmon_run_team_contest unless @salmon_run_team_contest.nil? - @salmon_run_team_contest_obj = send_request(:get, '/api/coop-grouping-team-contest/schedule').body.results[0] + @salmon_run_team_contest = Salmon.new( + send_request(:get, '/api/coop-grouping-team-contest/schedule').body['results'][0] + ) end def event - return @event_match_obj unless @event_match_obj.nil? + return @event_match unless @event_match.nil? - @event_match_obj = send_request(:get, '/api/event/schedule').body.results[0] + @event_match = Battle.new(send_request(:get, '/api/event/schedule').body['results'][0]) end end + + class Battle + attr_reader :start_time, :end_time, :rule, :stages, :is_fest + + def initialize(data) + @start_time = data&.[]('start_time') + @end_time = data&.[]('end_time') + @rule = Rule.new(data&.[]('rule')) + @stages = data&.[]('stages')&.map { |stage| Stage.new(stage) } + @is_fest = data&.[]('is_fest') + end + + class Stage + attr_reader :id, :name, :image + + def initialize(data) + @id = data&.[]('id') + @name = data&.[]('name') + @image = data&.[]('image') + end + end + + class Rule + attr_reader :name, :key + + def initialize(data) + @name = data&.[]('name') + @key = data&.[]('key') + end + end + + private_constant :Stage, :Rule + end + + class Salmon + attr_reader :start_time, :end_time, :stage, :weapons + + def initialize(data) + return unless data + + @start_time = data['start_time'] + @end_time = data['end_time'] + @stage = Stage.new(data['stage']) + @weapons = data['weapons'].map { |weapon| Weapon.new(weapon) } + end + + class Stage + attr_reader :name, :image + + def initialize(data) + @name = data['name'] + @image = data['image'] + end + end + + class Weapon + attr_reader :name, :image + + def initialize(data) + @name = data['name'] + @image = data['image'] + end + end + + private_constant :Stage, :Weapon + end + + private_constant :Battle, :Salmon private def send_request(method, path) response = splat3_connection.send(method, path)