diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2013-12-08 22:56:30 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2013-12-08 22:56:30 -0800 |
commit | fadb3e607a15636c36c06db84b2e00559bc8bdff (patch) | |
tree | 7601e51cd86a031ad691f10ba9d79af1ceb8f861 /lib | |
parent | caf3c7c938dd9e6571cda04fd9cc0034d1b24626 (diff) | |
download | ffi-yajl-fadb3e607a15636c36c06db84b2e00559bc8bdff.tar.gz |
move the ruby parse/encode classes to own files
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ffi_yajl.rb | 100 | ||||
-rw-r--r-- | lib/ffi_yajl/encoder.rb | 65 | ||||
-rw-r--r-- | lib/ffi_yajl/parser.rb | 36 |
3 files changed, 104 insertions, 97 deletions
diff --git a/lib/ffi_yajl.rb b/lib/ffi_yajl.rb index a49de6a..d5827bc 100644 --- a/lib/ffi_yajl.rb +++ b/lib/ffi_yajl.rb @@ -1,106 +1,12 @@ require 'rubygems' require 'ffi' +require 'ffi_yajl/encoder' +require 'ffi_yajl/parser' + module FFI_Yajl class ParseError < StandardError; end class EncodeError < StandardError; end - - class Encoder - attr_accessor :opts - - def encode(obj) - # initialization that we can do in pure ruby - yajl_gen_opts = {} - - yajl_gen_opts[:yajl_gen_validate_utf8] = true - yajl_gen_opts[:yajl_gen_beautify] = false - yajl_gen_opts[:yajl_gen_indent_string] = " " - - if opts[:pretty] - yajl_gen_opts[:yajl_gen_beautify] = true - yajl_gen_opts[:yajl_gen_indent_string] = opts[:indent] ? opts[:indent] : " " - end - - # call either the ext or ffi hook - str = do_yajl_encode(obj, yajl_gen_opts) - str.force_encoding('UTF-8') if defined? Encoding - end - - def self.encode(obj, *args) - new(*args).encode(obj) - end - - def initialize(opts = {}) - @opts = opts - end - - def self.raise_error_for_status(status) - case status - when 1 # yajl_gen_keys_must_be_strings - raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key" - when 2 # yajl_max_depth_exceeded - raise FFI_Yajl::EncodeError, "Max nesting depth exceeded" - when 3 # yajl_gen_in_error_state - raise FFI_Yajl::EncodeError, "YAJL internal error: a generator function (yajl_gen_XXX) was called while in an error state" - when 4 # yajl_gen_generation_complete - raise FFI_Yajl::EncodeError, "YAJL internal error: attempted to encode to an already-complete document" - when 5 # yajl_gen_invalid_number - raise FFI_Yajl::EncodeError, "Invalid number: cannot encode Infinity, -Infinity, or NaN" - when 6 # yajl_gen_no_buf - raise FFI_Yajl::EncodeError, "YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available" - else - raise FFI_Yajl::EncodeError, "Unknown YAJL Error, please report this as a bug" - end - end - - if ENV['FORCE_FFI_YAJL'] == "ffi" || defined?(Yajl) - # on Linux yajl-ruby and non-FFI ffi_yajl conflict - require 'ffi_yajl/ffi' - include FFI_Yajl::FFI::Encoder - else - begin - require 'ffi_yajl/ext' - include FFI_Yajl::Ext::Encoder - rescue LoadError - require 'ffi_yajl/ffi' - include FFI_Yajl::FFI::Encoder - end - end - end - - class Parser - attr_accessor :opts - - def self.parse(obj, *args) - new(*args).parse(obj) - end - - def initialize(opts = {}) - @opts = opts - end - - def parse(str) - # initialization that we can do in pure ruby - yajl_opts = {} - - # call either the ext or ffi hook - do_yajl_parse(str, yajl_opts) - end - - if ENV['FORCE_FFI_YAJL'] == "ffi" || defined?(Yajl) - # on Linux yajl-ruby and non-FFI ffi_yajl conflict - require 'ffi_yajl/ffi' - include FFI_Yajl::FFI::Parser - else - begin - require 'ffi_yajl/ext' - include FFI_Yajl::Ext::Parser - rescue LoadError - require 'ffi_yajl/ffi' - include FFI_Yajl::FFI::Parser - end - end - end end module FFI_Yajl diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb new file mode 100644 index 0000000..d720187 --- /dev/null +++ b/lib/ffi_yajl/encoder.rb @@ -0,0 +1,65 @@ + +module FFI_Yajl + class Encoder + attr_accessor :opts + + def encode(obj) + # initialization that we can do in pure ruby + yajl_gen_opts = {} + + yajl_gen_opts[:yajl_gen_validate_utf8] = true + yajl_gen_opts[:yajl_gen_beautify] = false + yajl_gen_opts[:yajl_gen_indent_string] = " " + + if opts[:pretty] + yajl_gen_opts[:yajl_gen_beautify] = true + yajl_gen_opts[:yajl_gen_indent_string] = opts[:indent] ? opts[:indent] : " " + end + + # call either the ext or ffi hook + str = do_yajl_encode(obj, yajl_gen_opts) + str.force_encoding('UTF-8') if defined? Encoding + end + + def self.encode(obj, *args) + new(*args).encode(obj) + end + + def initialize(opts = {}) + @opts = opts + end + + def self.raise_error_for_status(status) + case status + when 1 # yajl_gen_keys_must_be_strings + raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key" + when 2 # yajl_max_depth_exceeded + raise FFI_Yajl::EncodeError, "Max nesting depth exceeded" + when 3 # yajl_gen_in_error_state + raise FFI_Yajl::EncodeError, "YAJL internal error: a generator function (yajl_gen_XXX) was called while in an error state" + when 4 # yajl_gen_generation_complete + raise FFI_Yajl::EncodeError, "YAJL internal error: attempted to encode to an already-complete document" + when 5 # yajl_gen_invalid_number + raise FFI_Yajl::EncodeError, "Invalid number: cannot encode Infinity, -Infinity, or NaN" + when 6 # yajl_gen_no_buf + raise FFI_Yajl::EncodeError, "YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available" + else + raise FFI_Yajl::EncodeError, "Unknown YAJL Error, please report this as a bug" + end + end + + if ENV['FORCE_FFI_YAJL'] == "ffi" || defined?(Yajl) + # on Linux yajl-ruby and non-FFI ffi_yajl conflict + require 'ffi_yajl/ffi' + include FFI_Yajl::FFI::Encoder + else + begin + require 'ffi_yajl/ext' + include FFI_Yajl::Ext::Encoder + rescue LoadError + require 'ffi_yajl/ffi' + include FFI_Yajl::FFI::Encoder + end + end + end +end diff --git a/lib/ffi_yajl/parser.rb b/lib/ffi_yajl/parser.rb new file mode 100644 index 0000000..25acd3a --- /dev/null +++ b/lib/ffi_yajl/parser.rb @@ -0,0 +1,36 @@ + +module FFI_Yajl + class Parser + attr_accessor :opts + + def self.parse(obj, *args) + new(*args).parse(obj) + end + + def initialize(opts = {}) + @opts = opts + end + + def parse(str) + # initialization that we can do in pure ruby + yajl_opts = {} + + # call either the ext or ffi hook + do_yajl_parse(str, yajl_opts) + end + + if ENV['FORCE_FFI_YAJL'] == "ffi" || defined?(Yajl) + # on Linux yajl-ruby and non-FFI ffi_yajl conflict + require 'ffi_yajl/ffi' + include FFI_Yajl::FFI::Parser + else + begin + require 'ffi_yajl/ext' + include FFI_Yajl::Ext::Parser + rescue LoadError + require 'ffi_yajl/ffi' + include FFI_Yajl::FFI::Parser + end + end + end +end |