Created
May 23, 2014 03:47
-
-
Save jpterry/f4934a52404e07d11cd4 to your computer and use it in GitHub Desktop.
Finds all .json freeswitch cdrs in the current directory and posts them to local couchdb.
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
#!/usr/bin/env ruby | |
require 'rubygems' unless defined?(Gem) | |
require 'json' | |
require 'pry' | |
require 'net/http' | |
module Couch | |
class Server | |
def initialize(host, port, options = nil) | |
@host = host | |
@port = port | |
@options = options | |
end | |
def delete(uri) | |
request(Net::HTTP::Delete.new(uri)) | |
end | |
def get(uri) | |
request(Net::HTTP::Get.new(uri)) | |
end | |
def put(uri, json) | |
req = Net::HTTP::Put.new(uri) | |
req["content-type"] = "application/json" | |
req.body = json | |
request(req) | |
end | |
def post(uri, json) | |
req = Net::HTTP::Post.new(uri) | |
req["content-type"] = "application/json" | |
req.body = json | |
request(req) | |
end | |
def request(req) | |
res = Net::HTTP.start(@host, @port) { |http|http.request(req) } | |
unless res.kind_of?(Net::HTTPSuccess) | |
handle_error(req, res) | |
end | |
res | |
end | |
private | |
def handle_error(req, res) | |
e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}") | |
raise e | |
end | |
end | |
end | |
filenames = Dir.glob("*.json") | |
DB_NAME = 'freeswitch_cdrs' | |
server = Couch::Server.new("localhost", "5984") | |
server.delete("/#{DB_NAME}") | |
server.put("/#{DB_NAME}/", "") | |
filenames.each_with_index do |file,i| | |
contents = File.read(file) | |
hash = JSON.parse(contents) | |
uuid = hash["variables"]["uuid"] | |
contents = hash["variables"].to_json | |
puts "#{i} #{uuid}" | |
server.put("/#{DB_NAME}/#{uuid}", contents) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment