Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions coursera_syncer/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require "active_record"
require 'yaml'

namespace :db do

db_config = YAML::load(File.open('config/database.yml'))
db_config_admin = db_config.merge({'database' => 'postgres', 'schema_search_path' => 'public'})

desc "Create the database"
task :create do
ActiveRecord::Base.establish_connection(db_config_admin)
ActiveRecord::Base.connection.create_database(db_config["database"])
puts "Database created."
end

desc "Migrate the database"
task :migrate do
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Migrator.migrate("db/migrate/")
Rake::Task["db:schema"].invoke
puts "Database migrated."
end

desc "Drop the database"
task :drop do
ActiveRecord::Base.establish_connection(db_config_admin)
ActiveRecord::Base.connection.drop_database(db_config["database"])
puts "Database deleted."
end

desc "Reset the database"
task :reset => [:drop, :create, :migrate]

desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
task :schema do
ActiveRecord::Base.establish_connection(db_config)
require 'active_record/schema_dumper'
filename = "db/schema.rb"
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end

end

namespace :g do
desc "Generate migration"
task :migration do
name = ARGV[1] || raise("Specify name: rake g:migration your_migration")
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
path = File.expand_path("../db/migrate/#{timestamp}_#{name}.rb", __FILE__)
migration_class = name.split("_").map(&:capitalize).join

File.open(path, 'w') do |file|
file.write <<-EOF
class #{migration_class} < ActiveRecord::Migration
def self.up
end
def self.down
end
end
EOF
end

puts "Migration #{path} created"
abort # needed stop other tasks
end
end
5 changes: 5 additions & 0 deletions coursera_syncer/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# config/database.yml
host: 'localhost'
adapter: 'postgresql'
encoding: utf-8
database: 'coursera_syncer_test'
20 changes: 20 additions & 0 deletions coursera_syncer/coursera_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module CourseraClient
def self.get_courses
courses_response_body = get_raw_courses
raw_courses = courses_response_body['elements']
raw_courses.map do |raw_course|
{
coursera_id: raw_course['id'],
name: raw_course['name'],
description: raw_course['description'],
coursera_url: raw_course['previewLink']
# : raw_course['coursera_url'],
}
end
end

def self.get_raw_courses
# Assume we can send GET request to coursera and return raw course list
# JSON response body.
end
end
8 changes: 8 additions & 0 deletions coursera_syncer/coursera_syncer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative './coursera_client'

module CourseraSyncer
def self.sync!
courses_attributes = CourseraClient.get_courses
courses_attributes.each { |course_attrs| Course.create(course_attrs) }
end
end
11 changes: 11 additions & 0 deletions coursera_syncer/db/migrate/20160624144643_create_courses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateCourses < ActiveRecord::Migration
def change
create_table :courses do |t|
t.string :name
t.text :description
t.string :coursera_url
t.string :coursera_id
t.datetime :starts_at
end
end
end
29 changes: 29 additions & 0 deletions coursera_syncer/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160624144643) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "courses", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "coursera_url"
t.string "coursera_id"
t.datetime "starts_at"
t.datetime "created_at"
t.datetime "updated_at"
end

end
2 changes: 2 additions & 0 deletions coursera_syncer/models/course.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Course < ActiveRecord::Base
end
20 changes: 20 additions & 0 deletions coursera_syncer/spec/coursera_syncer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative './spec_helper'
require_relative '../coursera_syncer'
require_relative '../models/course'

describe CourseraSyncer do
before do
Course.destroy_all
file =
File.open(File.dirname(__FILE__) + '/fixtures/courses.json', 'rb').read
course_data = JSON.parse(file)
allow(CourseraClient).to receive(:get_raw_courses).and_return(course_data)
end
describe '::sync!' do
it 'syncs sources from coursera api course list' do
CourseraSyncer.sync!
expect(Course.count).not_to eq 0
expect(Course.find_by(coursera_id: 'v1-228')).not_to be_nil
end
end
end
1 change: 1 addition & 0 deletions coursera_syncer/spec/fixtures/courses.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions coursera_syncer/spec/models/course_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative '../spec_helper'
require_relative '../../models/course'

describe Course do
before { Course.destroy_all }
it 'can be persisted with attributes' do
Course.create(name: 'other name')
expect(Course.count).to eq 1
expect(Course.first.name).to eq 'other name'
end
end
8 changes: 8 additions & 0 deletions coursera_syncer/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'active_record'
require_relative '../models/course'

ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:database => 'coursera_syncer_test',
:host => 'localhost'
)