Created
March 25, 2016 20:32
-
-
Save rondale-sc/6c7d5df4f1f2304cef4f 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://127.0.0.1:4000/api" | |
end | |
class Episode < Base | |
has_many :show_notes | |
# has_many :guests | |
end | |
class Resource < Base | |
has_many :authors | |
end | |
class ShowNote < Base | |
belongs_to :resource | |
end | |
class Person < Base | |
end | |
end | |
token = "rR9ewjT16Fvol3+BaYj0F1H8rj9bhu7g" | |
def show_notes | |
show_notes = File.read(File.expand_path('~/dev/ember/ember-weekend/app/utils/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/app/utils/prod-fixtures/episodes.js')) | |
eval(episodes.gsub!(/export default/,'')) | |
end | |
MyApi::Base.connection do |connection| | |
connection.faraday.headers['Authorization'] = "Token #{token}" | |
end | |
episodes.reverse.each.with_index do |e, i| | |
je = MyApi::Episode.new({ | |
number: i+1, | |
title: e[:title], | |
description: e[:description], | |
slug: e[:slug], | |
"release-date": Date.parse(e[:releaseDate]).rfc3339, | |
filename: e[:filename], | |
duration: e[:duration] | |
}) | |
je.save | |
end | |
show_notes.each do |show_note| | |
show_note.fetch(:authors,[]).each do |author| | |
lookup = Hash[MyApi::Person.all.map {|p| [ p.handle, p.id ]}] | |
handle = author[:title].downcase.tr(' ', '-') | |
unless lookup[handle] | |
p = MyApi::Person.new({ | |
name: author[:title], | |
handle: handle, | |
url: author[:link], | |
'avatar-url' => 'https://example.com' | |
}) | |
p.save | |
end | |
end | |
end | |
show_notes.each do |show_note| | |
lookup = Hash[MyApi::Person.all.map {|p| [ p.handle, p.id ]}] | |
episode_lookup = Hash[MyApi::Episode.all.map {|e| [ e.number, e.id ] }] | |
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 = lookup[handle] | |
if id | |
MyApi::Person.find(id).first | |
else | |
MyApi::Person.new({ | |
name: author[:title], | |
handle: handle, | |
url: author[:link], | |
'avatar-url' => 'https://example.com' | |
}).save | |
end | |
end | |
resource = MyApi::Resource.new(doc) | |
resource.relationships.authors = authors | |
sn = MyApi::ShowNote.new(time_stamp: show_note[:timeStamp]) | |
sn.relationships.resource = resource | |
sn.relationships.episode = MyApi::Episode.find(episode_lookup[sn.number]).first | |
sn.save | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment