Skip to content

Instantly share code, notes, and snippets.

@johndel
Created November 23, 2013 18:10
Show Gist options
  • Save johndel/7617994 to your computer and use it in GitHub Desktop.
Save johndel/7617994 to your computer and use it in GitHub Desktop.
Parse json and store the results to a database
require 'rubygems'
require 'json'
require 'httparty'
require 'active_record'
require 'pry'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:database => 'lol',
:username => 'yourusername',
:password => 'yourpass',
:host => 'localhost')
class Hero < ActiveRecord::Base
end
response = HTTParty.get('http://localhost/championFull.json')
json = JSON.parse(response.body)
json["data"].each do |hero|
unless Hero.where(name: hero[1]["name"]).first
power = hero[1]["info"]["attack"] + hero[1]["info"]["defense"] + hero[1]["info"]["magic"]
hero = Hero.new(name: hero[1]["name"], title: hero[1]["title"], category: hero[1]["tags"].first, attack: hero[1]["info"]["attack"], defense: hero[1]["info"]["defense"], magic: hero[1]["info"]["magic"], difficulty: hero[1]["info"]["difficulty"], power: power)
hero.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment