spraints (owner)

Revisions

gist: 115709 Download_button fork
public
Public Clone URL: git://gist.github.com/115709.git
Embed All Files: show embed
couchdb-examples.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# To run this script, you need to install ruby, rubygems, the httparty gem,
# and any of the json gems.
require 'rubygems'
require 'httparty'
require 'json'
require 'pp'
 
class CouchDb
  include HTTParty
  format :json
  base_uri 'http://localhost:5984/'
 
  class << self
    # Add some logging
    [:post, :get, :delete, :put].each do |method|
      define_method method do |*args|
        puts "", "#{method}(#{args.inspect})"
        result = super
        pp result.delegate
        #puts result.inspect
        result
      end
    end
 
    def temp_view(db, view_definition)
      post(db + '/_temp_view', :body => view_definition.to_json, :headers => { 'Content-type' => 'application/json' })
    end
  end
end
 
def urlencode(str)
  str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
end
 
EmployeeDb = '/employees_example'
BlogDb = '/blog_example'
 
CouchDb.get("/_all_dbs", :format => :txt)
 
CouchDb.delete(EmployeeDb)
CouchDb.put(EmployeeDb)
resp = CouchDb.put(EmployeeDb + '/gwashington', :body => {}.to_json)
CouchDb.post(EmployeeDb + '/gwashington', :body =>
  {:name => "George Washington", :rev => resp['rev'], :titles => [
    {"begin" => 1773, "end" => 1785, "title" => "Commander of the Continental Army"},
    {"begin" => 1789, "end" => 1797, "title" => "President of the United States"},
    {"begin" => 1798, "end" => 1799, "title" => "United States Army Senior Officer"}
]}.to_json)
CouchDb.put(EmployeeDb + '/jadams', :body => {:name => "John Adams", :titles => [
  {"begin" => 1789, "end" => 1797, "title" => "Vice President of the United States"},
  {"begin" => 1797, "end" => 1801, "title" => "President of the United States"}
]}.to_json)
 
puts "", "Default View"
CouchDb.temp_view(EmployeeDb, {:map => <<END_MAP})
function(doc) { emit(null, doc); }
END_MAP
 
puts "", "Titles, by year"
by_year_map = <<END_MAP
function(doc) {
var i, title, year;
if(doc.titles && doc.name)
{
for(i = 0; i < doc.titles.length; i++)
{
title = doc.titles[i];
for (year = title.begin; year <= title.end; year++)
{
emit(year, {"name":doc.name, "begin":title.begin, "end": title.end, "title": title.title});
}
}
}
}
END_MAP
CouchDb.temp_view(EmployeeDb, {:map => by_year_map})
 
puts "", "Permanent Views"
CouchDb.put(EmployeeDb + '/_design/titles', :body => {
  :language => 'javascript',
  :views => {
     :by_year => {
       :map => by_year_map
     }
  }
}.to_json)
CouchDb.get(EmployeeDb + '/_design/titles/_view/by_year')
CouchDb.get(EmployeeDb + '/_design/titles/_view/by_year?key=1796')
 
 
CouchDb.delete(BlogDb)
CouchDb.put(BlogDb)
 
CouchDb.put(BlogDb + '/post1', :body => {
  :type => 'post',
  :title => 'My First Post!',
  :content => 'Welcome to my blog! I\'ll tell you all about the stuff I do, day to day.'
}.to_json)
CouchDb.put(BlogDb + '/post2', :body => {
  :type => 'post',
  :title => 'Report',
  :content => 'Today, I typed a bunch of TPS reports.'
}.to_json)
CouchDb.post(BlogDb + '/', :body => {
  :type => 'comment',
  :post_id => 'post1',
  :title => 'Awesome!',
  :content => 'I\'m totally looking forward to reading this blog!',
}.to_json)
CouchDb.post(BlogDb + '/', :body => {
  :type => 'comment',
  :post_id => 'post2',
  :content => 'You had to do that too??!?!?! No freakin\' way!'
}.to_json)
CouchDb.post(BlogDb + '/', :body => {
  :type => 'comment',
  :post_id => 'post2',
  :title => 'Good work!',
  :content => 'I read your reports, and they are really top notch!'
}.to_json)
 
all_posts_map = <<END_MAP
function(doc)
{
if(doc.type && doc.type == 'post')
{
emit(doc._id, doc);
}
}
END_MAP
with_comments_map = <<END_MAP
function(doc)
{
if(doc.type)
{
if(doc.type == 'post') emit([doc._id, 0], doc);
if(doc.type == 'comment') emit([doc.post_id, 1], doc);
}
}
END_MAP
comments_per_post_map = <<END_MAP
function(doc)
{
if(doc.type && doc.type == 'comment')
{
emit(doc.post_id, 1);
}
}
END_MAP
comments_per_post_reduce = <<END_REDUCE
function(key, values, rereduce)
{
return sum(values);
}
END_REDUCE
CouchDb.put(BlogDb + '/_design/posts', :body => {
  :language => 'javascript',
  :views => {
     :all => {
       :map => all_posts_map
     },
     :with_comments => {
       :map => with_comments_map
     },
     :comments_per_post => {
       :map => comments_per_post_map,
       :reduce => comments_per_post_reduce
     },
  }
}.to_json)
CouchDb.get(BlogDb + '/_design/posts/_view/all')
CouchDb.get(BlogDb + '/_design/posts/_view/with_comments?startkey=' + urlencode(['post2', 0].to_json) + '&endkey=' + urlencode(['post2', 1].to_json))
CouchDb.get(BlogDb + '/_design/posts/_view/comments_per_post?group=true')