Skip to content
Permalink
Browse files

Initial feature set for Relaxo

  • Loading branch information...
ioquatix committed Jul 11, 2012
0 parents commit 097e12136d1b98efb2a9c733471394869e8a7646
Showing with 995 additions and 0 deletions.
  1. +37 −0 README.md
  2. +74 −0 bin/relaxo
  3. +39 −0 build/Rakefile
  4. +22 −0 build/master.rb
  5. +39 −0 lib/relaxo.rb
  6. +112 −0 lib/relaxo/client.rb
  7. +171 −0 lib/relaxo/database.rb
  8. +220 −0 lib/relaxo/model.rb
  9. +129 −0 lib/relaxo/properties.rb
  10. +35 −0 lib/relaxo/properties/money.rb
  11. +61 −0 lib/relaxo/recordset.rb
  12. +27 −0 lib/relaxo/server.rb
  13. +29 −0 lib/relaxo/version.rb
@@ -0,0 +1,37 @@
Relaxo
======

* Author: Samuel G. D. Williams (<http://www.oriontransfer.co.nz>)
* Copyright (C) 2010, 2011 Samuel G. D. Williams.
* Released under the MIT license.

Relaxo provides a set of tools and objects for interacting with CouchDB. It aims to be as simple and efficient as possible while still improving the usability of various CouchDB features.

Relaxo includes a command line script to import documents into a CouchDB database:

relaxo http://localhost:5984/test design.yaml sample.yaml

Where `design.yaml` and `sample.yaml` contain lists of valid documents.

License
-------

Copyright (c) 2010, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,74 @@
#!/usr/bin/env ruby

require 'relaxo'
require 'yaml'

OPTIONS = {
:existing => :merge
}

if ARGV.delete('--merge')
OPTIONS[:existing] = :merge
elsif ARGV.delete('--replace')
OPTIONS[:existing] = :replace
elsif ARGV.delete('--update')
OPTIONS[:existing] = :update
end

$database = Relaxo.connect(ARGV.shift)
$documents = YAML::load(ARGF)

unless Array === $documents
$documents = [$documents]
end

$stats = {
:saved => 0,
:errors => 0,
:total => 0
}

$documents.each do |document|
begin
$stderr.puts "Loading #{document.inspect}"
existing_document = nil

if document && document[Relaxo::ID]
existing_document = $database.get(document[Relaxo::ID]) rescue nil
end

if existing_document
if OPTIONS[:existing] == :replace
$stderr.puts "Replacing existing document..."
# The document is replaced entirely with the incoming data.
document[Relaxo::REV] = existing_document[Relaxo::REV]
elsif OPTIONS[:existing] == :update
$stderr.puts "Updating existing document..."
# Values in `existing_document` take priority
document.update(existing_document)
elsif OPTIONS[:existing] == :merge
$stderr.puts "Merging existing document..."
# Values in `document` take priority, except for `_rev`.
document.update(existing_document) do |key, oldval, newval|
key == Relaxo::REV ? newval : oldval
end
end
end

$stderr.puts "Saving #{document.inspect}"
$result = $database.save(document)

$stats[:saved] += 1
rescue RestClient::BadRequest => ex
$stderr.puts ex.inspect

$stats[:errors] += 1
end

$stats[:total] += 1
end

$stderr.puts "#{$stats[:saved]} document(s) saved out of #{$stats[:total]}."
if $stats[:errors] > 0
$stderr.puts "#{$stats[:errors]} errors occurred!"
end
@@ -0,0 +1,39 @@

# Automatic Gem Build Script v3.0

require 'fileutils'
require 'rubygems'

MasterGemspec = "master.rb"

GEM = ENV['GEM'] || "gem"

desc 'Build a gemspec file'
task :build_gemspec do
$spec = Gem::Specification.load(MasterGemspec)

$versioned_gem = "#{$spec.name}-#{$spec.version}.gem"
$versioned_gemspec = "#{$spec.name}-#{$spec.version}.gemspec"
$base_gemspec = "#{$spec.name}.gemspec"

puts "Building #{$versioned_gemspec}..."

File.open($versioned_gemspec, "w") do |f|
f.write($spec.to_ruby)
end

puts "Copying file to ../#{$base_gemspec}"
FileUtils.cp($versioned_gemspec, "../#{$base_gemspec}")
end

task :build_gem => [:build_gemspec] do
Dir.chdir("../") do
system("#{GEM} build #{$base_gemspec}")
end
end

task :install_gem => [:build_gem] do
Dir.chdir("../") do
system("#{GEM} install --local #{$versioned_gem}")
end
end
@@ -0,0 +1,22 @@

Dir.chdir("../") do
require './lib/relaxo/version'

Gem::Specification.new do |s|
s.name = "relaxo"
s.version = Relaxo::VERSION::STRING
s.author = "Samuel Williams"
s.email = "samuel.williams@oriontransfer.co.nz"
s.homepage = "http://www.oriontransfer.co.nz/gems/relaxo"
s.platform = Gem::Platform::RUBY
s.summary = "Relaxo is a helper for loading and working with CouchDB."
s.files = FileList["{bin,lib,test}/**/*"] + ["README.md"]

s.executables << 'relaxo'

s.add_dependency("couchrest")
s.add_dependency("rest-client")

s.has_rdoc = "yard"
end
end
@@ -0,0 +1,39 @@
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'relaxo/properties'
require 'relaxo/model'
require 'relaxo/database'

module Relaxo
def self.connect(url)
host = "http://localhost:5984"

if url =~ /^(https?:\/\/.+?)\/(.+)$/
host = $1
name = $2
else
name = url
end

server = Server.new(host)
database = Database.new(server, name)
end
end
@@ -0,0 +1,112 @@
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'json'
require 'rest_client'
require 'cgi'

module Relaxo
module Client
DEFAULT_GET_HEADERS = {:accept => :json}
DEFAULT_PUT_HEADERS = {:accept => :json, :content_type => :json}

def self.execute(request)
# Poor mans debugging:
# $stderr.puts "#{request[:method]} #{request[:url]}..."

response = RestClient::Request.execute(request)
return JSON.parse(response)
end

def self.get(url)
execute({
:method => :get,
:url => url,
:headers => DEFAULT_GET_HEADERS
})
end

def self.head(url)
execute({
:method => :head,
:url => url,
:headers => DEFAULT_GET_HEADERS
})
end

def self.put(url, document)
execute({
:method => :put,
:url => url,
:headers => DEFAULT_PUT_HEADERS,
:payload => JSON.generate(document)
})
end

def self.post(url, document)
execute({
:method => :post,
:url => url,
:headers => DEFAULT_PUT_HEADERS,
:payload => JSON.generate(document)
})
end

def self.delete(url)
execute({
:method => :delete,
:url => url,
:headers => DEFAULT_PUT_HEADERS
})
end

def self.escape(component)
return CGI.escape(component)
end

def self.encode_parameters(parameters)
query = []

parameters.each do |key, value|
key_string = key.to_s

if Symbol === value || key_string.end_with?("docid")
query << escape(key_string) + '=' + escape(value.to_s)
else
query << escape(key_string) + '=' + escape(value.to_json)
end
end

return '?' + query.join('&')
end

def self.encode_url(url, parameters = {})
if parameters.empty?
url
else
url + encode_parameters(parameters)
end
end

def self.escape_id(id)
/^_design\/(.*)/ =~ id ? "_design/#{escape($1)}" : escape(id)
end
end
end

0 comments on commit 097e121

Please sign in to comment.
You can’t perform that action at this time.