Module: ActivityStreams::BinarySpec
- Includes:
- FileSpec
- Defined in:
- lib/streams/activitystreams.rb
Instance Method Summary collapse
-
#data(src, options = {:compress=>:deflate, :level=>9, :hash=>:md5}) ⇒ Object
Specify the data for the Binary object.
Methods included from ObjectSpec
#attachment, #downstream_duplicate, #ext_vocab, included, #upstream_duplicate
Methods included from Spec
Methods included from Spec::Defs
#def_absolute_iri, #def_boolean, #def_bound_float, #def_date_time, #def_iri, #def_non_negative_int, #def_numeric, #def_object, #def_object_array, #def_string, #def_string_array
Instance Method Details
#data(src, options = {:compress=>:deflate, :level=>9, :hash=>:md5}) ⇒ Object
Specify the data for the Binary object. The src must either be an IO object or a string containing a file path and name or an ArgumentError will be raised. Deflate compression by default, level 9, pass in :gzip to use Gzip compression or nil to disable compression entirely. The length and compression fields will automatically be set. This method will NOT close the src IO when it’s done, you’ll need to handle that yourself. Currently this doesn’t do any error handling on the IO read. Also, it currently reads the entire IO stream first, buffers it into memory, then compresses before base64 encoding…
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 |
# File 'lib/streams/activitystreams.rb', line 806 def data(src, ={:compress=>:deflate, :level=>9, :hash=>:md5}) compress = .fetch :compress, :deflate level = .fetch :level, 9 hash = .fetch :hash, :md5 if src.is_a? String File.open(src, 'r') {|f| data f, } else raise ArgumentError unless src.is_a? IO # Optionally generate a hash over the data as it is read if hash hasher = init_hasher(hash) d = src.read {|block| hasher.update block } self[hash] = hasher.hexdigest else d = src.read end # Set the uncompressed length of the data in octets self[:length] = d.length # Apply compression if necessary if compress d = do_compression d, compress, level self[:compress] = compress end # Set the data self[:data] = Base64.urlsafe_encode64(d) end end |