Class: LoLBase::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/lolbase/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Connection

Returns a new instance of Connection.



25
26
27
28
# File 'lib/lolbase/connection.rb', line 25

def initialize(key = nil)
  @key = key || LoLBase.config.default_key
  self
end

Instance Method Details

#get(path, options = {}) ⇒ Object

Override HTTParty’s get method to append the API key and to process errors returned from request

Raises:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lolbase/connection.rb', line 13

def get(path, options = {})
  if options[:query].nil?
    options.merge!({ query: { api_key: @key } })
  else
    options[:query].merge!({ api_key: @key })
  end

  response = self.class.get path, options
  raise LoLBaseError, response.message if response.code != 200
  response.body
end

#summoner(identifier, region = LoLBase.config.default_region) ⇒ Object

Syntactic sugar: lookup summoner by name or by ID



31
32
33
34
35
36
37
# File 'lib/lolbase/connection.rb', line 31

def summoner(identifier, region = LoLBase.config.default_region)
  if identifier.is_a? String
    return summoner_by_name(identifier, region)
  else
    return summoner_by_id(identifier, region)
  end
end

#summoner_by_id(id, region = LoLBase.config.default_region) ⇒ Object



43
44
45
# File 'lib/lolbase/connection.rb', line 43

def summoner_by_id(id, region = LoLBase.config.default_region)
  return Summoner.new({ id: id, region: region }, self)
end

#summoner_by_name(name, region = LoLBase.config.default_region) ⇒ Object



39
40
41
# File 'lib/lolbase/connection.rb', line 39

def summoner_by_name(name, region = LoLBase.config.default_region)
  return Summoner.new({ name: name, region: region }, self)
end