Convert YAML binary response in UTF-8 to Ruby 1.8.7

I get a response from the API and get:

      response: 
        job: 
          unit_count: "1"
          slug: Answers
          lc_tgt: ja
          body_tgt: !binary |
            5Zue562U

          lc_src: en
          body_src: Answers
          job_id: "1948888"
      opstat: ok

This body_tgt value must be a pair of Japanese characters (回答), but they are converted for safe delivery. I am in 1.8.7, so I cannot force_encoding. Is there any way to unpack () them?

+3
source share
1 answer

This appears to be a YAML document, not JSON, using the binary YAML data language (which, in turn, uses base64 encoding).

Ruby, built into the YAML parsing library, should be able to parse the data for you:

> x = YAML.load('      response: 
        job: 
          unit_count: "1"
          slug: Answers
          lc_tgt: ja
          body_tgt: !binary |
            5Zue562U

          lc_src: en
          body_src: Answers
          job_id: "1948888"
      opstat: ok')
 => {"opstat"=>"ok", "response"=>{"job"=>{"slug"=>"Answers", 
"unit_count"=>"1", "lc_tgt"=>"ja", "lc_src"=>"en", "body_tgt"=>"回答",
"job_id"=>"1948888", "body_src"=>"Answers"}}}

YAML UTF-8, , ya2yaml " to_yaml", UTF-8. ya2yaml gem, :

> require 'ya2yaml'
> x.ya2yaml(:syck_compatible => true)
+4

All Articles