Sha256: 99246cc2afd68a4169872d6555a91f1dd1c416e75a07232784ead2cbcab6fcdd

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Lol
  class StaticRequest < Request
    STANDARD_ENDPOINTS = %w(champion item mastery rune summoner_spell)

    def self.api_version
      "v1"
    end

    # Returns a full url for an API call
    # Overrides api_url from Request
    # @param path [String] API path to call
    # @return [String] full fledged url
    def api_url path, params = {}
      query_string = URI.encode_www_form params.merge api_key: api_key
      File.join "http://prod.api.pvp.net/api/lol/static-data/#{region}/#{self.class.api_version}/", "#{path}?#{query_string}"
    end

    STANDARD_ENDPOINTS.each do |endpoint|
      define_method(endpoint) { Proxy.new self, endpoint }
    end
    def realm
      Proxy.new self, 'realm'
    end

    def get(endpoint, id=nil, params={})
      id ? find(endpoint, id, params) : all(endpoint, params)
    end

    private

    def find(endpoint, id, params={})
      model_class(endpoint).new \
        perform_request(api_url("#{endpoint.dasherize}/#{id}", params)).to_hash
    end

    def all(endpoint, params={})
      if endpoint == "realm"
        model_class(endpoint).new perform_request(api_url(endpoint.dasherize, params)).to_hash
      else
        perform_request(api_url(endpoint.dasherize, params))["data"].map do |id, values|
          model_class(endpoint).new(values.merge(id: id))
        end
      end
    end

    def model_class(endpoint)
      OpenStruct
    end

    class Proxy
      def initialize(request, endpoint)
        @request = request
        @endpoint = endpoint
      end

      def get(id=nil, params={})
        if id.is_a?(Hash)
          params = id
          id = nil
        end

        @request.get @endpoint, id, params
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-lol-0.9.10 lib/lol/static_request.rb