engineyard-serverside 2.8.0.pre → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/engineyard-serverside-execute-service-hook +1 -1
- data/features/hook/running-a-deploy-hook.feature +277 -0
- data/features/hook/step_definitions/running-a-deploy-hook-steps.rb +200 -0
- data/features/step_definitions/command_steps.rb +8 -0
- data/features/step_definitions/version_steps.rb +5 -0
- data/features/support/env.rb +63 -0
- data/features/support/error_codes.rb +17 -0
- data/features/support/fs_helpers.rb +82 -0
- data/features/support/output_helpers.rb +7 -0
- data/features/support/runner.rb +41 -0
- data/features/version.feature +7 -0
- data/lib/engineyard-serverside.rb +2 -5
- data/lib/engineyard-serverside/callbacks/collection/base.rb +12 -5
- data/lib/engineyard-serverside/callbacks/distributor.rb +2 -7
- data/lib/engineyard-serverside/callbacks/executor/executable.rb +2 -0
- data/lib/engineyard-serverside/cli/app.rb +6 -0
- data/lib/engineyard-serverside/cli/workflows/calling_deploy_hooks.rb +1 -2
- data/lib/engineyard-serverside/configuration.rb +3 -3
- data/lib/engineyard-serverside/deploy.rb +4 -8
- data/lib/engineyard-serverside/shell.rb +38 -10
- data/lib/engineyard-serverside/version.rb +1 -1
- data/spec/engineyard-serverside/callbacks/collection/combined_spec.rb +232 -0
- data/spec/engineyard-serverside/callbacks/collection/deploy_hooks_spec.rb +170 -0
- data/spec/engineyard-serverside/callbacks/collection/service_hooks/collection_spec.rb +171 -0
- data/spec/engineyard-serverside/callbacks/collection/service_hooks/combined_spec.rb +174 -0
- data/spec/engineyard-serverside/callbacks/collection/service_hooks_spec.rb +31 -0
- data/spec/engineyard-serverside/callbacks/collection_spec.rb +49 -0
- data/spec/engineyard-serverside/callbacks/distributor/remote_spec.rb +135 -0
- data/spec/engineyard-serverside/callbacks/distributor/viability_filter_spec.rb +208 -0
- data/spec/engineyard-serverside/callbacks/distributor_spec.rb +43 -0
- data/spec/engineyard-serverside/callbacks/executor/executable_spec.rb +386 -0
- data/spec/engineyard-serverside/callbacks/executor/ruby/context_spec.rb +538 -0
- data/spec/engineyard-serverside/callbacks/executor/ruby/executor_spec.rb +313 -0
- data/spec/engineyard-serverside/callbacks/executor/ruby_spec.rb +35 -0
- data/spec/engineyard-serverside/callbacks/executor_spec.rb +45 -0
- data/spec/engineyard-serverside/callbacks_spec.rb +31 -0
- data/spec/engineyard-serverside/cli/workflows/base_spec.rb +237 -0
- data/spec/engineyard-serverside/cli/workflows/calling_deploy_hooks_spec.rb +65 -0
- data/spec/engineyard-serverside/cli/workflows/deploying_applications_spec.rb +61 -0
- data/spec/engineyard-serverside/cli/workflows/disabling_maintenance_spec.rb +61 -0
- data/spec/engineyard-serverside/cli/workflows/enabling_maintenance_spec.rb +61 -0
- data/spec/engineyard-serverside/cli/workflows/helpers_spec.rb +64 -0
- data/spec/engineyard-serverside/cli/workflows/rolling_back_applications_spec.rb +61 -0
- data/spec/engineyard-serverside/cli/workflows/showing_maintenance_status_spec.rb +60 -0
- data/spec/engineyard-serverside/cli/workflows_spec.rb +87 -0
- data/spec/engineyard-serverside/dependency_manager/base_spec.rb +139 -0
- data/spec/engineyard-serverside/paths_spec.rb +820 -0
- data/spec/engineyard-serverside/propagator_spec.rb +56 -0
- data/spec/engineyard-serverside/server_spec.rb +390 -0
- data/spec/engineyard-serverside/shell/helpers_spec.rb +98 -0
- data/spec/engineyard-serverside/shell/yieldio_spec.rb +33 -0
- data/spec/engineyard-serverside/shell_spec.rb +543 -0
- data/spec/engineyard-serverside/slug/distributor_spec.rb +199 -0
- data/spec/engineyard-serverside/slug/enabler_spec.rb +191 -0
- data/spec/engineyard-serverside/slug/failure_handler_spec.rb +114 -0
- data/spec/engineyard-serverside/slug/finalizer_spec.rb +191 -0
- data/spec/engineyard-serverside/slug/generator_spec.rb +82 -0
- data/spec/engineyard-serverside/slug/migrator_spec.rb +137 -0
- data/spec/engineyard-serverside/slug/restarter_spec.rb +226 -0
- data/spec/engineyard-serverside/slug/source/updater_spec.rb +710 -0
- data/spec/engineyard-serverside/slug/source_spec.rb +40 -0
- data/spec/engineyard-serverside/source_spec.rb +171 -0
- data/spec/railway_spec.rb +130 -0
- data/spec/result/failure_spec.rb +113 -0
- data/spec/result/success_spec.rb +109 -0
- data/spec/result_spec.rb +23 -0
- data/spec/spec_helper.rb +3 -349
- metadata +327 -602
- data/lib/vendor/json_pure/COPYING +0 -57
- data/lib/vendor/json_pure/GPL +0 -340
- data/lib/vendor/json_pure/README.rdoc +0 -358
- data/lib/vendor/json_pure/VERSION +0 -1
- data/lib/vendor/json_pure/lib/json.rb +0 -62
- data/lib/vendor/json_pure/lib/json/add/bigdecimal.rb +0 -28
- data/lib/vendor/json_pure/lib/json/add/complex.rb +0 -22
- data/lib/vendor/json_pure/lib/json/add/core.rb +0 -11
- data/lib/vendor/json_pure/lib/json/add/date.rb +0 -34
- data/lib/vendor/json_pure/lib/json/add/date_time.rb +0 -48
- data/lib/vendor/json_pure/lib/json/add/exception.rb +0 -31
- data/lib/vendor/json_pure/lib/json/add/ostruct.rb +0 -31
- data/lib/vendor/json_pure/lib/json/add/range.rb +0 -29
- data/lib/vendor/json_pure/lib/json/add/rational.rb +0 -22
- data/lib/vendor/json_pure/lib/json/add/regexp.rb +0 -30
- data/lib/vendor/json_pure/lib/json/add/struct.rb +0 -30
- data/lib/vendor/json_pure/lib/json/add/symbol.rb +0 -25
- data/lib/vendor/json_pure/lib/json/add/time.rb +0 -38
- data/lib/vendor/json_pure/lib/json/common.rb +0 -484
- data/lib/vendor/json_pure/lib/json/ext.rb +0 -21
- data/lib/vendor/json_pure/lib/json/generic_object.rb +0 -70
- data/lib/vendor/json_pure/lib/json/pure.rb +0 -21
- data/lib/vendor/json_pure/lib/json/pure/generator.rb +0 -522
- data/lib/vendor/json_pure/lib/json/pure/parser.rb +0 -359
- data/lib/vendor/json_pure/lib/json/version.rb +0 -8
- data/lib/vendor/multi_json/CHANGELOG.md +0 -121
- data/lib/vendor/multi_json/CONTRIBUTING.md +0 -46
- data/lib/vendor/multi_json/Gemfile +0 -31
- data/lib/vendor/multi_json/LICENSE.md +0 -20
- data/lib/vendor/multi_json/README.md +0 -105
- data/lib/vendor/multi_json/Rakefile +0 -12
- data/lib/vendor/multi_json/lib/multi_json.rb +0 -137
- data/lib/vendor/multi_json/lib/multi_json/adapters/gson.rb +0 -20
- data/lib/vendor/multi_json/lib/multi_json/adapters/json_common.rb +0 -35
- data/lib/vendor/multi_json/lib/multi_json/adapters/json_gem.rb +0 -12
- data/lib/vendor/multi_json/lib/multi_json/adapters/json_pure.rb +0 -12
- data/lib/vendor/multi_json/lib/multi_json/adapters/nsjsonserialization.rb +0 -35
- data/lib/vendor/multi_json/lib/multi_json/adapters/oj.rb +0 -29
- data/lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb +0 -58
- data/lib/vendor/multi_json/lib/multi_json/adapters/yajl.rb +0 -20
- data/lib/vendor/multi_json/lib/multi_json/vendor/okjson.rb +0 -602
- data/lib/vendor/multi_json/lib/multi_json/version.rb +0 -3
- data/lib/vendor/multi_json/spec/adapter_shared_example.rb +0 -162
- data/lib/vendor/multi_json/spec/helper.rb +0 -45
- data/lib/vendor/multi_json/spec/json_common_shared_example.rb +0 -36
- data/lib/vendor/multi_json/spec/multi_json_spec.rb +0 -151
- data/spec/archive_deploy_spec.rb +0 -53
- data/spec/basic_deploy_spec.rb +0 -26
- data/spec/bundler_deploy_spec.rb +0 -160
- data/spec/configuration_spec.rb +0 -206
- data/spec/custom_deploy_spec.rb +0 -128
- data/spec/deploy_hook_spec.rb +0 -378
- data/spec/deprecation_spec.rb +0 -23
- data/spec/ey_yml_customized_deploy_spec.rb +0 -99
- data/spec/fixtures/gitrepo.tar.gz +0 -0
- data/spec/fixtures/invalid_hook.rb +0 -1
- data/spec/fixtures/lockfiles/0.9-no-bundler +0 -111
- data/spec/fixtures/lockfiles/0.9-with-bundler +0 -117
- data/spec/fixtures/lockfiles/1.0-no-bundler +0 -54
- data/spec/fixtures/lockfiles/1.0.0.rc.1-with-bundler +0 -162
- data/spec/fixtures/lockfiles/1.0.18-do_mysql +0 -88
- data/spec/fixtures/lockfiles/1.0.18-do_postgres +0 -79
- data/spec/fixtures/lockfiles/1.0.18-mysql +0 -43
- data/spec/fixtures/lockfiles/1.0.18-mysql2 +0 -43
- data/spec/fixtures/lockfiles/1.0.18-pg +0 -43
- data/spec/fixtures/lockfiles/1.0.6-no-bundler +0 -51
- data/spec/fixtures/lockfiles/1.0.6-with-any-bundler +0 -52
- data/spec/fixtures/lockfiles/1.0.6-with-bundler +0 -52
- data/spec/fixtures/lockfiles/1.15.1-no-bundler +0 -51
- data/spec/fixtures/lockfiles/1.3.1-rails-3.2.13 +0 -112
- data/spec/fixtures/lockfiles/not-a-lockfile +0 -1
- data/spec/fixtures/repos/assets_detected/Gemfile +0 -5
- data/spec/fixtures/repos/assets_detected/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_detected/README +0 -1
- data/spec/fixtures/repos/assets_detected/Rakefile +0 -5
- data/spec/fixtures/repos/assets_detected/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_detected/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_detected/config/ey.yml +0 -3
- data/spec/fixtures/repos/assets_disabled/Gemfile +0 -5
- data/spec/fixtures/repos/assets_disabled/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_disabled/README +0 -1
- data/spec/fixtures/repos/assets_disabled/Rakefile +0 -6
- data/spec/fixtures/repos/assets_disabled/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_disabled/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_disabled/config/ey.yml +0 -3
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/Gemfile +0 -5
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/README +0 -1
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/Rakefile +0 -6
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_disabled_in_ey_yml/config/ey.yml +0 -5
- data/spec/fixtures/repos/assets_disabled_utf8/Gemfile +0 -5
- data/spec/fixtures/repos/assets_disabled_utf8/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_disabled_utf8/README +0 -3
- data/spec/fixtures/repos/assets_disabled_utf8/Rakefile +0 -5
- data/spec/fixtures/repos/assets_disabled_utf8/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_disabled_utf8/config/application.rb +0 -7
- data/spec/fixtures/repos/assets_disabled_utf8/config/ey.yml +0 -3
- data/spec/fixtures/repos/assets_enabled_all/Gemfile +0 -5
- data/spec/fixtures/repos/assets_enabled_all/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_enabled_all/README +0 -1
- data/spec/fixtures/repos/assets_enabled_all/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_enabled_all/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_enabled_all/config/ey.yml +0 -6
- data/spec/fixtures/repos/assets_enabled_all/script/assets +0 -5
- data/spec/fixtures/repos/assets_enabled_all/tmp/obstruction +0 -1
- data/spec/fixtures/repos/assets_enabled_in_ey_yml/Gemfile +0 -3
- data/spec/fixtures/repos/assets_enabled_in_ey_yml/Gemfile.lock +0 -10
- data/spec/fixtures/repos/assets_enabled_in_ey_yml/README +0 -1
- data/spec/fixtures/repos/assets_enabled_in_ey_yml/Rakefile +0 -8
- data/spec/fixtures/repos/assets_enabled_in_ey_yml/config/ey.yml +0 -4
- data/spec/fixtures/repos/assets_enabled_util_only/Gemfile +0 -5
- data/spec/fixtures/repos/assets_enabled_util_only/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_enabled_util_only/README +0 -1
- data/spec/fixtures/repos/assets_enabled_util_only/Rakefile +0 -6
- data/spec/fixtures/repos/assets_enabled_util_only/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_enabled_util_only/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_enabled_util_only/config/ey.yml +0 -6
- data/spec/fixtures/repos/assets_error/Gemfile +0 -5
- data/spec/fixtures/repos/assets_error/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_error/README +0 -1
- data/spec/fixtures/repos/assets_error/Rakefile +0 -4
- data/spec/fixtures/repos/assets_error/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_error/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_error/config/ey.yml +0 -4
- data/spec/fixtures/repos/assets_in_hook/Gemfile +0 -5
- data/spec/fixtures/repos/assets_in_hook/Gemfile.lock +0 -88
- data/spec/fixtures/repos/assets_in_hook/README +0 -2
- data/spec/fixtures/repos/assets_in_hook/Rakefile +0 -5
- data/spec/fixtures/repos/assets_in_hook/app/assets/empty +0 -0
- data/spec/fixtures/repos/assets_in_hook/config/application.rb +0 -5
- data/spec/fixtures/repos/assets_in_hook/config/ey.yml +0 -3
- data/spec/fixtures/repos/assets_in_hook/deploy/before_compile_assets.rb +0 -2
- data/spec/fixtures/repos/bundle_fails/Gemfile +0 -1
- data/spec/fixtures/repos/bundle_fails/README +0 -1
- data/spec/fixtures/repos/bundle_fails/deploy/after_bundle.rb +0 -1
- data/spec/fixtures/repos/bundler_disabled/Gemfile +0 -4
- data/spec/fixtures/repos/bundler_disabled/Gemfile.lock +0 -12
- data/spec/fixtures/repos/bundler_disabled/README +0 -1
- data/spec/fixtures/repos/bundler_disabled/config/ey.yml +0 -2
- data/spec/fixtures/repos/bundler_disabled/deploy/after_bundle.rb +0 -1
- data/spec/fixtures/repos/bundler_disabled/deploy/before_bundle.rb +0 -1
- data/spec/fixtures/repos/bundler_old/Gemfile +0 -5
- data/spec/fixtures/repos/bundler_old/Gemfile.lock +0 -15
- data/spec/fixtures/repos/bundler_old/README +0 -1
- data/spec/fixtures/repos/default/Gemfile +0 -4
- data/spec/fixtures/repos/default/Gemfile.lock +0 -12
- data/spec/fixtures/repos/default/README +0 -5
- data/spec/fixtures/repos/default/ey.yml +0 -3
- data/spec/fixtures/repos/executable_hooks/README +0 -1
- data/spec/fixtures/repos/executable_hooks/deploy/before_restart +0 -72
- data/spec/fixtures/repos/executable_hooks_not_executable/README +0 -3
- data/spec/fixtures/repos/executable_hooks_not_executable/deploy/before_restart +0 -3
- data/spec/fixtures/repos/ey_yml/Gemfile +0 -4
- data/spec/fixtures/repos/ey_yml/Gemfile.lock +0 -12
- data/spec/fixtures/repos/ey_yml/README +0 -1
- data/spec/fixtures/repos/ey_yml/config/ey.yml +0 -18
- data/spec/fixtures/repos/ey_yml/deploy/before_migrate.rb +0 -6
- data/spec/fixtures/repos/ey_yml_alt/Gemfile +0 -4
- data/spec/fixtures/repos/ey_yml_alt/Gemfile.lock +0 -12
- data/spec/fixtures/repos/ey_yml_alt/README +0 -1
- data/spec/fixtures/repos/ey_yml_alt/deploy/before_migrate.rb +0 -6
- data/spec/fixtures/repos/ey_yml_alt/ey.yml +0 -12
- data/spec/fixtures/repos/hook_fails/README +0 -1
- data/spec/fixtures/repos/hook_fails/deploy/before_deploy.rb +0 -1
- data/spec/fixtures/repos/hooks/README +0 -1
- data/spec/fixtures/repos/hooks/deploy/after_bundle.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/after_compile_assets.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/after_deploy.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/after_migrate.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/after_restart.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/after_symlink.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/before_bundle.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/before_compile_assets.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/before_deploy.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/before_migrate.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/before_restart.rb +0 -1
- data/spec/fixtures/repos/hooks/deploy/before_symlink.rb +0 -1
- data/spec/fixtures/repos/multi_dep_manager/README +0 -1
- data/spec/fixtures/repos/multi_dep_manager/composer.json +0 -5
- data/spec/fixtures/repos/multi_dep_manager/composer.lock +0 -462
- data/spec/fixtures/repos/multi_dep_manager/package.json +0 -7
- data/spec/fixtures/repos/multi_dep_manager/public/index.php +0 -4
- data/spec/fixtures/repos/no_ey_config/Gemfile +0 -3
- data/spec/fixtures/repos/no_ey_config/Gemfile.lock +0 -10
- data/spec/fixtures/repos/no_ey_config/README +0 -1
- data/spec/fixtures/repos/no_ey_config/ey.yml +0 -3
- data/spec/fixtures/repos/no_ey_config_no_warning/Gemfile +0 -3
- data/spec/fixtures/repos/no_ey_config_no_warning/Gemfile.lock +0 -10
- data/spec/fixtures/repos/no_ey_config_no_warning/README +0 -1
- data/spec/fixtures/repos/no_ey_config_no_warning/ey.yml +0 -5
- data/spec/fixtures/repos/no_gemfile_lock/Gemfile +0 -4
- data/spec/fixtures/repos/no_gemfile_lock/README +0 -1
- data/spec/fixtures/repos/no_gemfile_lock/ey.yml +0 -3
- data/spec/fixtures/repos/nodejs/README +0 -1
- data/spec/fixtures/repos/nodejs/package.json +0 -7
- data/spec/fixtures/repos/not_bundled/README +0 -1
- data/spec/fixtures/repos/npm_disabled/README +0 -1
- data/spec/fixtures/repos/npm_disabled/config/ey.yml +0 -2
- data/spec/fixtures/repos/npm_disabled/package.json +0 -7
- data/spec/fixtures/repos/php_composer_disabled/README +0 -1
- data/spec/fixtures/repos/php_composer_disabled/composer.json +0 -5
- data/spec/fixtures/repos/php_composer_disabled/composer.lock +0 -462
- data/spec/fixtures/repos/php_composer_disabled/config/ey.yml +0 -2
- data/spec/fixtures/repos/php_composer_disabled/public/index.php +0 -4
- data/spec/fixtures/repos/php_composer_lock/README +0 -1
- data/spec/fixtures/repos/php_composer_lock/composer.json +0 -5
- data/spec/fixtures/repos/php_composer_lock/composer.lock +0 -462
- data/spec/fixtures/repos/php_composer_lock/public/index.php +0 -4
- data/spec/fixtures/repos/php_no_composer_lock/README +0 -1
- data/spec/fixtures/repos/php_no_composer_lock/composer.json +0 -21
- data/spec/fixtures/repos/php_no_composer_lock/public/index.php +0 -4
- data/spec/fixtures/repos/public_system/Gemfile +0 -4
- data/spec/fixtures/repos/public_system/Gemfile.lock +0 -12
- data/spec/fixtures/repos/public_system/README +0 -5
- data/spec/fixtures/repos/public_system/ey.yml +0 -3
- data/spec/fixtures/repos/public_system/public/system/cant_touch_this.txt +0 -3
- data/spec/fixtures/repos/sqlite3/Gemfile +0 -4
- data/spec/fixtures/repos/sqlite3/Gemfile.lock +0 -89
- data/spec/fixtures/repos/sqlite3/README +0 -1
- data/spec/fixtures/retwisj.war +0 -0
- data/spec/fixtures/valid_hook.rb +0 -1
- data/spec/git_strategy_spec.rb +0 -34
- data/spec/lockfile_parser_spec.rb +0 -126
- data/spec/maintenance_spec.rb +0 -44
- data/spec/multi_dependency_manager_spec.rb +0 -25
- data/spec/nodejs_deploy_spec.rb +0 -30
- data/spec/php_deploy_spec.rb +0 -81
- data/spec/platform_configure_spec.rb +0 -61
- data/spec/rails31_deploy_spec.rb +0 -172
- data/spec/restart_spec.rb +0 -43
- data/spec/rollback_spec.rb +0 -87
- data/spec/server_spec.rb +0 -70
- data/spec/services_deploy_spec.rb +0 -165
- data/spec/shell_spec.rb +0 -57
- data/spec/source/archive_spec.rb +0 -33
- data/spec/source/git_spec.rb +0 -44
- data/spec/sqlite3_deploy_spec.rb +0 -38
- data/spec/support/integration.rb +0 -103
- data/spec/support/source_doubles.rb +0 -28
- data/spec/support/timecop.rb +0 -5
- data/spec/symlink_spec.rb +0 -15
@@ -1,484 +0,0 @@
|
|
1
|
-
require 'json/version'
|
2
|
-
require 'json/generic_object'
|
3
|
-
|
4
|
-
module JSON
|
5
|
-
class << self
|
6
|
-
# If _object_ is string-like, parse the string and return the parsed result
|
7
|
-
# as a Ruby data structure. Otherwise generate a JSON text from the Ruby
|
8
|
-
# data structure object and return it.
|
9
|
-
#
|
10
|
-
# The _opts_ argument is passed through to generate/parse respectively. See
|
11
|
-
# generate and parse for their documentation.
|
12
|
-
def [](object, opts = {})
|
13
|
-
if object.respond_to? :to_str
|
14
|
-
JSON.parse(object.to_str, opts)
|
15
|
-
else
|
16
|
-
JSON.generate(object, opts)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Returns the JSON parser class that is used by JSON. This is either
|
21
|
-
# JSON::Ext::Parser or JSON::Pure::Parser.
|
22
|
-
attr_reader :parser
|
23
|
-
|
24
|
-
# Set the JSON parser class _parser_ to be used by JSON.
|
25
|
-
def parser=(parser) # :nodoc:
|
26
|
-
@parser = parser
|
27
|
-
remove_const :Parser if JSON.const_defined_in?(self, :Parser)
|
28
|
-
const_set :Parser, parser
|
29
|
-
end
|
30
|
-
|
31
|
-
# Return the constant located at _path_. The format of _path_ has to be
|
32
|
-
# either ::A::B::C or A::B::C. In any case, A has to be located at the top
|
33
|
-
# level (absolute namespace path?). If there doesn't exist a constant at
|
34
|
-
# the given path, an ArgumentError is raised.
|
35
|
-
def deep_const_get(path) # :nodoc:
|
36
|
-
path.to_s.split(/::/).inject(Object) do |p, c|
|
37
|
-
case
|
38
|
-
when c.empty? then p
|
39
|
-
when JSON.const_defined_in?(p, c) then p.const_get(c)
|
40
|
-
else
|
41
|
-
begin
|
42
|
-
p.const_missing(c)
|
43
|
-
rescue NameError => e
|
44
|
-
raise ArgumentError, "can't get const #{path}: #{e}"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Set the module _generator_ to be used by JSON.
|
51
|
-
def generator=(generator) # :nodoc:
|
52
|
-
old, $VERBOSE = $VERBOSE, nil
|
53
|
-
@generator = generator
|
54
|
-
generator_methods = generator::GeneratorMethods
|
55
|
-
for const in generator_methods.constants
|
56
|
-
klass = deep_const_get(const)
|
57
|
-
modul = generator_methods.const_get(const)
|
58
|
-
klass.class_eval do
|
59
|
-
instance_methods(false).each do |m|
|
60
|
-
m.to_s == 'to_json' and remove_method m
|
61
|
-
end
|
62
|
-
include modul
|
63
|
-
end
|
64
|
-
end
|
65
|
-
self.state = generator::State
|
66
|
-
const_set :State, self.state
|
67
|
-
const_set :SAFE_STATE_PROTOTYPE, State.new
|
68
|
-
const_set :FAST_STATE_PROTOTYPE, State.new(
|
69
|
-
:indent => '',
|
70
|
-
:space => '',
|
71
|
-
:object_nl => "",
|
72
|
-
:array_nl => "",
|
73
|
-
:max_nesting => false
|
74
|
-
)
|
75
|
-
const_set :PRETTY_STATE_PROTOTYPE, State.new(
|
76
|
-
:indent => ' ',
|
77
|
-
:space => ' ',
|
78
|
-
:object_nl => "\n",
|
79
|
-
:array_nl => "\n"
|
80
|
-
)
|
81
|
-
ensure
|
82
|
-
$VERBOSE = old
|
83
|
-
end
|
84
|
-
|
85
|
-
# Returns the JSON generator module that is used by JSON. This is
|
86
|
-
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
87
|
-
attr_reader :generator
|
88
|
-
|
89
|
-
# Returns the JSON generator state class that is used by JSON. This is
|
90
|
-
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
91
|
-
attr_accessor :state
|
92
|
-
|
93
|
-
# This is create identifier, which is used to decide if the _json_create_
|
94
|
-
# hook of a class should be called. It defaults to 'json_class'.
|
95
|
-
attr_accessor :create_id
|
96
|
-
end
|
97
|
-
self.create_id = 'json_class'
|
98
|
-
|
99
|
-
NaN = 0.0/0
|
100
|
-
|
101
|
-
Infinity = 1.0/0
|
102
|
-
|
103
|
-
MinusInfinity = -Infinity
|
104
|
-
|
105
|
-
# The base exception for JSON errors.
|
106
|
-
class JSONError < StandardError
|
107
|
-
def self.wrap(exception)
|
108
|
-
obj = new("Wrapped(#{exception.class}): #{exception.message.inspect}")
|
109
|
-
obj.set_backtrace exception.backtrace
|
110
|
-
obj
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
# This exception is raised if a parser error occurs.
|
115
|
-
class ParserError < JSONError; end
|
116
|
-
|
117
|
-
# This exception is raised if the nesting of parsed data structures is too
|
118
|
-
# deep.
|
119
|
-
class NestingError < ParserError; end
|
120
|
-
|
121
|
-
# :stopdoc:
|
122
|
-
class CircularDatastructure < NestingError; end
|
123
|
-
# :startdoc:
|
124
|
-
|
125
|
-
# This exception is raised if a generator or unparser error occurs.
|
126
|
-
class GeneratorError < JSONError; end
|
127
|
-
# For backwards compatibility
|
128
|
-
UnparserError = GeneratorError
|
129
|
-
|
130
|
-
# This exception is raised if the required unicode support is missing on the
|
131
|
-
# system. Usually this means that the iconv library is not installed.
|
132
|
-
class MissingUnicodeSupport < JSONError; end
|
133
|
-
|
134
|
-
module_function
|
135
|
-
|
136
|
-
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
137
|
-
#
|
138
|
-
# _opts_ can have the following
|
139
|
-
# keys:
|
140
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
141
|
-
# structures. Disable depth checking with :max_nesting => false. It defaults
|
142
|
-
# to 100.
|
143
|
-
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
144
|
-
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
145
|
-
# to false.
|
146
|
-
# * *symbolize_names*: If set to true, returns symbols for the names
|
147
|
-
# (keys) in a JSON object. Otherwise strings are returned. Strings are
|
148
|
-
# the default.
|
149
|
-
# * *create_additions*: If set to false, the Parser doesn't create
|
150
|
-
# additions even if a matching class and create_id was found. This option
|
151
|
-
# defaults to true.
|
152
|
-
# * *object_class*: Defaults to Hash
|
153
|
-
# * *array_class*: Defaults to Array
|
154
|
-
def parse(source, opts = {})
|
155
|
-
Parser.new(source, opts).parse
|
156
|
-
end
|
157
|
-
|
158
|
-
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
159
|
-
# The bang version of the parse method defaults to the more dangerous values
|
160
|
-
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
161
|
-
#
|
162
|
-
# _opts_ can have the following keys:
|
163
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
164
|
-
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
|
165
|
-
# methods defaults to not doing max depth checking: This can be dangerous
|
166
|
-
# if someone wants to fill up your stack.
|
167
|
-
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
|
168
|
-
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
169
|
-
# to true.
|
170
|
-
# * *create_additions*: If set to false, the Parser doesn't create
|
171
|
-
# additions even if a matching class and create_id was found. This option
|
172
|
-
# defaults to true.
|
173
|
-
def parse!(source, opts = {})
|
174
|
-
opts = {
|
175
|
-
:max_nesting => false,
|
176
|
-
:allow_nan => true
|
177
|
-
}.update(opts)
|
178
|
-
Parser.new(source, opts).parse
|
179
|
-
end
|
180
|
-
|
181
|
-
# Generate a JSON document from the Ruby data structure _obj_ and return
|
182
|
-
# it. _state_ is * a JSON::State object,
|
183
|
-
# * or a Hash like object (responding to to_hash),
|
184
|
-
# * an object convertible into a hash by a to_h method,
|
185
|
-
# that is used as or to configure a State object.
|
186
|
-
#
|
187
|
-
# It defaults to a state object, that creates the shortest possible JSON text
|
188
|
-
# in one line, checks for circular data structures and doesn't allow NaN,
|
189
|
-
# Infinity, and -Infinity.
|
190
|
-
#
|
191
|
-
# A _state_ hash can have the following keys:
|
192
|
-
# * *indent*: a string used to indent levels (default: ''),
|
193
|
-
# * *space*: a string that is put after, a : or , delimiter (default: ''),
|
194
|
-
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
195
|
-
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
196
|
-
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
197
|
-
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
198
|
-
# generated, otherwise an exception is thrown if these values are
|
199
|
-
# encountered. This options defaults to false.
|
200
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
201
|
-
# structures from which JSON is to be generated. Disable depth checking
|
202
|
-
# with :max_nesting => false, it defaults to 100.
|
203
|
-
#
|
204
|
-
# See also the fast_generate for the fastest creation method with the least
|
205
|
-
# amount of sanity checks, and the pretty_generate method for some
|
206
|
-
# defaults for pretty output.
|
207
|
-
def generate(obj, opts = nil)
|
208
|
-
if State === opts
|
209
|
-
state, opts = opts, nil
|
210
|
-
else
|
211
|
-
state = SAFE_STATE_PROTOTYPE.dup
|
212
|
-
end
|
213
|
-
if opts
|
214
|
-
if opts.respond_to? :to_hash
|
215
|
-
opts = opts.to_hash
|
216
|
-
elsif opts.respond_to? :to_h
|
217
|
-
opts = opts.to_h
|
218
|
-
else
|
219
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
220
|
-
end
|
221
|
-
state = state.configure(opts)
|
222
|
-
end
|
223
|
-
state.generate(obj)
|
224
|
-
end
|
225
|
-
|
226
|
-
# :stopdoc:
|
227
|
-
# I want to deprecate these later, so I'll first be silent about them, and
|
228
|
-
# later delete them.
|
229
|
-
alias unparse generate
|
230
|
-
module_function :unparse
|
231
|
-
# :startdoc:
|
232
|
-
|
233
|
-
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
234
|
-
# This method disables the checks for circles in Ruby objects.
|
235
|
-
#
|
236
|
-
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
237
|
-
# _obj_ argument because this will cause JSON to go into an infinite loop.
|
238
|
-
def fast_generate(obj, opts = nil)
|
239
|
-
if State === opts
|
240
|
-
state, opts = opts, nil
|
241
|
-
else
|
242
|
-
state = FAST_STATE_PROTOTYPE.dup
|
243
|
-
end
|
244
|
-
if opts
|
245
|
-
if opts.respond_to? :to_hash
|
246
|
-
opts = opts.to_hash
|
247
|
-
elsif opts.respond_to? :to_h
|
248
|
-
opts = opts.to_h
|
249
|
-
else
|
250
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
251
|
-
end
|
252
|
-
state.configure(opts)
|
253
|
-
end
|
254
|
-
state.generate(obj)
|
255
|
-
end
|
256
|
-
|
257
|
-
# :stopdoc:
|
258
|
-
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
259
|
-
alias fast_unparse fast_generate
|
260
|
-
module_function :fast_unparse
|
261
|
-
# :startdoc:
|
262
|
-
|
263
|
-
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
264
|
-
# The returned document is a prettier form of the document returned by
|
265
|
-
# #unparse.
|
266
|
-
#
|
267
|
-
# The _opts_ argument can be used to configure the generator. See the
|
268
|
-
# generate method for a more detailed explanation.
|
269
|
-
def pretty_generate(obj, opts = nil)
|
270
|
-
if State === opts
|
271
|
-
state, opts = opts, nil
|
272
|
-
else
|
273
|
-
state = PRETTY_STATE_PROTOTYPE.dup
|
274
|
-
end
|
275
|
-
if opts
|
276
|
-
if opts.respond_to? :to_hash
|
277
|
-
opts = opts.to_hash
|
278
|
-
elsif opts.respond_to? :to_h
|
279
|
-
opts = opts.to_h
|
280
|
-
else
|
281
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
282
|
-
end
|
283
|
-
state.configure(opts)
|
284
|
-
end
|
285
|
-
state.generate(obj)
|
286
|
-
end
|
287
|
-
|
288
|
-
# :stopdoc:
|
289
|
-
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
290
|
-
alias pretty_unparse pretty_generate
|
291
|
-
module_function :pretty_unparse
|
292
|
-
# :startdoc:
|
293
|
-
|
294
|
-
class << self
|
295
|
-
# The global default options for the JSON.load method:
|
296
|
-
# :max_nesting: false
|
297
|
-
# :allow_nan: true
|
298
|
-
# :quirks_mode: true
|
299
|
-
attr_accessor :load_default_options
|
300
|
-
end
|
301
|
-
self.load_default_options = {
|
302
|
-
:max_nesting => false,
|
303
|
-
:allow_nan => true,
|
304
|
-
:quirks_mode => true,
|
305
|
-
:create_additions => true,
|
306
|
-
}
|
307
|
-
|
308
|
-
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
309
|
-
# either be a string-like object, an IO-like object, or an object responding
|
310
|
-
# to the read method. If _proc_ was given, it will be called with any nested
|
311
|
-
# Ruby object as an argument recursively in depth first order. To modify the
|
312
|
-
# default options pass in the optional _options_ argument as well.
|
313
|
-
#
|
314
|
-
# BEWARE: This method is meant to serialise data from trusted user input,
|
315
|
-
# like from your own database server or clients under your control, it could
|
316
|
-
# be dangerous to allow untrusted users to pass JSON sources into it. The
|
317
|
-
# default options for the parser can be changed via the load_default_options
|
318
|
-
# method.
|
319
|
-
#
|
320
|
-
# This method is part of the implementation of the load/dump interface of
|
321
|
-
# Marshal and YAML.
|
322
|
-
def load(source, proc = nil, options = {})
|
323
|
-
opts = load_default_options.merge options
|
324
|
-
if source.respond_to? :to_str
|
325
|
-
source = source.to_str
|
326
|
-
elsif source.respond_to? :to_io
|
327
|
-
source = source.to_io.read
|
328
|
-
elsif source.respond_to?(:read)
|
329
|
-
source = source.read
|
330
|
-
end
|
331
|
-
if opts[:quirks_mode] && (source.nil? || source.empty?)
|
332
|
-
source = 'null'
|
333
|
-
end
|
334
|
-
result = parse(source, opts)
|
335
|
-
recurse_proc(result, &proc) if proc
|
336
|
-
result
|
337
|
-
end
|
338
|
-
|
339
|
-
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
340
|
-
def recurse_proc(result, &proc)
|
341
|
-
case result
|
342
|
-
when Array
|
343
|
-
result.each { |x| recurse_proc x, &proc }
|
344
|
-
proc.call result
|
345
|
-
when Hash
|
346
|
-
result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
|
347
|
-
proc.call result
|
348
|
-
else
|
349
|
-
proc.call result
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
alias restore load
|
354
|
-
module_function :restore
|
355
|
-
|
356
|
-
class << self
|
357
|
-
# The global default options for the JSON.dump method:
|
358
|
-
# :max_nesting: false
|
359
|
-
# :allow_nan: true
|
360
|
-
# :quirks_mode: true
|
361
|
-
attr_accessor :dump_default_options
|
362
|
-
end
|
363
|
-
self.dump_default_options = {
|
364
|
-
:max_nesting => false,
|
365
|
-
:allow_nan => true,
|
366
|
-
:quirks_mode => true,
|
367
|
-
}
|
368
|
-
|
369
|
-
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
370
|
-
# the result.
|
371
|
-
#
|
372
|
-
# If anIO (an IO-like object or an object that responds to the write method)
|
373
|
-
# was given, the resulting JSON is written to it.
|
374
|
-
#
|
375
|
-
# If the number of nested arrays or objects exceeds _limit_, an ArgumentError
|
376
|
-
# exception is raised. This argument is similar (but not exactly the
|
377
|
-
# same!) to the _limit_ argument in Marshal.dump.
|
378
|
-
#
|
379
|
-
# The default options for the generator can be changed via the
|
380
|
-
# dump_default_options method.
|
381
|
-
#
|
382
|
-
# This method is part of the implementation of the load/dump interface of
|
383
|
-
# Marshal and YAML.
|
384
|
-
def dump(obj, anIO = nil, limit = nil)
|
385
|
-
if anIO and limit.nil?
|
386
|
-
anIO = anIO.to_io if anIO.respond_to?(:to_io)
|
387
|
-
unless anIO.respond_to?(:write)
|
388
|
-
limit = anIO
|
389
|
-
anIO = nil
|
390
|
-
end
|
391
|
-
end
|
392
|
-
opts = JSON.dump_default_options
|
393
|
-
limit and opts.update(:max_nesting => limit)
|
394
|
-
result = generate(obj, opts)
|
395
|
-
if anIO
|
396
|
-
anIO.write result
|
397
|
-
anIO
|
398
|
-
else
|
399
|
-
result
|
400
|
-
end
|
401
|
-
rescue JSON::NestingError
|
402
|
-
raise ArgumentError, "exceed depth limit"
|
403
|
-
end
|
404
|
-
|
405
|
-
# Swap consecutive bytes of _string_ in place.
|
406
|
-
def self.swap!(string) # :nodoc:
|
407
|
-
0.upto(string.size / 2) do |i|
|
408
|
-
break unless string[2 * i + 1]
|
409
|
-
string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
|
410
|
-
end
|
411
|
-
string
|
412
|
-
end
|
413
|
-
|
414
|
-
# Shortuct for iconv.
|
415
|
-
if ::String.method_defined?(:encode)
|
416
|
-
# Encodes string using Ruby's _String.encode_
|
417
|
-
def self.iconv(to, from, string)
|
418
|
-
string.encode(to, from)
|
419
|
-
end
|
420
|
-
else
|
421
|
-
require 'iconv'
|
422
|
-
# Encodes string using _iconv_ library
|
423
|
-
def self.iconv(to, from, string)
|
424
|
-
Iconv.conv(to, from, string)
|
425
|
-
end
|
426
|
-
end
|
427
|
-
|
428
|
-
if ::Object.method(:const_defined?).arity == 1
|
429
|
-
def self.const_defined_in?(modul, constant)
|
430
|
-
modul.const_defined?(constant)
|
431
|
-
end
|
432
|
-
else
|
433
|
-
def self.const_defined_in?(modul, constant)
|
434
|
-
modul.const_defined?(constant, false)
|
435
|
-
end
|
436
|
-
end
|
437
|
-
end
|
438
|
-
|
439
|
-
module ::Kernel
|
440
|
-
private
|
441
|
-
|
442
|
-
# Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
|
443
|
-
# one line.
|
444
|
-
def j(*objs)
|
445
|
-
objs.each do |obj|
|
446
|
-
puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
|
447
|
-
end
|
448
|
-
nil
|
449
|
-
end
|
450
|
-
|
451
|
-
# Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
|
452
|
-
# indentation and over many lines.
|
453
|
-
def jj(*objs)
|
454
|
-
objs.each do |obj|
|
455
|
-
puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
|
456
|
-
end
|
457
|
-
nil
|
458
|
-
end
|
459
|
-
|
460
|
-
# If _object_ is string-like, parse the string and return the parsed result as
|
461
|
-
# a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
|
462
|
-
# structure object and return it.
|
463
|
-
#
|
464
|
-
# The _opts_ argument is passed through to generate/parse respectively. See
|
465
|
-
# generate and parse for their documentation.
|
466
|
-
def JSON(object, *args)
|
467
|
-
if object.respond_to? :to_str
|
468
|
-
JSON.parse(object.to_str, args.first)
|
469
|
-
else
|
470
|
-
JSON.generate(object, args.first)
|
471
|
-
end
|
472
|
-
end
|
473
|
-
end
|
474
|
-
|
475
|
-
# Extends any Class to include _json_creatable?_ method.
|
476
|
-
class ::Class
|
477
|
-
# Returns true if this class can be used to create an instance
|
478
|
-
# from a serialised JSON string. The class has to implement a class
|
479
|
-
# method _json_create_ that expects a hash as first parameter. The hash
|
480
|
-
# should include the required data.
|
481
|
-
def json_creatable?
|
482
|
-
respond_to?(:json_create)
|
483
|
-
end
|
484
|
-
end
|