js2json 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2216932edab3ebf93e24d0d11d61cf48c1ba5000
4
+ data.tar.gz: c4704500a36ca8bb11bcc642207b619fcc41fc1b
5
+ SHA512:
6
+ metadata.gz: 99a4d261e2bc558d996a687caf6ff1ea784fc8e2dca7b150626dbff1dd3bada52d67f5614291343bbc3a601128425f8c7f0f896d2ed9c0d4ec46a9a5f78b6f0a
7
+ data.tar.gz: 9276a91d28ba7bcb0f51c3f74ae199518fac3c1aac622855906a0a4bd58198486d5901d9e797a9cd2c2f5d29b4fa1181c65a86a2d3515a60c0530a18a843f721
@@ -0,0 +1,18 @@
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
18
+ test.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in js2json.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Genki Sugawara
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.
@@ -0,0 +1,93 @@
1
+ # js2json
2
+
3
+ It is a library to convert from JavaScript to JSON.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'js2json'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install js2json
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'js2json'
23
+
24
+ json = Js2json.js2json(<<-EOS)
25
+ function plus(a, b) {
26
+ return a + b;
27
+ };
28
+
29
+ // Comment
30
+ ({
31
+ foo: "bar",
32
+ "zoo": plus(1, 2),
33
+ 'BAZ': 'A' + 'B',
34
+ plus: plus,
35
+ });
36
+ EOS
37
+
38
+ puts json # => {
39
+ # "foo": "bar",
40
+ # "zoo": 3,
41
+ # "BAZ": "AB",
42
+ # "plus": "function plus(a, b) {\n return a + b;\n}"
43
+ # }
44
+ ```
45
+
46
+ ### Use Ruby in JavaScript
47
+
48
+ ```ruby
49
+ require 'js2json'
50
+
51
+ json = Js2json.js2json(<<-EOS)
52
+ Ruby.Kernel.require('net/http');
53
+
54
+ var page = Ruby.Net.HTTP.start('example.com', 80, function(http) {
55
+ return http.get('/').body();
56
+ });
57
+
58
+ ({
59
+ site: 'example.com',
60
+ page: page,
61
+ });
62
+ EOS
63
+ ```
64
+
65
+ ### Auto bracket script
66
+
67
+ ```ruby
68
+ require 'js2json'
69
+
70
+ json = Js2json.js2json(<<-EOS, :bracket_script => true)
71
+ {
72
+ foo: "bar",
73
+ zoo: "baz",
74
+ }
75
+ EOS
76
+
77
+ puts json # => {
78
+ # "foo": "bar",
79
+ # "zoo": "baz"
80
+ # }
81
+ ```
82
+
83
+ ### Command line tool
84
+
85
+ $ echo '{foo:"bar", zoo:"baz"}' | js2json
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it ( http://github.com/winebarrel/js2json/fork )
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path("#{File.dirname __FILE__}/../lib")
3
+ require 'rubygems'
4
+ require 'js2json'
5
+ require 'open-uri'
6
+ require 'optparse'
7
+
8
+ Version = Js2json::VERSION
9
+
10
+ input = '-'
11
+ output = '-'
12
+ options = {}
13
+
14
+ def xopen(path, mode = 'r')
15
+ if path == '-'
16
+ yield(mode['w'] ? $stdout : $stdin)
17
+ else
18
+ open(path, mode) {|f| yield(f) }
19
+ end
20
+ end
21
+
22
+ def create_converter(expr)
23
+ case (expr || '').strip
24
+ when /\A(proc|Proc|lambda)\b/, /\A->/
25
+ Object.new.instance_eval(expr)
26
+ when /\Afunction\b/
27
+ V8::Context.new.eval("(#{expr})")
28
+ end
29
+ end
30
+
31
+ ARGV.options do |opt|
32
+ begin
33
+ opt.on('-i', '--input INPUT') {|v| input = v }
34
+ opt.on('-o', '--output OUTPUT') {|v| output = v }
35
+ opt.on('', '--bracket-script') { options[:bracket_script] = true }
36
+ opt.on('', '--conv CONVERTER') {|v| options[:conv] = create_converter(v) }
37
+ opt.on('', '--key-conv CONVERTER') {|v| options[:key_conv] = create_converter(v) }
38
+ opt.parse!
39
+ rescue => e
40
+ $stderr.puts e
41
+ exit 1
42
+ end
43
+ end
44
+
45
+ script = xopen(input) {|f| f.read }
46
+ json = Js2json.js2json(script, options)
47
+
48
+ xopen(input, 'w') do |f|
49
+ f.puts json
50
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'js2json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'js2json'
8
+ spec.version = Js2json::VERSION
9
+ spec.authors = ['Genki Sugawara']
10
+ spec.email = ['sgwr_dts@yahoo.co.jp']
11
+ spec.summary = %q{It is a library to convert from JavaScript to JSON.}
12
+ spec.description = %q{It is a library to convert from JavaScript to JSON.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'json'
22
+ spec.add_dependency 'therubyracer'
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '>= 2.11.0'
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'json'
2
+ require 'v8'
3
+
4
+ require 'js2json/ruby_scope'
5
+ require 'js2json/version'
6
+ require 'js2json/v8_converter'
7
+
8
+ module Js2json
9
+ def js2ruby(expr, options = {})
10
+ unless expr.kind_of?(String)
11
+ raise TypeError, "wrong argument type #{expr.class} (expected String)"
12
+ end
13
+
14
+ unless options.kind_of?(Hash)
15
+ raise TypeError, "wrong argument type #{options.class} (expected Hash)"
16
+ end
17
+
18
+ cxt = V8::Context.new
19
+ cxt['Ruby'] = Js2json::RubyScope.new
20
+ cxt['load'] = cxt.method(:load)
21
+ cxt['print'] = Kernel.method(:puts)
22
+
23
+ expr = "(#{expr})" if options[:bracket_script]
24
+ obj = cxt.eval(expr)
25
+
26
+ converter = Js2json::V8Converter.new(options)
27
+ converter.convert_to_ruby(obj)
28
+ end
29
+ module_function :js2ruby
30
+
31
+ def js2json(expr, options = {})
32
+ rb_obj = js2ruby(expr, options)
33
+ JSON.pretty_generate(rb_obj)
34
+ end
35
+ module_function :js2json
36
+ end
@@ -0,0 +1,64 @@
1
+ module Js2json
2
+ class RubyScope
3
+ def self.convert_from_ruby_to_v8(obj)
4
+ case obj
5
+ when Array
6
+ obj.map {|i| convert_from_ruby_to_v8(i) }
7
+ when Hash
8
+ Hash[*obj.map {|k, v|
9
+ [
10
+ convert_from_ruby_to_v8(k),
11
+ convert_from_ruby_to_v8(v),
12
+ ]
13
+ }.flatten]
14
+ when NilClass, Numeric, Proc, String, TrueClass, FalseClass
15
+ obj
16
+ else
17
+ Js2json::RubyScope::ObjectWrapper.new(obj)
18
+ end
19
+ end
20
+
21
+
22
+ class ObjectWrapper
23
+ def initialize(obj)
24
+ @obj = obj
25
+ end
26
+
27
+ def [](name)
28
+ name = name.to_s
29
+
30
+ if name =~ /\A[A-Z]/
31
+ Js2json::RubyScope.convert_from_ruby_to_v8(@obj.const_get(name))
32
+ else
33
+ proc do |*args|
34
+ args.shift
35
+ retval = nil
36
+
37
+ if args.last.kind_of?(V8::Function)
38
+ func = args.pop
39
+
40
+ block = proc do |*block_args|
41
+ block_args = block_args.map {|i|
42
+ Js2json::RubyScope.convert_from_ruby_to_v8(i)
43
+ }
44
+
45
+ func.call(*block_args)
46
+ end
47
+
48
+ retval = @obj.send(name, *args, &block)
49
+ else
50
+ retval = @obj.send(name, *args)
51
+ end
52
+
53
+ Js2json::RubyScope.convert_from_ruby_to_v8(retval)
54
+ end
55
+ end
56
+ end
57
+ end # ObjectWrapper
58
+
59
+ def [](name)
60
+ obj = Module.const_get(name)
61
+ Js2json::RubyScope.convert_from_ruby_to_v8(obj)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ module Js2json
2
+ class V8Converter
3
+ def initialize(options = {})
4
+ @options = options
5
+ end
6
+
7
+ def convert_to_ruby(obj)
8
+ case obj
9
+ when V8::Array
10
+ obj.map {|i| convert_to_ruby(i) }
11
+ when V8::Function
12
+ convert_to_ruby(obj.to_s)
13
+ when V8::Object
14
+ Hash[*obj.map {|k, v|
15
+ key_conv = @options[:key_conv]
16
+ k = convert_to_ruby(k)
17
+ k = key_conv.call(k) if key_conv
18
+ [k, convert_to_ruby(v)]
19
+ }.flatten]
20
+ else
21
+ conv = @options[:conv]
22
+ conv ? conv.call(obj) : obj
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Js2json
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,99 @@
1
+ describe Js2json do
2
+ it 'should convert from JavaScript to JSON' do
3
+ json = Js2json.js2json(<<-EOS)
4
+ function plus(a, b) {
5
+ return a + b;
6
+ }
7
+
8
+ // Comment
9
+ ({
10
+ foo: "bar",
11
+ "zoo": plus(1, 2),
12
+ 'BAZ': 'A' + 'B',
13
+ plus: plus,
14
+ })
15
+ EOS
16
+
17
+ expect(json).to eq (<<-'EOS').chomp
18
+ {
19
+ "foo": "bar",
20
+ "zoo": 3,
21
+ "BAZ": "AB",
22
+ "plus": "function plus(a, b) {\n return a + b;\n}"
23
+ }
24
+ EOS
25
+ end
26
+
27
+ it 'should convert from JavaScript to JSON (with converter)' do
28
+ conv = proc {|v| v.to_s }
29
+ json = Js2json.js2json(<<-EOS, :conv => conv)
30
+ ({
31
+ foo: 100,
32
+ bar: "200",
33
+ })
34
+ EOS
35
+
36
+ expect(json).to eq (<<-'EOS').chomp
37
+ {
38
+ "foo": "100",
39
+ "bar": "200"
40
+ }
41
+ EOS
42
+ end
43
+
44
+ it 'should convert from JavaScript to JSON (with key converter)' do
45
+ key_conv = proc {|v| v.to_s + '_' }
46
+ json = Js2json.js2json(<<-EOS, :key_conv => key_conv)
47
+ ({
48
+ foo: 100,
49
+ bar: "200",
50
+ })
51
+ EOS
52
+
53
+ expect(json).to eq (<<-'EOS').chomp
54
+ {
55
+ "foo_": 100,
56
+ "bar_": "200"
57
+ }
58
+ EOS
59
+ end
60
+
61
+ it 'should convert from JavaScript to JSON (bachet script)' do
62
+ expect {
63
+ Js2json.js2json(<<-EOS)
64
+ {
65
+ foo: 100,
66
+ bar: "200",
67
+ }
68
+ EOS
69
+ }.to raise_error
70
+
71
+ json = Js2json.js2json(<<-EOS, :bracket_script => true)
72
+ {
73
+ foo: 100,
74
+ bar: "200",
75
+ }
76
+ EOS
77
+
78
+ expect(json).to eq (<<-'EOS').chomp
79
+ {
80
+ "foo": 100,
81
+ "bar": "200"
82
+ }
83
+ EOS
84
+ end
85
+
86
+ it 'should convert from JavaScript to JSON (use Ruby object)' do
87
+ json = Js2json.js2json(<<-EOS)
88
+ Ruby.Kernel.require('net/http');
89
+
90
+ var page = Ruby.Net.HTTP.start('example.com', 80, function(http) {
91
+ return http.get('/').body();
92
+ });
93
+
94
+ [page]
95
+ EOS
96
+
97
+ expect(json).to be =~ %r|<title>Example Domain</title>|
98
+ end
99
+ end
@@ -0,0 +1 @@
1
+ require 'js2json'
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js2json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: therubyracer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.11.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.11.0
83
+ description: It is a library to convert from JavaScript to JSON.
84
+ email:
85
+ - sgwr_dts@yahoo.co.jp
86
+ executables:
87
+ - js2json
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/js2json
98
+ - js2json.gemspec
99
+ - lib/js2json.rb
100
+ - lib/js2json/ruby_scope.rb
101
+ - lib/js2json/v8_converter.rb
102
+ - lib/js2json/version.rb
103
+ - spec/js2json_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.0.14
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: It is a library to convert from JavaScript to JSON.
129
+ test_files:
130
+ - spec/js2json_spec.rb
131
+ - spec/spec_helper.rb
132
+ has_rdoc: