class RitoPlz::API::Request

Attributes

path[R]
region[R]

Public Class Methods

new(region, path, params = {}) click to toggle source

Use application configuration or use custom configuration

# File lib/RitoPlz/API/request.rb, line 11
def initialize(region, path, params = {})
  @path = path
  @region = region
  @type = params[:type] || :api
end

Public Instance Methods

get(query_params = {}) click to toggle source
# File lib/RitoPlz/API/request.rb, line 17
def get(query_params = {})
  uri = URI(request_url(query_params))
  response = Net::HTTP.get_response(uri)
  verify_response!(response)
  return response
end
post(params, query_params = {}) click to toggle source
# File lib/RitoPlz/API/request.rb, line 24
def post(params, query_params = {})
  uri = URI(request_url(query_params))
  req = Net::HTTP::Post.new(uri)
  req.body = params.to_json
  req['content-type'] = "application/json"

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  verify_response!(response)
  return response
end
put(params, query_params = {}) click to toggle source
# File lib/RitoPlz/API/request.rb, line 38
def put(params, query_params = {})
  uri = URI(request_url(query_params))
  req = Net::HTTP::Put.new(uri)
  req.body = params.to_json
  req['content-type'] = "application/json"

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  verify_response!(response)
  return response
end

Private Instance Methods

format_params(query_params) click to toggle source
# File lib/RitoPlz/API/request.rb, line 69
def format_params(query_params)
  query_params.each do |key, value|
    if value.is_a?(Array)
      query_params[key] = value.join(',')
    end
  end
  query_params
end
request_url(query_params = {}) click to toggle source
# File lib/RitoPlz/API/request.rb, line 54
def request_url(query_params = {})
  query_params[:api_key] = RitoPlz.configuration.api_key

  case @type
  when :status
    base_url = "https://status.leagueoflegends.com"
  else
    base_url = "https://#{region}.api.pvp.net"
  end

  final_params = format_params(query_params)

  base_url + path + "?#{URI.encode_www_form(final_params)}"
end
verify_response!(response) click to toggle source
# File lib/RitoPlz/API/request.rb, line 78
def verify_response!(response)
  case response.code
  when '400'
    raise BadRequestException
  when '401'
    raise UnauthorizedException
  when '403'
    raise ForbiddenException
  when '404'
    raise NotFoundException
  when '415'
    raise UnsupportedMediaTypeException
  when '429'
    raise RateLimitExceededException
  when '500'
    raise InternalServerException
  when '503'
    raise ServiceUnavailableException
  end
end