Last active
April 26, 2016 02:09
-
-
Save rondale-sc/5ef4375ca9821052d55d757646d35c77 to your computer and use it in GitHub Desktop.
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 'json_api_client' | |
module MyApi | |
# this is an "abstract" base class that | |
class Base < JsonApiClient::Resource | |
# set the api base url in an abstract base class | |
self.site = "http://localhost:4000/api" | |
end | |
class Episode < Base | |
has_many "show-notes" | |
has_many :guests | |
end | |
class Resource < Base | |
has_many :authors | |
end | |
class ShowNote < Base | |
def self.table_name | |
"show-notes" | |
end | |
end | |
class Person < Base | |
end | |
end | |
# token = "cbUzzBba0uEd72+IDXU7BG2w7DoRtG1z" # prod | |
token = "k6h/jDqpRNIRk3zJqisFPTL4+km4Xb+c" | |
def show_notes | |
show_notes = File.read(File.expand_path('~/dev/ember/ember-weekend/prod-fixtures/show-notes.js')) | |
show_notes.gsub!(/export default/,'') | |
show_notes.gsub!(/^\s+\/\/.*$/,'') | |
eval(show_notes) | |
end | |
def episodes | |
episodes = File.read(File.expand_path('~/dev/ember/ember-weekend/prod-fixtures/episodes.js')) | |
eval(episodes.gsub!(/export default/,'')) | |
end | |
MyApi::Base.connection do |connection| | |
connection.faraday.headers['Authorization'] = "Token #{token}" | |
end | |
class EpisodeImporter | |
attr_reader :episode_lookup, :people_lookup | |
def initialize | |
@episode_lookup = Hash[MyApi::Episode.all.map {|e| [ e.number, e.id ] }] | |
@people_lookup = Hash[MyApi::Person.all.map {|p| [ p.handle, p.id ]}] | |
end | |
def create_episode(data,i) | |
id = i+1 | |
return if episode_lookup[id] | |
print '.' | |
je = MyApi::Episode.new({ | |
number: id, | |
title: data[:title], | |
description: data[:description], | |
slug: data[:slug], | |
"release-date": Date.parse(data[:releaseDate]).rfc3339, | |
filename: data[:filename], | |
duration: data[:duration] | |
}) | |
guests = (data[:guests] || []).map do |author| | |
handle = author[:name].downcase.tr(' ', '-') | |
if people_lookup[handle] | |
s = MyApi::Person.new( | |
name: author[:name], | |
handle: handle, | |
url: author[:guestInfoURL], | |
'avatar-url' => author[:avatarURL], | |
bio: author[:bio], | |
tagline: author[:tagLine] | |
) | |
s.save | |
s | |
end | |
end | |
je.relationships.guests = guests | |
je.save | |
end | |
end | |
class PeopleImporter | |
attr_reader :people_lookup | |
def initialize | |
@people_lookup = Hash[MyApi::Person.all.map {|p| [ p.handle, p.id ]}] | |
end | |
def create_people(authors) | |
authors.each do |author| | |
handle = author[:title].downcase.tr(' ', '-') | |
unless people_lookup[handle] | |
print "." | |
p = MyApi::Person.new({ | |
name: author[:title], | |
handle: handle, | |
url: author[:link], | |
'avatar-url' => 'https://example.com' | |
}) | |
p.save | |
end | |
end | |
end | |
end | |
class ShowNoteImporter | |
attr_reader :episode_lookup, :people_lookup, :resource_lookup | |
def initialize | |
@episode_lookup = Hash[MyApi::Episode.all.map {|e| [ e.number, e.id ] }] | |
@people_lookup = Hash[MyApi::Person.all.map {|p| [ p.handle, p.id ]}] | |
@resource_lookup = Hash[MyApi::Resource.all.map {|r| [ r.title, r.id] }] | |
end | |
def create_resource(show_note) | |
doc = { | |
title: show_note[:resource][:title], | |
url: show_note[:resource][:link] | |
} | |
authors = show_note.fetch(:authors, []).map do |author| | |
handle = author[:title].downcase.tr(' ', '-') | |
id = people_lookup[handle] | |
if id | |
MyApi::Person.find(id).first | |
else | |
p = MyApi::Person.new({ | |
name: author[:title], | |
handle: handle, | |
url: author[:link], | |
'avatar-url' => 'https://example.com' | |
}) | |
p.save | |
p | |
end | |
end | |
resource = MyApi::Resource.new(doc) | |
resource.relationships.authors = authors | |
resource.save | |
resource | |
end | |
def create_show_note(show_note) | |
return if resource_lookup[show_note[:resource][:title]] | |
doc = {"time-stamp" => show_note[:timeStamp]} | |
sn = MyApi::ShowNote.new(doc) | |
sn.relationships.resource = create_resource(show_note) | |
sn.relationships.episode = MyApi::Episode.find(episode_lookup[show_note[:episode]]).first | |
print "." | |
sn.save | |
end | |
end | |
puts "\nEpisodes:" | |
episode_importer = EpisodeImporter.new | |
episodes.reverse.each.with_index do |e, i| | |
episode_importer.create_episode(e,i) | |
end | |
puts "\nPeople:" | |
people_importer = PeopleImporter.new | |
show_notes.each do |show_note| | |
people_importer.create_people(show_note.fetch(:authors,[])) | |
end | |
puts "\nShow Notes:" | |
show_note_imporer = ShowNoteImporter.new | |
show_notes.each do |show_note| | |
show_note_imporer.create_show_note(show_note) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment