Sha256: 722cc9274dcbcd1bfaec0341f5db279e31e51e17bbfa6ba5035bf8e78925ae6a

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

class Hash
  def method_missing(name)
    return self[name].extend Hash 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
    class << self
      modes = ["regular", "bankara_challenge", "bankara_open", "x"]
      schedules = ["now", "next"]

      schedules.each do |schedule|
        modes.each do |mode|
          define_method("#{mode}_#{schedule}".to_sym) do
            return instance_variable_get("@#{mode}_#{schedule}_obj") if instance_variable_defined?("@#{mode}_#{schedule}_obj")

            instance_variable_set("@#{mode}_#{schedule}_obj", send_request(:get, "/api/#{mode.dasherize}/#{schedule}").body.results[0])
          end
        end

        define_method("salmon_run_#{schedule}".to_sym) do
          return instance_variable_get("@salmon_run_#{schedule}_obj") if instance_variable_defined?("@salmon_run_#{schedule}_obj")

          instance_variable_set("@salmon_run_#{schedule}_obj", send_request(:get, "/api/coop-grouping/#{schedule}").body.results[0])
        end
      end

      private

      def send_request(method, path, params = nil, headers = nil)
        response = splat3_connection.send(method, path, params, headers)
        Ika3::Response.new(response)
      end

      def api_url
        "https://spla3.yuu26.com/"
      end

      def splat3_connection
        @splat3_connection ||= Faraday::new(faraday_options) do |c|
          c.request :json
          c.response :json
          c.adapter Faraday.default_adapter
        end
      end

      def faraday_options
        { url: api_url }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ika3-0.2.1 lib/ika3/schedule.rb