class Smiten::Core

The Core class implements those calls that are common to both the Smite and Paladins endpoints

Attributes

authKey[RW]
calls[R]
connector[R]
developerId[RW]
language_code[R]
portal_id[RW]

Public Class Methods

new(options) click to toggle source

Creates a new connection to the Hirez API end points It takes a single hash parameter which must contain the following two keys

:developerId
:authKey

These can be requested from the Hirez development team

Calls superclass method
# File lib/smiten/core.rb, line 46
def initialize(options)
  # We default to talking to the paladins endpoint
  options.merge!(headers: { 'Content-Type' => 'application/json' })
  options.merge!(url: PaladinsEndpoint) unless options[:url]
  @developerId = options.delete(:developerId)
  @authKey = options.delete(:authKey)
  @language_code = LanguageCode[:english]
  @portal_id = nil
  @session_id = nil
  @connector = Faraday.new(options) do |f|
    f.request :json # encode req bodies as JSON
    f.request :retry # retry transient failures
    f.response :json # decode response bodies as JSON
  end
  @calls = {}
  build_apis
  super
end

Public Instance Methods

core_api() click to toggle source

Returns a hash containing the API signatures keyed by ruby method name

# File lib/smiten/api.rb, line 4
def core_api
  { ping:                                   [nil,      -> { "ping#{ResponseFormat}" }],
    create_session:                         [nil,      -> { "#{boilerplate('createsession')}/#{timestamp}" }],
    test_session:                           [nil,      -> { "#{boilerplate('testsession')}" }],
    get_data_used:                          [nil,      -> { "#{boilerplate('getdataused')}" }],
    get_hirez_server_status:                [nil,      -> { "#{boilerplate('gethirezserverstatus')}" }],
    get_patch_info:                         [nil,      -> { "#{boilerplate('getpatchinfo')}" }],
    get_items:                              ['Item',   -> { "#{boilerplate('getitems')}/#{language_code}" }],
    get_bounty_items:                       [nil,      -> { "#{boilerplate('getbountyitems')}" }],
    get_player:                             ['Player', -> { "#{boilerplate('getplayer')}/#{player}#{"/#{portal_id}" if portal_id }"}],
    get_player_batch:                       ['Player', -> { "#{boilerplate('getplayerbatch')}/#{player_ids.map(&:to_s).join(',')}" }],
    get_player_id_by_name:                  [nil,      -> { "#{boilerplate('getplayeridbyname')}/#{player_name}" }],
    get_player_id_by_portal_user_id:        [nil,      -> { "#{boilerplate('getplayeridbyportaluserid')}/#{portal_id}/#{portal_user_id}" }],
    get_player_ids_by_gamer_tag:            [nil,      -> { "#{boilerplate('getplayeridsbygamertag')}/#{portal_id}/#{gamer_tag}" }],
    get_friends:                            [nil,      -> { "#{boilerplate('getfriends')}/#{player_id}" }],
    get_player_status:                      [nil,      -> { "#{boilerplate('getplayerstatus')}/#{player_id}" }],
    get_match_history:                      [nil,      -> { "#{boilerplate('getmatchhistory')}/#{player_id}" }],
    get_queue_stats:                        [nil,      -> { "#{boilerplate('getqueuestats')}/#{player_id}/#{queue_id}" }],
    search_players:                         [nil,      -> { "#{boilerplate('searchplayers')}/#{search_string}" }],
    get_demo_details:                       [nil,      -> { "#{boilerplate('getdemodetails')}/#{match_id}" }],
    get_match_details:                      [nil,      -> { "#{boilerplate('getmatchdetails')}/#{match_id}" }],
    get_match_details_batch:                [nil,      -> { "#{boilerplate('getmatchdetailsbatch')}/#{match_ids.map(&:to_s).join(',')}" }],
    get_match_ids_by_queue:                 [nil,      -> { "#{boilerplate('getmatchidsbyqueue')}/#{queue_id}/#{date}/#{hour}" }],
    get_match_player_details:               [nil,      -> { "#{boilerplate('getmatchplayerdetails')}/#{match_id}" }],
    get_top_matches:                        [nil,      -> { "#{boilerplate('gettopmatches')}" }],
    get_league_leaderboard:                 [nil,      -> { "#{boilerplate('getleagueleaderboard')}/#{queue_id}/#{tier}/#{round}" }],
    get_league_seasons:                     [nil,      -> { "#{boilerplate('getleagueseasons')}/#{queue_id}" }],
    get_team_details:                       [nil,      -> { "#{boilerplate('getteamdetails')}/#{team_id}" }],
    get_team_players:                       [nil,      -> { "#{boilerplate('getteamplayers')}/#{team_id}" }],
    get_esports_proleague_details:          [nil,      -> { "#{boilerplate('getesportsproleaguedetails')}" }],
    get_motd:                               [nil,      -> { "#{boilerplate('getmotd')}"}]
  }
end
for_champion(id) { |self| ... } click to toggle source

Sets the champion_id on the connection and returns the connection

# File lib/smiten/core.rb, line 91
def for_champion(id)
  self.champion_id = id
  yield(self)
end
get() click to toggle source

Synchronously retrieve the response from the Hirez WEB API

# File lib/smiten/core.rb, line 111
def get
  # NOTE: The html error page returned from Hirez breaks the json parser. I just ignore that and
  # retrieve the original response
  start = Time.now
  5.times do
    begin
      result = connector.get(@url)
    rescue Faraday::ParsingError => e
      result = e.response
    end
    case
    when (200..300).include?(result.status)
      return result.body
    when result.status == 408
      puts '* Server Timeout detected... Retrying'
    when result.status == 504
      puts '* Gateway Timeout detected... Retrying'
    else
      raise(Error, "API Response Error:#{textify(result.body)}")
    end
  end
  puts "* Timeout detected... Terminating after #{Time.now - start} seconds"
  nil
end
in_language(name_or_id) { |self| ... } click to toggle source

Sets the language on the connection and returns the connection Accepts either a language_code or the language name as a symbol

# File lib/smiten/core.rb, line 98
def in_language(name_or_id)
  self.language_code = name_or_id.is_a?(Numeric) ? name_or_id : LanguageCode[name_or_id]
  yield(self)
end