Class: Couchbase::Bucket::CouchRequest
- Inherits:
-
Object
- Object
- Couchbase::Bucket::CouchRequest
- Defined in:
- ext/couchbase_ext/couchbase_ext.c
Instance Attribute Summary collapse
-
#chunked ⇒ Object
(also: #chunked?)
readonly
rb_define_attr(cb_cCouchRequest, “chunked”, 1, 0);.
-
#extended ⇒ Object
(also: #extended?)
readonly
rb_define_attr(cb_cCouchRequest, “extended”, 1, 0);.
-
#path ⇒ Object
readonly
rb_define_attr(cb_cCouchRequest, “path”, 1, 0);.
Instance Method Summary collapse
- #continue ⇒ Object
-
#initialize(*args) ⇒ Bucket::CouchRequest
constructor
Initialize new CouchRequest.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of the CouchRequest.
-
#on_body ⇒ Object
Set
on_body
callback. - #pause ⇒ Object
-
#perform ⇒ Object
Execute CouchRequest.
Constructor Details
#initialize(*args) ⇒ Bucket::CouchRequest
Initialize new CouchRequest
187 188 189 190 191 192 193 194 195 196 197 198 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 |
# File 'ext/couchbase_ext/http.c', line 187
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 (CLASS_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
#chunked ⇒ Object (readonly) Also known as: chunked?
rb_define_attr(cb_cCouchRequest, “chunked”, 1, 0);
377 378 379 380 381 382 |
# File 'ext/couchbase_ext/http.c', line 377
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;
}
|
#extended ⇒ Object (readonly) Also known as: extended?
rb_define_attr(cb_cCouchRequest, “extended”, 1, 0);
391 392 393 394 395 396 |
# File 'ext/couchbase_ext/http.c', line 391
VALUE
cb_http_request_extended_get(VALUE self)
{
struct cb_http_request_st *req = DATA_PTR(self);
return req->extended ? Qtrue : Qfalse;
}
|
#path ⇒ Object (readonly)
rb_define_attr(cb_cCouchRequest, “path”, 1, 0);
363 364 365 366 367 368 |
# File 'ext/couchbase_ext/http.c', line 363
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
#continue ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'ext/couchbase_ext/http.c', line 333
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;
free(req->ctx);
if (exc != Qnil) {
cb_gc_unprotect(req->bucket, exc);
rb_exc_raise(exc);
}
return *rv;
}
} else {
cb_http_request_perform(self);
}
return Qnil;
}
|
#inspect ⇒ String
Returns a string containing a human-readable representation of the CouchRequest.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'ext/couchbase_ext/http.c', line 162
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_body ⇒ Object
Set on_body
callback
258 259 260 261 262 263 264 265 266 267 |
# File 'ext/couchbase_ext/http.c', line 258
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;
}
|
#pause ⇒ Object
325 326 327 328 329 330 331 |
# File 'ext/couchbase_ext/http.c', line 325
VALUE
cb_http_request_pause(VALUE self)
{
struct cb_http_request_st *req = DATA_PTR(self);
lcb_breakout(req->bucket->handle);
return Qnil;
}
|
#perform ⇒ Object
Execute Couchbase::Bucket::CouchRequest
274 275 276 277 278 279 280 281 282 283 284 285 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 |
# File 'ext/couchbase_ext/http.c', line 274
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;
ctx = calloc(1, sizeof(struct cb_context_st));
if (ctx == NULL) {
rb_raise(cb_eClientNoMemoryError, "failed to allocate memory");
}
rv = Qnil;
ctx->rv = &rv;
ctx->bucket = bucket = req->bucket;
ctx->proc = rb_block_given_p() ? cb_gc_protect(bucket, rb_block_proc()) : req->on_body_callback;
ctx->extended = req->extended;
ctx->request = req;
ctx->headers_val = cb_gc_protect(bucket, rb_hash_new());
ctx->exception = Qnil;
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) {
free(ctx);
rb_exc_raise(exc);
}
req->running = 1;
req->ctx = ctx;
if (bucket->async) {
return Qnil;
} else {
lcb_wait(bucket->handle);
if (req->completed) {
exc = ctx->exception;
free(ctx);
if (exc != Qnil) {
cb_gc_unprotect(bucket, exc);
rb_exc_raise(exc);
}
return rv;
} else {
return Qnil;
}
}
return Qnil;
}
|