Created
March 28, 2011 08:31
-
-
Save obi-a/890154 to your computer and use it in GitHub Desktop.
Ruby recipes for couchDB - Still a work in progress
This file contains hidden or 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 'rubygems' | |
require 'rest_client' | |
require 'yajl' | |
begin | |
#create a db | |
#response = RestClient.put 'http://127.0.0.1:5984/contacts', {:content_type => :json} | |
#c = response.to_str | |
#puts c | |
#all databases | |
#response = RestClient.get 'http://127.0.0.1:5984/_all_dbs', {:content_type => :json} | |
#c = response.to_str | |
#puts c | |
#delete a database | |
#response = RestClient.delete 'http://127.0.0.1:5984/books', {:content_type => :json} | |
#c = response.to_str | |
#puts c | |
#get contacts all values | |
#response = RestClient.get 'http://127.0.0.1:5984/contacts', {:content_type => :json} | |
#hash = Yajl::Parser.parse(response.to_str) | |
#puts hash.inspect | |
def add() | |
#create a new document | |
monitor = { :monitor => 'url', | |
:every => '2m', | |
:test => 'Github Repo Test', | |
:url => 'https://github.com/obi-a/Ragios', | |
:contact => '[email protected]', | |
:via => 'gmail', | |
:notify_interval => '6h' | |
} | |
str = Yajl::Encoder.encode(monitor) | |
#get contacts all values | |
response = RestClient.put "http://127.0.0.1:5984/monitors/3", str, {:content_type => :json, :accept => :json} | |
hash = Yajl::Parser.parse(response.to_str) | |
puts hash.inspect | |
end | |
def Edit_doc | |
#edit a document | |
monitor = { :_rev => '1-6778b125a8600c599d52b064dc4018fe', | |
:monitor => 'url', | |
:every => '2m', | |
:test => 'Github Repo Test', | |
:url => 'https://github.com/obi-a/Ragios', | |
:contact => '[email protected]', | |
:via => 'gmail', | |
:notify_interval => '6h', | |
:total_num_of_tests => '180' | |
} | |
str = Yajl::Encoder.encode(monitor) | |
response = RestClient.put 'http://127.0.0.1:5984/monitors/3',str, {:content_type => :json, :accept => :json} | |
hash = Yajl::Parser.parse(response.to_str) | |
puts hash.inspect | |
end | |
#display all docs | |
#response = RestClient.get 'http://127.0.0.1:5984/monitors/_all_docs', {:content_type => :json} | |
#hash = Yajl::Parser.parse(response.to_str) | |
#puts hash.inspect | |
#display all docs -- in descending order | |
#response = RestClient.get 'http://127.0.0.1:5984/monitors/_all_docs?descending=true', {:content_type => :json} | |
#hash = Yajl::Parser.parse(response.to_str) | |
#puts hash.inspect | |
#display all docs -- in descending order limit 2 | |
#response = RestClient.get 'http://127.0.0.1:5984/monitors/_all_docs?descending=true&limit=2', {:content_type => :json} | |
#hash = Yajl::Parser.parse(response.to_str) | |
#puts hash.inspect | |
#display all docs --include docs in results | |
#response = RestClient.get 'http://127.0.0.1:5984/monitors/_all_docs?include_docs=true', {:content_type => :json} | |
#hash = Yajl::Parser.parse(response.to_str) | |
#puts hash.inspect | |
#retrieve a document by ID | |
doc_id = 'fc6db4a5-9a43-42cf-8817-6bc0e3c22542' | |
begin | |
response = RestClient.get 'http://127.0.0.1:5984/monitors/' + doc_id | |
hash = Yajl::Parser.parse(response.to_str) | |
puts hash.inspect | |
rescue => e | |
hash = Yajl::Parser.parse(e.response.to_s) | |
puts hash.inspect | |
end | |
def get_rows() | |
#return array of rows only | |
#display only rows on each doc | |
response = RestClient.get 'http://127.0.0.1:5984/monitors/_all_docs?include_docs=true', {:content_type => :json} | |
hash = Yajl::Parser.parse(response.to_str) | |
#puts hash.inspect | |
rows = hash["rows"] | |
only_rows = [] | |
count = 0 | |
rows.each do |row| | |
only_rows[count] = row["doc"] | |
count = count + 1 | |
end | |
puts only_rows.inspect | |
return only_rows | |
end | |
rescue => e | |
#rescue Exception | |
#puts response.inspect | |
#puts e.inspect | |
puts "failed" | |
#hash = Yajl::Parser.parse(e.response.to_s) | |
#puts hash.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment