Skip to content

Instantly share code, notes, and snippets.

require 'json'
require 'securerandom'
require 'couchbase'
couchbase = Couchbase.connect(:bucket => "users",:hostname => "localhost")
def generate_offers
offers = ['324-567-343','888-756-343','343-645-121','691-809-507','192-343-572','298-897-673']
number_of_offers = rand(1..3)
selected_offers = []
{
"id": "-JEVHOVwyze3I9u1G_JHhg",
"doc_type": "user",
"join_date": "2014-01-29 03:40:05 UTC",
"last_active": "2014-02-03 04:54:50 UTC",
"number_of_visits": 4768,
"origin": "RU"
}
function (doc, meta) {
if(meta.type == "json") {
if(doc.doc_type && doc.doc_type == "user") {
if(doc.join_date) {
emit(dateToArray(doc.join_date));
}
}
}
}
function (doc, meta) {
if(meta.type == "json") {
if(doc.doc_type && doc.doc_type == "user") {
if(doc.last_active && doc.origin) {
emit([doc.origin,dateToArray(doc.last_active)]);
}
}
}
}
function (doc, meta) {
if(meta.type == "json") {
if(doc.doc_type && doc.doc_type == "user") {
if(doc.origin) {
emit(doc.origin);
}
}
}
}
{
"id": "lRkFyEuBLa7AnGGS1jfagQ",
"doc_type": "user",
"join_date": "2014-02-23 09:18:52 UTC",
"last_active": "2014-02-23 10:55:29 UTC",
"number_of_visits": 4227,
"origin": "GB",
"offers": [
"298-897-673",
"888-756-343",
function (doc, meta) {
if(meta.type == "json") {
if(doc.doc_type == "user") {
if(doc.offers) {
emit(doc.offers);
}
}
}
}
require 'json'
require 'couchbase'
@couchbase = Couchbase.connect(:bucket => "users",:hostname => "localhost")
def retreive_offer_data
design_doc = @couchbase.design_docs["users"]
offers_collected = {}
offers_collected['date'] = Time.now.utc.to_s
{
"date": "2014-02-23 17:46:47 UTC",
"doc_type": "offers_collected",
"[192-343-572]": 165,
"[298-897-673]": 171,
"[324-567-343]": 184,
"[343-645-121]": 160,
"[691-809-507]": 148,
"[888-756-343]": 172
}
function (doc, meta) {
if(meta.type == 'json') {
if(doc.doc_type == "offers_collected" && doc.date) {
emit(dateToArray(doc.date));
}
}
}