Method: Couchbase::Bucket::CouchRequest#initialize
- Defined in:
- ext/couchbase_ext/http.c
#initialize(*args) ⇒ Bucket::CouchRequest
Initialize new CouchRequest
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; } |