Created
March 9, 2017 08:45
-
-
Save snapcase/24277f019674dfbb0c8cc47d04abd331 to your computer and use it in GitHub Desktop.
armory profile
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
require 'httparty' | |
class Armory | |
include HTTParty | |
base_uri 'https://eu.api.battle.net/wow' | |
CLASS = { | |
1 => { name: 'Warrior', color: 'C79C6E' }, | |
2 => { name: 'Paladin', color: 'F58CBA' }, | |
3 => { name: 'Hunter', color: 'ABD473' }, | |
4 => { name: 'Rogue', color: 'FFF569' }, | |
5 => { name: 'Priest', color: 'FFFFFF' }, | |
6 => { name: 'Death Knight', color: 'C41F3B' }, | |
7 => { name: 'Shaman', color: '0070DE' }, | |
8 => { name: 'Mage', color: '69CCF0' }, | |
9 => { name: 'Warlock', color: '9482C9' }, | |
10 => { name: 'Monk', color: '00FF96' }, | |
11 => { name: 'Druid', color: 'FF7D0A' }, | |
12 => { name: 'Demon Hunter', color: 'A330C9' } | |
} | |
def initialize(key) | |
self.class.default_params apikey: key, locale: 'en_GB' | |
end | |
# get achievements and itemlevel | |
def character(realm, char) | |
char = URI.encode(char) | |
options = { query: { fields: 'items,titles,professions,achievements' } } | |
json = self.class.get("/character/#{realm}/#{char}", options) | |
return unless json.code == 200 # something broke | |
# moo | |
name = json['name'] | |
title = json['titles'].find { |h| h['selected'] } | |
# update name if there's a title | |
name = json['titles'].find { |h| h['selected'] }['name'].gsub('%s', name) if title | |
prof = json['professions']['primary'].map { |p| p['name'] } # Array | |
wclass = json['class'] | |
lvl = json['level'] | |
# AP | |
i = json['achievements']['criteria'].index(30103) | |
ap = i ? json['achievements']['criteriaQuantity'][i].to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse : 0 | |
# traits | |
hand = json['items']['mainHand']['artifactTraits'].empty? ? 'offHand' : 'mainHand' | |
relics = json['items'][hand]['relics'].size | |
traits = json['items'][hand]['artifactTraits'].map { |x| | |
x['rank'] | |
}.inject(:+) - relics | |
{ | |
name: name, | |
avatar: json['thumbnail'], | |
last: json['lastModified'] / 1000, | |
class: CLASS[wclass][:name], | |
color: CLASS[wclass][:color], | |
lvl: json['level'], | |
realm: json['realm'], | |
profs: prof, | |
ilvleq: json['items']['averageItemLevelEquipped'], | |
ilvl: json['items']['averageItemLevel'], | |
achi: json['achievementPoints'], | |
ap: ap, | |
traits: traits | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment