Class: YAJI::Parser

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

Constant Summary collapse

READ_BUFFER_SIZE =
INT2FIX(READ_BUFSIZE)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



225
226
227
228
# File 'ext/yaji/parser_ext.c', line 225

static VALUE rb_yaji_parser_init(int argc, VALUE *argv, VALUE self)
{
	return self;
}

Class Method Details

.new(*args) ⇒ Object



179
180
181
182
183
184
185
186
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
# File 'ext/yaji/parser_ext.c', line 179

static VALUE rb_yaji_parser_new(int argc, VALUE *argv, VALUE klass)
{
	yaji_parser* p;
	VALUE opts, obj;

	obj = Data_Make_Struct(klass, yaji_parser, rb_yaji_parser_mark, rb_yaji_parser_free, p);
	p->handle = NULL;
	p->config.allowComments = 1;
	p->config.checkUTF8 = 1;
	p->symbolize_keys = 0;
	p->rbufsize = Qnil;
	p->input = Qnil;
	p->parser_cb = Qnil;

	rb_scan_args(argc, argv, "11", &p->input, &opts);
	if (TYPE(p->input) == T_STRING) {
		p->input = rb_class_new_instance(1, &p->input, c_stringio);
	} else if (rb_respond_to(p->input, id_perform) && rb_respond_to(p->input, id_on_body)) {
		rb_block_call(p->input, id_on_body, 0, NULL, rb_yaji_parser_parse_chunk, obj);
	} else if (!rb_respond_to(p->input, id_read)) {
		rb_raise(c_parse_error, "input must be a String or IO or "
				"something responding to #perform and #on_body e.g. Curl::Easy");
	}
	if (!NIL_P(opts)) {
		Check_Type(opts, T_HASH);
		if (rb_hash_aref(opts, sym_allow_comments) == Qfalse) {
			p->config.allowComments = 0;
		}
		if (rb_hash_aref(opts, sym_check_utf8) == Qfalse) {
			p->config.checkUTF8 = 0;
		}
		if (rb_hash_aref(opts, sym_symbolize_keys) == Qtrue) {
			p->symbolize_keys = 1;
		}
		p->rbufsize = rb_hash_aref(opts, sym_read_buffer_size);
	}
	if (NIL_P(p->rbufsize)) {
		p->rbufsize = INT2FIX(READ_BUFSIZE);
	} else {
		Check_Type(p->rbufsize, T_FIXNUM);
	}
	p->handle = yajl_alloc(&callbacks, &p->config, NULL, (void *)obj);
	rb_obj_call_init(obj, 0, 0);
	return obj;
}

Instance Method Details

#each(*args) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
# File 'ext/yaji/parser_ext.c', line 339

static VALUE rb_yaji_parser_each(int argc, VALUE* argv, VALUE self)
{
	VALUE query, proc, params[3];
	rb_scan_args(argc, argv, "01&", &query, &proc);
	RETURN_ENUMERATOR(self, argc, argv);
	params[0] = proc;	    // callback
	params[1] = rb_ary_new();   // stack
	params[2] = query;
	rb_block_call(self, id_parse, 0, NULL, rb_yaji_each_iter, (VALUE)params);
	return Qnil;
}

#parse(*args) ⇒ Object



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
# File 'ext/yaji/parser_ext.c', line 230

static VALUE rb_yaji_parser_parse(int argc, VALUE* argv, VALUE self)
{
	yajl_status rc;
	yaji_parser* p = (yaji_parser*) DATA_PTR(self);
	int i;

	rb_scan_args(argc, argv, "00&", &p->parser_cb);
	RETURN_ENUMERATOR(self, argc, argv);

	p->path = rb_ary_new();
	p->path_str = rb_str_new("", 0);
	p->chunk = Qnil;

	if (rb_respond_to(p->input, id_perform)) {
		rb_funcall(p->input, id_perform, 0);
	} else {
		p->chunk = rb_str_new(NULL, 0);
		while (rb_funcall(p->input, id_read, 2, p->rbufsize, p->chunk) != Qnil) {
			rb_yaji_parser_parse_chunk(p->chunk, self);
		}
	}

	p->events = rb_ary_new();
	rc = yajl_parse_complete(p->handle);
	if (rc == yajl_status_insufficient_data || rc == yajl_status_error) {
		RERAISE_PARSER_ERROR(p);
	}
	for (i=0; i<RARRAY_LEN(p->events); i++) {
		rb_funcall(p->parser_cb, id_call, 1, RARRAY_PTR(p->events)[i]);
	}

	return Qnil;
}