msgpacker 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1c7626f013b0cf4db1e2518a0dd781205cdc985
4
+ data.tar.gz: 543d29199dcc04dde4f9adf3e63a634954f06f05
5
+ SHA512:
6
+ metadata.gz: 02d4ec2971a86ed5e48bd2fbd5dfb1d79e06a545b0cc40ede15cc5d1f650920f2a7e42513f496defac8be6d86369f901631ee4126a684574c92bd9638784a8b2
7
+ data.tar.gz: 5fc907f9acbc115268f2ef1951c9d1f8945dacad60fe1ff6515be7cd52fa2ffb2bf488a40b8b2f1ce6afa741679e253845d9f0e9563691615fa2f586c378483d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in msgpacker.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 김대현
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # MsgPacker: JSON ⬌ MessagePack
2
+
3
+ a rubygem which converts a [JSON](http://www.json.org) stream to [MessagePack](http://msgpack.org)-ed one and vice versa.
4
+ It contains an excutable script that reads STDIN and writes the converted stream to STDOUT.
5
+
6
+ ## Installation
7
+
8
+ Install this gem
9
+
10
+ gem install msgpacker
11
+
12
+ Then you now have a executable script named `msgpacker`.
13
+
14
+ ## Usage
15
+
16
+ Prepare a sample JSON file
17
+
18
+ $ echo '{"compact": true, "schema": 0}' > DATA.json
19
+
20
+ And then execute the following to make it as MessagePack
21
+
22
+ $ cat DATA.json | msgpacker > DATA.mpac
23
+
24
+ Finally, you might also want to decode the MsgPack-ed file
25
+
26
+ $ cat DATA.mpac | msgpacker -d -n > DATA_decoded.json
27
+
28
+ If you give the `-n` option, it prints decoded json items with newline(\n) character appended.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.name = "spec"
6
+ t.libs << "lib"
7
+ t.pattern = 'spec/*_spec.rb'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :spec
data/bin/msgpacker ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/msgpacker'
3
+ MsgPacker::App.new.run
data/lib/msgpacker.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "msgpacker/version"
2
+ require "msgpacker/encode"
3
+ require "msgpacker/decode"
4
+ require "msgpacker/app"
@@ -0,0 +1,28 @@
1
+ require 'optparse'
2
+
3
+ module MsgPacker
4
+ class App
5
+ def run
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: msgpacker [options]"
9
+ opts.on("-d", "--decode", "decode msgpack to json") do |d|
10
+ options[:decode] = d
11
+ end
12
+ opts.on("-n", "--newline", "appends a newline character for each JSON item") do |n|
13
+ options[:newline] = n
14
+ end
15
+ opts.on("-p", "--pretty", "prettify JSON output") do |p|
16
+ options[:pretty] = p
17
+ end
18
+ end.parse!
19
+ coder = unless options[:decode]
20
+ MsgPacker::Encoder
21
+ else
22
+ MsgPacker::Decoder
23
+ end.new(options)
24
+ STDOUT.sync = true
25
+ coder.code(STDIN, STDOUT)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ require "yajl"
2
+ require "msgpack"
3
+
4
+ module MsgPacker
5
+ class Decoder
6
+ def initialize opts = {}
7
+ @options = opts.select {|k, v| [:pretty, :indent, :newline].include? k}
8
+ end
9
+
10
+ def code is = STDIN, os = STDOUT
11
+ MessagePack::Unpacker.new(is).each do |obj|
12
+ Yajl::Encoder.encode(obj, os, @options)
13
+ os.write "\n" if @options[:newline]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require "yajl"
2
+ require "msgpack"
3
+
4
+ module MsgPacker
5
+ class Encoder
6
+ def initialize opts = {}
7
+ @options = opts.dup
8
+ end
9
+
10
+ def object_parsed obj
11
+ @os.write obj.to_msgpack
12
+ end
13
+
14
+ def code is = STDIN, os = STDOUT
15
+ @os = os
16
+ parser = Yajl::Parser.new
17
+ parser.on_parse_complete = method(:object_parsed)
18
+ is.each_char { |ch| parser << ch }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module Msgpacker
2
+ VERSION = "0.0.4" # add 'prettify' feature for JSON output
3
+ # VERSION = "0.0.3"
4
+ end
data/msgpacker.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'msgpacker/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "msgpacker"
7
+ spec.version = Msgpacker::VERSION
8
+ spec.authors = ["Daehyun Kim"]
9
+ spec.email = ["https://twitter.com/hatemogi"]
10
+ spec.description = %q{This gem provides a executable ruby script that converts a JSON stream to MessagePack-ed one and the other way around.}
11
+ spec.summary = %q{streaming JSON <-> MessagePack converter}
12
+ spec.homepage = "http://github.com/hatemogi/msgpacker"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+ spec.bindir = 'bin'
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_runtime_dependency "yajl-ruby"
24
+ spec.add_runtime_dependency "msgpack"
25
+
26
+ spec.executables << 'msgpacker'
27
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'helper'
2
+ require 'stringio'
3
+
4
+ describe MsgPacker::Decoder do
5
+ before do
6
+ @e = MsgPacker::Decoder.new
7
+ @msgpacked = "\x82\xA7compact\xC3\xA6schema\x00"
8
+ @out = StringIO.new("", "w")
9
+ end
10
+
11
+ it "should decode an array" do
12
+ @e.code(StringIO.new(@msgpacked), @out)
13
+ @out.string.must_equal '{"compact":true,"schema":0}'
14
+ end
15
+
16
+ it "shouldn't insert a newline character without the -n option" do
17
+ @e.code(StringIO.new(@msgpacked * 2), @out)
18
+ @out.string.must_equal '{"compact":true,"schema":0}' * 2
19
+ end
20
+
21
+ it "should insert a newline character with the -n option" do
22
+ MsgPacker::Decoder.new(newline: true).code(StringIO.new(@msgpacked * 2), @out)
23
+ expected = '{"compact":true,"schema":0}'
24
+ @out.string.must_equal [expected, "\n", expected, "\n"].join
25
+ end
26
+
27
+ it "should prettify JSON output when the -p option is given" do
28
+ MsgPacker::Decoder.new(pretty: true).code(StringIO.new(@msgpacked), @out)
29
+ @out.string.must_equal %Q({\n "compact": true,\n "schema": 0\n})
30
+ end
31
+
32
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'helper'
2
+ require 'stringio'
3
+
4
+ describe MsgPacker::Encoder do
5
+ before do
6
+ @e = MsgPacker::Encoder.new
7
+ end
8
+
9
+ it "should encode an array" do
10
+ o = StringIO.new("", "w")
11
+ @e.code(StringIO.new('{"compact": true, "schema": 0}'), o)
12
+ o.string.must_equal "\x82\xA7compact\xC3\xA6schema\x00"
13
+ end
14
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require_relative '../lib/msgpacker'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: msgpacker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Daehyun Kim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yajl-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: msgpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This gem provides a executable ruby script that converts a JSON stream
70
+ to MessagePack-ed one and the other way around.
71
+ email:
72
+ - https://twitter.com/hatemogi
73
+ executables:
74
+ - msgpacker
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/msgpacker
84
+ - lib/msgpacker.rb
85
+ - lib/msgpacker/app.rb
86
+ - lib/msgpacker/decode.rb
87
+ - lib/msgpacker/encode.rb
88
+ - lib/msgpacker/version.rb
89
+ - msgpacker.gemspec
90
+ - spec/decode_spec.rb
91
+ - spec/encode_spec.rb
92
+ - spec/helper.rb
93
+ homepage: http://github.com/hatemogi/msgpacker
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: streaming JSON <-> MessagePack converter
117
+ test_files:
118
+ - spec/decode_spec.rb
119
+ - spec/encode_spec.rb
120
+ - spec/helper.rb