Last active
November 27, 2015 14:32
-
-
Save Extrapolator214/03c632b7294377e88a06 to your computer and use it in GitHub Desktop.
API client for Smite game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this API client is designed for Rails app, but you can use other framework | |
# by replacing key storage and http client as you see fit. | |
# | |
# Usage: | |
# api = SmiteApi.new | |
# api.testsession | |
# queue_stats = api.getqueuestats(123, 426) | |
# | |
class SmiteApi | |
BASE_URL = 'http://api.smitegame.com/smiteapi.svc' | |
METHODS = %w[ | |
testsession | |
getdataused | |
getdemodetails | |
getesportsproleaguedetails | |
getfriends | |
getgodranks | |
getgods | |
getgodrecommendeditems | |
getitems | |
getmatchdetails | |
getmatchplayerdetails | |
getmatchidsbyqueue | |
getleagueleaderboard | |
getleagueseasons | |
getmatchhistory | |
getmotd | |
getplayer | |
getplayerstatus | |
getqueuestats | |
getteamdetails | |
getteammatchhistory | |
getteamplayers | |
gettopmatches | |
searchteams | |
] | |
def initialize | |
@creds = Rails.application.secrets.api['smite'] | |
time = utc_time | |
response = Faraday.get [BASE_URL, 'createsessionJson', @creds['dev_id'], signature('createsession', time), time].join('/') | |
@session_id = JSON(response.body)['session_id'] | |
end | |
def self.ping | |
response = Faraday.get [BASE_URL, 'pingJson'].join('/') | |
return response.body | |
end | |
METHODS.each do |method| | |
define_method(method.to_sym) do |*args| | |
request(method, *args) | |
end | |
end | |
private | |
def request(method, *args) | |
time = utc_time | |
response = Faraday.get [BASE_URL, "#{method}Json", @creds['dev_id'], signature(method, time), @session_id, time, *args].join('/') | |
JSON(response.body) | |
rescue JSON::ParserError | |
response.body # return a string | |
end | |
def signature(method, time) | |
string = [@creds['dev_id'], method, @creds['auth_key'], time].join | |
Digest::MD5.hexdigest(string) | |
end | |
def utc_time | |
Time.now.utc.strftime('%Y%m%d%H%M%S') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment