Skip to content

Instantly share code, notes, and snippets.

@bsaideepak
Created April 13, 2016 19:26
Show Gist options
  • Save bsaideepak/369b34c25991fd5049c3dc792b6c4855 to your computer and use it in GitHub Desktop.
Save bsaideepak/369b34c25991fd5049c3dc792b6c4855 to your computer and use it in GitHub Desktop.
Confreaks Video API Wrapper in ruby
#Rails project
#Database migration for persisting Video metadata
class CreateVideos < ActiveRecord::Migration
def change
create_table :videos do |t|
t.timestamps null: false
t.integer :video_id
t.text :title
t.text :event
t.text :abstract
end
end
end
#run database creation and migration commands, rake db:create and rake db:migrate
#Define route in config/routes.rb
get '/confVideo' => 'api_geek#getVideo'
#Defining a rails controller to pull data from the Confreaks.tv video API, storing in database, and rendering results
require 'net/http'
require 'json'
class ApiGeekController < ApplicationController
def getVideo
@result = Net::HTTP.get('confreaks.tv' ,'/api/v1/videos.json?limit=50')
@resultSet = JSON.parse(@result)
@resultSet.each do |i|
unless Video.exists?(video_id: i["id"])
Video.create(video_id: i["id"], title: i["title"], event: i["event"] ,abstract: i["abstract"])
end
end
@data = Video.all
render json: @data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment