Sha256: f82dccaaf1ea4eacc5b1aabceb00baeb590654608a10cd57d2e31bd35ca41d89

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module QuakeliveApi
  class Base
    attr_accessor :player_name

    def initialize(player_name)
      @player_name = player_name

      set_parser Nokogiri::HTML(get)
      setup_variables!
    end

    def inspect
      "#{self.class}:#{object_id}\n" + instance_variables.map do |v|
        next if v.to_s == "@parser"
        "#{v}=#{instance_variable_get(v).inspect}"
      end.compact.join("\n")
    end

    private

    def get
      Net::HTTP.get(URI.parse(URI::encode("#{QuakeliveApi.site}#{url}")))
    end

    def url
      raise NotImplementedError
    end

    def parser
      @parser
    end

    def set_parser(document)
      @parser ||= ::QuakeliveApi::Parser.const_get(self.class.class_name).new(document)
    end

    def self.class_name
      name.split('::').last
    end

    def setup_variables!
      raise Error::PlayerNotFound if parser.invalid_player?
      raise Error::RequestError   if parser.request_error?

      setup_variables
    end

    def setup_variables
      raise NotImplementedError
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
quakelive_api-0.1.2 lib/quakelive_api/base.rb
quakelive_api-0.1.1 lib/quakelive_api/base.rb
quakelive_api-0.1.0 lib/quakelive_api/base.rb