Skip to content

Instantly share code, notes, and snippets.

@alexdunae
Created April 2, 2015 00:00
Show Gist options
  • Save alexdunae/91e5860e717a2c575c31 to your computer and use it in GitHub Desktop.
Save alexdunae/91e5860e717a2c575c31 to your computer and use it in GitHub Desktop.
require 'json'
require 'open-uri'
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#while testing use the seeds data for parsing..
#when not testing create new ruby model(s) for parsing data
# step 0 - figure out which 'character' this is about
c = Character.find_or_initialize_by(name: 'Wraithshadow', server: 'Runetotem')
# step 1 - build URL
url = "http://us.battle.net/api/wow/character/#{c.server}/#{c.name}"
# step 2 - read data from URL
raw_data = open(url).read
# step 3 - parse data as JSON
json = JSON.parse(raw_data)
ap json
c.user = User.first
# step 4 - use parsed JSON to create Rails record
c.name = json['name']
c.server = json['realm']
c.race = json['race']
c.gender = convert_gender(json['gender'])
c.character_class = convert_character_class(json['class'])
c.level = json['level']
c.thumbnail = json['thumbnail']
#c.other_field = json['fields'][0]['otherFieldWeirdName']
c.save!
def convert_gender(value)
if value == '1'
'F'
else
'M'
end
end
def convert_character_class(value)
klasses = {
'1' => 'Druid',
'9' => 'Unicorn'
}
klasses[value]
end
# later
json['pets']['collected'].each do |pet|
pet = user.pets.find_or_initialize_by(unique_name: pet['unique_name'])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment