Class: Couchbase::Bucket::CouchRequest

Inherits:
Object
  • Object
show all
Defined in:
ext/couchbase_ext/couchbase_ext.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Bucket::CouchRequest

Initialize new CouchRequest

Since:

  • 1.2.0



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/couchbase_ext/http.c', line 199

VALUE
cb_http_request_init(int argc, VALUE *argv, VALUE self)
{
    struct cb_http_request_st *request = DATA_PTR(self);
    VALUE bucket, path, opts, on_body, pp, arg;
    rb_scan_args(argc, argv, "22", &bucket, &pp, &opts, &on_body);

    if (NIL_P(on_body) && rb_block_given_p()) {
        on_body = rb_block_proc();
    }
    if (!RTEST(rb_obj_is_kind_of(bucket, cb_cBucket))) {
        rb_raise(rb_eTypeError, "wrong argument type (expected Couchbase::Bucket)");
    }
    memset(&request->cmd, 0, sizeof(lcb_http_cmd_t));
    request->type = LCB_HTTP_TYPE_VIEW;
    request->on_body_callback = on_body;
    request->bucket = DATA_PTR(bucket);
    request->bucket_obj = bucket;
    request->extended = Qfalse;
    path = StringValue(pp);	/* convert path to string */
    request->cmd.v.v0.path = strdup(RSTRING_PTR(path));
    request->cmd.v.v0.npath = RSTRING_LEN(path);
    request->cmd.v.v0.method = LCB_HTTP_METHOD_GET;
    request->cmd.v.v0.content_type = strdup("application/json");

    if (opts != Qnil) {
        Check_Type(opts, T_HASH);
        request->extended = RTEST(rb_hash_aref(opts, cb_sym_extended));
        request->cmd.v.v0.chunked = RTEST(rb_hash_aref(opts, cb_sym_chunked));
        if ((arg = rb_hash_aref(opts, cb_sym_type)) != Qnil) {
            if (arg == cb_sym_view) {
                request->type = LCB_HTTP_TYPE_VIEW;
            } else if (arg == cb_sym_management) {
                request->type = LCB_HTTP_TYPE_MANAGEMENT;
            } else {
                rb_raise(rb_eArgError, "unsupported request type");
            }
        }
        if ((arg = rb_hash_aref(opts, cb_sym_method)) != Qnil) {
            if (arg == cb_sym_get) {
                request->cmd.v.v0.method = LCB_HTTP_METHOD_GET;
            } else if (arg == cb_sym_post) {
                request->cmd.v.v0.method = LCB_HTTP_METHOD_POST;
            } else if (arg == cb_sym_put) {
                request->cmd.v.v0.method = LCB_HTTP_METHOD_PUT;
            } else if (arg == cb_sym_delete) {
                request->cmd.v.v0.method = LCB_HTTP_METHOD_DELETE;
            } else {
                rb_raise(rb_eArgError, "unsupported HTTP method");
            }
        }
        if ((arg = rb_hash_aref(opts, cb_sym_body)) != Qnil) {
            Check_Type(arg, T_STRING);
            request->cmd.v.v0.body = strdup(RSTRING_PTR(arg));
            request->cmd.v.v0.nbody = RSTRING_LEN(arg);
        }
        if ((arg = rb_hash_aref(opts, cb_sym_content_type)) != Qnil) {
            Check_Type(arg, T_STRING);
            free((char *)request->cmd.v.v0.content_type);
            request->cmd.v.v0.content_type = strdup(RSTRING_PTR(arg));
        }
    }

    return self;
}

Instance Attribute Details

#chunkedObject (readonly) Also known as: chunked?

rb_define_attr(cb_cCouchRequest, “chunked”, 1, 0);



386
387
388
389
390
391
# File 'ext/couchbase_ext/http.c', line 386

VALUE
cb_http_request_chunked_get(VALUE self)
{
    struct cb_http_request_st *req = DATA_PTR(self);
    return req->cmd.v.v0.chunked ? Qtrue : Qfalse;
}

#extendedObject (readonly) Also known as: extended?

rb_define_attr(cb_cCouchRequest, “extended”, 1, 0);



400
401
402
403
404
405
# File 'ext/couchbase_ext/http.c', line 400

VALUE
cb_http_request_extended_get(VALUE self)
{
    struct cb_http_request_st *req = DATA_PTR(self);
    return req->extended ? Qtrue : Qfalse;
}

#pathObject (readonly)

rb_define_attr(cb_cCouchRequest, “path”, 1, 0);



372
373
374
375
376
377
# File 'ext/couchbase_ext/http.c', line 372

VALUE
cb_http_request_path_get(VALUE self)
{
    struct cb_http_request_st *req = DATA_PTR(self);
    return STR_NEW_CSTR(req->cmd.v.v0.path);
}

Instance Method Details

#continueObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'ext/couchbase_ext/http.c', line 343

VALUE
cb_http_request_continue(VALUE self)
{
    VALUE exc, rv;
    struct cb_http_request_st *req = DATA_PTR(self);

    if (req->running) {
        lcb_wait(req->bucket->handle);
        if (req->completed) {
            exc = req->ctx->exception;
            rv = req->ctx->rv;
            cb_context_free(req->ctx);
            if (exc != Qnil) {
                rb_exc_raise(exc);
            }
            return rv;
        }
    } else {
        cb_http_request_perform(self);
    }
    return Qnil;
}

#inspectString

Returns a string containing a human-readable representation of the CouchRequest.

Returns:

Since:

  • 1.2.0



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/couchbase_ext/http.c', line 174

VALUE
cb_http_request_inspect(VALUE self)
{
    VALUE str;
    struct cb_http_request_st *req = DATA_PTR(self);
    char buf[200];

    str = rb_str_buf_new2("#<");
    rb_str_buf_cat2(str, rb_obj_classname(self));
    snprintf(buf, 20, ":%p \"", (void *)self);
    rb_str_buf_cat2(str, buf);
    rb_str_buf_cat2(str, req->cmd.v.v0.path);
    snprintf(buf, 100, "\" chunked:%s>", req->cmd.v.v0.chunked ? "true" : "false");
    rb_str_buf_cat2(str, buf);

    return str;
}

#on_bodyObject

Set on_body callback

Since:

  • 1.2.0



270
271
272
273
274
275
276
277
278
279
# File 'ext/couchbase_ext/http.c', line 270

VALUE
cb_http_request_on_body(VALUE self)
{
    struct cb_http_request_st *request = DATA_PTR(self);
    VALUE old = request->on_body_callback;
    if (rb_block_given_p()) {
        request->on_body_callback = rb_block_proc();
    }
    return old;
}

#pauseObject



335
336
337
338
339
340
341
# File 'ext/couchbase_ext/http.c', line 335

VALUE
cb_http_request_pause(VALUE self)
{
    struct cb_http_request_st *req = DATA_PTR(self);
    lcb_breakout(req->bucket->handle);
    return Qnil;
}

#performObject

Since:

  • 1.2.0



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'ext/couchbase_ext/http.c', line 286

VALUE
cb_http_request_perform(VALUE self)
{
    struct cb_http_request_st *req = DATA_PTR(self);
    struct cb_context_st *ctx;
    VALUE rv, exc;
    lcb_error_t err;
    struct cb_bucket_st *bucket = req->bucket;

    if (!cb_bucket_connected_bang(bucket, cb_sym_http_request)) {
        return Qnil;
    }

    ctx = cb_context_alloc(bucket);
    ctx->rv = Qnil;
    ctx->proc = rb_block_given_p() ? rb_block_proc() : req->on_body_callback;
    ctx->extended = req->extended;
    ctx->request = req;
    ctx->headers_val = rb_hash_new();

    err = lcb_make_http_request(bucket->handle, (const void *)ctx,
            req->type, &req->cmd, &req->request);
    exc = cb_check_error(err, "failed to schedule document request",
            STR_NEW(req->cmd.v.v0.path, req->cmd.v.v0.npath));
    if (exc != Qnil) {
        lcb_cancel_http_request(bucket->handle, req->request);
        rb_exc_raise(exc);
    }
    req->running = 1;
    req->ctx = ctx;
    if (bucket->async) {
        return Qnil;
    } else {
        lcb_wait(bucket->handle);
        if (req->completed) {
            rv = ctx->rv;
            exc = ctx->exception;
            cb_context_free(ctx);
            if (exc != Qnil) {
                rb_exc_raise(exc);
            }
            return rv;
        } else {
            return Qnil;
        }
    }
    return Qnil;
}