stripe 1.6.3 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/stripe-console +0 -0
- data/lib/stripe.rb +55 -26
- data/lib/stripe/version.rb +1 -1
- metadata +45 -21
- data/vendor/stripe-json/lib/json/common.rb +0 -426
- data/vendor/stripe-json/lib/json/pure.rb +0 -28
- data/vendor/stripe-json/lib/json/pure/generator.rb +0 -442
- data/vendor/stripe-json/lib/json/pure/parser.rb +0 -320
- data/vendor/stripe-json/lib/json/version.rb +0 -8
data/bin/stripe-console
CHANGED
File without changes
|
data/lib/stripe.rb
CHANGED
@@ -8,19 +8,7 @@ require 'openssl'
|
|
8
8
|
|
9
9
|
gem 'rest-client', '~> 1.4'
|
10
10
|
require 'rest_client'
|
11
|
-
|
12
|
-
begin
|
13
|
-
require 'json'
|
14
|
-
rescue LoadError
|
15
|
-
raise if defined?(JSON)
|
16
|
-
require File.join(File.dirname(__FILE__), '../vendor/stripe-json/lib/json/pure')
|
17
|
-
|
18
|
-
# moderately ugly hack to deal with the clobbering that
|
19
|
-
# ActiveSupport's JSON subjects us to
|
20
|
-
class JSON::Pure::Generator::State
|
21
|
-
attr_reader :encoder, :only, :except
|
22
|
-
end
|
23
|
-
end
|
11
|
+
require 'multi_json'
|
24
12
|
|
25
13
|
require File.join(File.dirname(__FILE__), 'stripe/version')
|
26
14
|
|
@@ -54,7 +42,8 @@ module Stripe
|
|
54
42
|
'invoice' => Invoice,
|
55
43
|
'plan' => Plan,
|
56
44
|
'coupon' => Coupon,
|
57
|
-
'event' => Event
|
45
|
+
'event' => Event,
|
46
|
+
'transfer' => Transfer
|
58
47
|
}
|
59
48
|
case resp
|
60
49
|
when Array
|
@@ -131,6 +120,24 @@ module Stripe
|
|
131
120
|
end
|
132
121
|
end
|
133
122
|
|
123
|
+
module JSON
|
124
|
+
if MultiJson.respond_to?(:dump)
|
125
|
+
def self.dump(*args)
|
126
|
+
MultiJson.dump(*args)
|
127
|
+
end
|
128
|
+
def self.load(*args)
|
129
|
+
MultiJson.load(*args)
|
130
|
+
end
|
131
|
+
else
|
132
|
+
def self.dump(*args)
|
133
|
+
MultiJson.encode(*args)
|
134
|
+
end
|
135
|
+
def self.load(*args)
|
136
|
+
MultiJson.decode(*args)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
134
141
|
module APIOperations
|
135
142
|
module Create
|
136
143
|
module ClassMethods
|
@@ -205,11 +212,11 @@ module Stripe
|
|
205
212
|
obj
|
206
213
|
end
|
207
214
|
|
208
|
-
def to_s(*args); JSON.
|
215
|
+
def to_s(*args); Stripe::JSON.dump(@values, :pretty => true); end
|
209
216
|
|
210
217
|
def inspect()
|
211
218
|
id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
|
212
|
-
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.
|
219
|
+
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + Stripe::JSON.dump(@values, :pretty => true)
|
213
220
|
end
|
214
221
|
|
215
222
|
def refresh_from(values, api_key, partial=false)
|
@@ -246,7 +253,7 @@ module Stripe
|
|
246
253
|
end
|
247
254
|
def keys; @values.keys; end
|
248
255
|
def values; @values.values; end
|
249
|
-
def to_json(*a); @values
|
256
|
+
def to_json(*a); Stripe::JSON.dump(@values); end
|
250
257
|
def as_json(*a); @values.as_json(*a); end
|
251
258
|
def to_hash; @values; end
|
252
259
|
def each(&blk); @values.each(&blk); end
|
@@ -369,8 +376,17 @@ module Stripe
|
|
369
376
|
subscription
|
370
377
|
end
|
371
378
|
|
379
|
+
def delete_discount
|
380
|
+
Stripe.request(:delete, discount_url, @api_key)
|
381
|
+
refresh_from({ :discount => nil }, api_key, true)
|
382
|
+
end
|
383
|
+
|
372
384
|
private
|
373
385
|
|
386
|
+
def discount_url
|
387
|
+
url + '/discount'
|
388
|
+
end
|
389
|
+
|
374
390
|
def subscription_url
|
375
391
|
url + '/subscription'
|
376
392
|
end
|
@@ -447,6 +463,21 @@ module Stripe
|
|
447
463
|
include Stripe::APIOperations::List
|
448
464
|
end
|
449
465
|
|
466
|
+
class Transfer < APIResource
|
467
|
+
include Stripe::APIOperations::List
|
468
|
+
|
469
|
+
def transactions(params={})
|
470
|
+
response, api_key = Stripe.request(:get, transactions_url, @api_key, params)
|
471
|
+
Util.convert_to_stripe_object(response, api_key)
|
472
|
+
end
|
473
|
+
|
474
|
+
private
|
475
|
+
|
476
|
+
def transactions_url
|
477
|
+
url + '/transactions'
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
450
481
|
class StripeError < StandardError
|
451
482
|
attr_reader :message
|
452
483
|
attr_reader :http_status
|
@@ -542,10 +573,8 @@ module Stripe
|
|
542
573
|
payload = params
|
543
574
|
end
|
544
575
|
|
545
|
-
# There's a bug in some version of activesupport where JSON.dump
|
546
|
-
# stops working
|
547
576
|
begin
|
548
|
-
headers = { :x_stripe_client_user_agent => JSON.dump(ua) }.merge(headers)
|
577
|
+
headers = { :x_stripe_client_user_agent => Stripe::JSON.dump(ua) }.merge(headers)
|
549
578
|
rescue => e
|
550
579
|
headers = {
|
551
580
|
:x_stripe_client_raw_user_agent => ua.inspect,
|
@@ -554,12 +583,12 @@ module Stripe
|
|
554
583
|
end
|
555
584
|
|
556
585
|
headers = {
|
557
|
-
:user_agent => "Stripe/v1 RubyBindings/#{Stripe::VERSION}"
|
586
|
+
:user_agent => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
|
587
|
+
:authorization => "Bearer #{api_key}"
|
558
588
|
}.merge(headers)
|
559
589
|
opts = {
|
560
590
|
:method => method,
|
561
591
|
:url => url,
|
562
|
-
:user => api_key,
|
563
592
|
:headers => headers,
|
564
593
|
:open_timeout => 30,
|
565
594
|
:payload => payload,
|
@@ -593,8 +622,8 @@ module Stripe
|
|
593
622
|
begin
|
594
623
|
# Would use :symbolize_names => true, but apparently there is
|
595
624
|
# some library out there that makes symbolize_names not work.
|
596
|
-
resp = JSON.
|
597
|
-
rescue
|
625
|
+
resp = Stripe::JSON.load(rbody)
|
626
|
+
rescue MultiJson::DecodeError
|
598
627
|
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
|
599
628
|
end
|
600
629
|
|
@@ -610,10 +639,10 @@ module Stripe
|
|
610
639
|
|
611
640
|
def self.handle_api_error(rcode, rbody)
|
612
641
|
begin
|
613
|
-
error_obj = JSON.
|
642
|
+
error_obj = Stripe::JSON.load(rbody)
|
614
643
|
error_obj = Util.symbolize_names(error_obj)
|
615
644
|
error = error_obj[:error] or raise StripeError.new # escape from parsing
|
616
|
-
rescue
|
645
|
+
rescue MultiJson::DecodeError, StripeError
|
617
646
|
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
|
618
647
|
end
|
619
648
|
|
data/lib/stripe/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stripe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 1.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ross Boucher
|
@@ -16,12 +16,13 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2012-
|
19
|
+
date: 2012-05-17 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
22
23
|
name: rest-client
|
23
24
|
prerelease: false
|
24
|
-
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
26
|
none: false
|
26
27
|
requirements:
|
27
28
|
- - ~>
|
@@ -31,12 +32,27 @@ dependencies:
|
|
31
32
|
- 1
|
32
33
|
- 4
|
33
34
|
version: "1.4"
|
35
|
+
requirement: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
34
37
|
type: :runtime
|
35
|
-
|
38
|
+
name: multi_json
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 13
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 1
|
49
|
+
version: "1.1"
|
50
|
+
requirement: *id002
|
36
51
|
- !ruby/object:Gem::Dependency
|
52
|
+
type: :development
|
37
53
|
name: mocha
|
38
54
|
prerelease: false
|
39
|
-
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
40
56
|
none: false
|
41
57
|
requirements:
|
42
58
|
- - ">="
|
@@ -45,12 +61,12 @@ dependencies:
|
|
45
61
|
segments:
|
46
62
|
- 0
|
47
63
|
version: "0"
|
48
|
-
|
49
|
-
version_requirements: *id002
|
64
|
+
requirement: *id003
|
50
65
|
- !ruby/object:Gem::Dependency
|
66
|
+
type: :development
|
51
67
|
name: shoulda
|
52
68
|
prerelease: false
|
53
|
-
|
69
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
54
70
|
none: false
|
55
71
|
requirements:
|
56
72
|
- - ">="
|
@@ -59,12 +75,12 @@ dependencies:
|
|
59
75
|
segments:
|
60
76
|
- 0
|
61
77
|
version: "0"
|
62
|
-
|
63
|
-
version_requirements: *id003
|
78
|
+
requirement: *id004
|
64
79
|
- !ruby/object:Gem::Dependency
|
80
|
+
type: :development
|
65
81
|
name: test-unit
|
66
82
|
prerelease: false
|
67
|
-
|
83
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
68
84
|
none: false
|
69
85
|
requirements:
|
70
86
|
- - ">="
|
@@ -73,8 +89,21 @@ dependencies:
|
|
73
89
|
segments:
|
74
90
|
- 0
|
75
91
|
version: "0"
|
92
|
+
requirement: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
76
94
|
type: :development
|
77
|
-
|
95
|
+
name: rake
|
96
|
+
prerelease: false
|
97
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
requirement: *id006
|
78
107
|
description: Stripe is the easiest way to accept payments online. See https://stripe.com for details.
|
79
108
|
email:
|
80
109
|
- boucher@stripe.com
|
@@ -90,11 +119,6 @@ files:
|
|
90
119
|
- lib/stripe.rb
|
91
120
|
- lib/stripe/version.rb
|
92
121
|
- lib/data/ca-certificates.crt
|
93
|
-
- vendor/stripe-json/lib/json/pure.rb
|
94
|
-
- vendor/stripe-json/lib/json/common.rb
|
95
|
-
- vendor/stripe-json/lib/json/version.rb
|
96
|
-
- vendor/stripe-json/lib/json/pure/generator.rb
|
97
|
-
- vendor/stripe-json/lib/json/pure/parser.rb
|
98
122
|
homepage: https://stripe.com/api
|
99
123
|
licenses: []
|
100
124
|
|
@@ -124,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
148
|
requirements: []
|
125
149
|
|
126
150
|
rubyforge_project:
|
127
|
-
rubygems_version: 1.8.
|
151
|
+
rubygems_version: 1.8.21
|
128
152
|
signing_key:
|
129
153
|
specification_version: 3
|
130
154
|
summary: Ruby bindings for the Stripe API
|
@@ -1,426 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'version')
|
2
|
-
|
3
|
-
module JSON
|
4
|
-
class << self
|
5
|
-
# If _object_ is string-like parse the string and return the parsed result
|
6
|
-
# as a Ruby data structure. Otherwise generate a JSON text from the Ruby
|
7
|
-
# data structure object and return it.
|
8
|
-
#
|
9
|
-
# The _opts_ argument is passed through to generate/parse respectively, see
|
10
|
-
# generate and parse for their documentation.
|
11
|
-
def [](object, opts = {})
|
12
|
-
if object.respond_to? :to_str
|
13
|
-
JSON.parse(object.to_str, opts)
|
14
|
-
else
|
15
|
-
JSON.generate(object, opts)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# Returns the JSON parser class, that is used by JSON. This might be either
|
20
|
-
# JSON::Ext::Parser or JSON::Pure::Parser.
|
21
|
-
attr_reader :parser
|
22
|
-
|
23
|
-
# Set the JSON parser class _parser_ to be used by JSON.
|
24
|
-
def parser=(parser) # :nodoc:
|
25
|
-
@parser = parser
|
26
|
-
remove_const :Parser if JSON.const_defined_in?(self, :Parser)
|
27
|
-
const_set :Parser, parser
|
28
|
-
end
|
29
|
-
|
30
|
-
# Return the constant located at _path_. The format of _path_ has to be
|
31
|
-
# either ::A::B::C or A::B::C. In any case A has to be located at the top
|
32
|
-
# level (absolute namespace path?). If there doesn't exist a constant at
|
33
|
-
# the given path, an ArgumentError is raised.
|
34
|
-
def deep_const_get(path) # :nodoc:
|
35
|
-
path.to_s.split(/::/).inject(Object) do |p, c|
|
36
|
-
case
|
37
|
-
when c.empty? then p
|
38
|
-
when JSON.const_defined_in?(p, c) then p.const_get(c)
|
39
|
-
else
|
40
|
-
begin
|
41
|
-
p.const_missing(c)
|
42
|
-
rescue NameError => e
|
43
|
-
raise ArgumentError, "can't get const #{path}: #{e}"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
# Set the module _generator_ to be used by JSON.
|
50
|
-
def generator=(generator) # :nodoc:
|
51
|
-
old, $VERBOSE = $VERBOSE, nil
|
52
|
-
@generator = generator
|
53
|
-
generator_methods = generator::GeneratorMethods
|
54
|
-
for const in generator_methods.constants
|
55
|
-
klass = deep_const_get(const)
|
56
|
-
modul = generator_methods.const_get(const)
|
57
|
-
klass.class_eval do
|
58
|
-
instance_methods(false).each do |m|
|
59
|
-
m.to_s == 'to_json' and remove_method m
|
60
|
-
end
|
61
|
-
include modul
|
62
|
-
end
|
63
|
-
end
|
64
|
-
self.state = generator::State
|
65
|
-
const_set :State, self.state
|
66
|
-
const_set :SAFE_STATE_PROTOTYPE, State.new
|
67
|
-
const_set :FAST_STATE_PROTOTYPE, State.new(
|
68
|
-
:indent => '',
|
69
|
-
:space => '',
|
70
|
-
:object_nl => "",
|
71
|
-
:array_nl => "",
|
72
|
-
:max_nesting => false
|
73
|
-
)
|
74
|
-
const_set :PRETTY_STATE_PROTOTYPE, State.new(
|
75
|
-
:indent => ' ',
|
76
|
-
:space => ' ',
|
77
|
-
:object_nl => "\n",
|
78
|
-
:array_nl => "\n"
|
79
|
-
)
|
80
|
-
ensure
|
81
|
-
$VERBOSE = old
|
82
|
-
end
|
83
|
-
|
84
|
-
# Returns the JSON generator modul, that is used by JSON. This might be
|
85
|
-
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
86
|
-
attr_reader :generator
|
87
|
-
|
88
|
-
# Returns the JSON generator state class, that is used by JSON. This might
|
89
|
-
# be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
90
|
-
attr_accessor :state
|
91
|
-
|
92
|
-
# This is create identifier, that is used to decide, if the _json_create_
|
93
|
-
# hook of a class should be called. It defaults to 'json_class'.
|
94
|
-
attr_accessor :create_id
|
95
|
-
end
|
96
|
-
self.create_id = 'json_class'
|
97
|
-
|
98
|
-
NaN = 0.0/0
|
99
|
-
|
100
|
-
Infinity = 1.0/0
|
101
|
-
|
102
|
-
MinusInfinity = -Infinity
|
103
|
-
|
104
|
-
# The base exception for JSON errors.
|
105
|
-
class JSONError < StandardError; end
|
106
|
-
|
107
|
-
# This exception is raised, if a parser error occurs.
|
108
|
-
class ParserError < JSONError; end
|
109
|
-
|
110
|
-
# this is what activesupport expects :(
|
111
|
-
class ParseError < ParserError; end
|
112
|
-
|
113
|
-
# This exception is raised, if the nesting of parsed datastructures is too
|
114
|
-
# deep.
|
115
|
-
class NestingError < ParserError; end
|
116
|
-
|
117
|
-
# :stopdoc:
|
118
|
-
class CircularDatastructure < NestingError; end
|
119
|
-
# :startdoc:
|
120
|
-
|
121
|
-
# This exception is raised, if a generator or unparser error occurs.
|
122
|
-
class GeneratorError < JSONError; end
|
123
|
-
# For backwards compatibility
|
124
|
-
UnparserError = GeneratorError
|
125
|
-
|
126
|
-
# This exception is raised, if the required unicode support is missing on the
|
127
|
-
# system. Usually this means, that the iconv library is not installed.
|
128
|
-
class MissingUnicodeSupport < JSONError; end
|
129
|
-
|
130
|
-
module_function
|
131
|
-
|
132
|
-
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
133
|
-
#
|
134
|
-
# _opts_ can have the following
|
135
|
-
# keys:
|
136
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
137
|
-
# structures. Disable depth checking with :max_nesting => false, it defaults
|
138
|
-
# to 19.
|
139
|
-
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
140
|
-
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
141
|
-
# to false.
|
142
|
-
# * *symbolize_names*: If set to true, returns symbols for the names
|
143
|
-
# (keys) in a JSON object. Otherwise strings are returned, which is also
|
144
|
-
# the default.
|
145
|
-
# * *create_additions*: If set to false, the Parser doesn't create
|
146
|
-
# additions even if a matchin class and create_id was found. This option
|
147
|
-
# defaults to true.
|
148
|
-
# * *object_class*: Defaults to Hash
|
149
|
-
# * *array_class*: Defaults to Array
|
150
|
-
def parse(source, opts = {})
|
151
|
-
Parser.new(source, opts).parse
|
152
|
-
end
|
153
|
-
|
154
|
-
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
155
|
-
# The bang version of the parse method, defaults to the more dangerous values
|
156
|
-
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
157
|
-
#
|
158
|
-
# _opts_ can have the following keys:
|
159
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
160
|
-
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
|
161
|
-
# methods defaults to not doing max depth checking: This can be dangerous,
|
162
|
-
# if someone wants to fill up your stack.
|
163
|
-
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
|
164
|
-
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
165
|
-
# to true.
|
166
|
-
# * *create_additions*: If set to false, the Parser doesn't create
|
167
|
-
# additions even if a matchin class and create_id was found. This option
|
168
|
-
# defaults to true.
|
169
|
-
def parse!(source, opts = {})
|
170
|
-
opts = {
|
171
|
-
:max_nesting => false,
|
172
|
-
:allow_nan => true
|
173
|
-
}.update(opts)
|
174
|
-
Parser.new(source, opts).parse
|
175
|
-
end
|
176
|
-
|
177
|
-
# Generate a JSON document from the Ruby data structure _obj_ and return
|
178
|
-
# it. _state_ is * a JSON::State object,
|
179
|
-
# * or a Hash like object (responding to to_hash),
|
180
|
-
# * an object convertible into a hash by a to_h method,
|
181
|
-
# that is used as or to configure a State object.
|
182
|
-
#
|
183
|
-
# It defaults to a state object, that creates the shortest possible JSON text
|
184
|
-
# in one line, checks for circular data structures and doesn't allow NaN,
|
185
|
-
# Infinity, and -Infinity.
|
186
|
-
#
|
187
|
-
# A _state_ hash can have the following keys:
|
188
|
-
# * *indent*: a string used to indent levels (default: ''),
|
189
|
-
# * *space*: a string that is put after, a : or , delimiter (default: ''),
|
190
|
-
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
191
|
-
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
192
|
-
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
193
|
-
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
194
|
-
# generated, otherwise an exception is thrown, if these values are
|
195
|
-
# encountered. This options defaults to false.
|
196
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
197
|
-
# structures from which JSON is to be generated. Disable depth checking
|
198
|
-
# with :max_nesting => false, it defaults to 19.
|
199
|
-
#
|
200
|
-
# See also the fast_generate for the fastest creation method with the least
|
201
|
-
# amount of sanity checks, and the pretty_generate method for some
|
202
|
-
# defaults for a pretty output.
|
203
|
-
def generate(obj, opts = nil)
|
204
|
-
state = SAFE_STATE_PROTOTYPE.dup
|
205
|
-
if opts
|
206
|
-
if opts.respond_to? :to_hash
|
207
|
-
opts = opts.to_hash
|
208
|
-
elsif opts.respond_to? :to_h
|
209
|
-
opts = opts.to_h
|
210
|
-
else
|
211
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
212
|
-
end
|
213
|
-
state = state.configure(opts)
|
214
|
-
end
|
215
|
-
state.generate(obj)
|
216
|
-
end
|
217
|
-
|
218
|
-
# :stopdoc:
|
219
|
-
# I want to deprecate these later, so I'll first be silent about them, and
|
220
|
-
# later delete them.
|
221
|
-
alias unparse generate
|
222
|
-
module_function :unparse
|
223
|
-
# :startdoc:
|
224
|
-
|
225
|
-
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
226
|
-
# This method disables the checks for circles in Ruby objects.
|
227
|
-
#
|
228
|
-
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
229
|
-
# _obj_ argument, because this will cause JSON to go into an infinite loop.
|
230
|
-
def fast_generate(obj, opts = nil)
|
231
|
-
state = FAST_STATE_PROTOTYPE.dup
|
232
|
-
if opts
|
233
|
-
if opts.respond_to? :to_hash
|
234
|
-
opts = opts.to_hash
|
235
|
-
elsif opts.respond_to? :to_h
|
236
|
-
opts = opts.to_h
|
237
|
-
else
|
238
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
239
|
-
end
|
240
|
-
state.configure(opts)
|
241
|
-
end
|
242
|
-
state.generate(obj)
|
243
|
-
end
|
244
|
-
|
245
|
-
# :stopdoc:
|
246
|
-
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
247
|
-
alias fast_unparse fast_generate
|
248
|
-
module_function :fast_unparse
|
249
|
-
# :startdoc:
|
250
|
-
|
251
|
-
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
252
|
-
# The returned document is a prettier form of the document returned by
|
253
|
-
# #unparse.
|
254
|
-
#
|
255
|
-
# The _opts_ argument can be used to configure the generator, see the
|
256
|
-
# generate method for a more detailed explanation.
|
257
|
-
def pretty_generate(obj, opts = nil)
|
258
|
-
state = PRETTY_STATE_PROTOTYPE.dup
|
259
|
-
if opts
|
260
|
-
if opts.respond_to? :to_hash
|
261
|
-
opts = opts.to_hash
|
262
|
-
elsif opts.respond_to? :to_h
|
263
|
-
opts = opts.to_h
|
264
|
-
else
|
265
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
266
|
-
end
|
267
|
-
state.configure(opts)
|
268
|
-
end
|
269
|
-
state.generate(obj)
|
270
|
-
end
|
271
|
-
|
272
|
-
# :stopdoc:
|
273
|
-
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
274
|
-
alias pretty_unparse pretty_generate
|
275
|
-
module_function :pretty_unparse
|
276
|
-
# :startdoc:
|
277
|
-
|
278
|
-
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
279
|
-
# either be a string-like object, an IO like object, or an object responding
|
280
|
-
# to the read method. If _proc_ was given, it will be called with any nested
|
281
|
-
# Ruby object as an argument recursively in depth first order.
|
282
|
-
#
|
283
|
-
# This method is part of the implementation of the load/dump interface of
|
284
|
-
# Marshal and YAML.
|
285
|
-
def load(source, proc = nil)
|
286
|
-
if source.respond_to? :to_str
|
287
|
-
source = source.to_str
|
288
|
-
elsif source.respond_to? :to_io
|
289
|
-
source = source.to_io.read
|
290
|
-
else
|
291
|
-
source = source.read
|
292
|
-
end
|
293
|
-
result = parse(source, :max_nesting => false, :allow_nan => true)
|
294
|
-
recurse_proc(result, &proc) if proc
|
295
|
-
result
|
296
|
-
end
|
297
|
-
|
298
|
-
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
299
|
-
def recurse_proc(result, &proc)
|
300
|
-
case result
|
301
|
-
when Array
|
302
|
-
result.each { |x| recurse_proc x, &proc }
|
303
|
-
proc.call result
|
304
|
-
when Hash
|
305
|
-
result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
|
306
|
-
proc.call result
|
307
|
-
else
|
308
|
-
proc.call result
|
309
|
-
end
|
310
|
-
end
|
311
|
-
|
312
|
-
alias restore load
|
313
|
-
module_function :restore
|
314
|
-
|
315
|
-
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
316
|
-
# the result.
|
317
|
-
#
|
318
|
-
# If anIO (an IO like object or an object that responds to the write method)
|
319
|
-
# was given, the resulting JSON is written to it.
|
320
|
-
#
|
321
|
-
# If the number of nested arrays or objects exceeds _limit_ an ArgumentError
|
322
|
-
# exception is raised. This argument is similar (but not exactly the
|
323
|
-
# same!) to the _limit_ argument in Marshal.dump.
|
324
|
-
#
|
325
|
-
# This method is part of the implementation of the load/dump interface of
|
326
|
-
# Marshal and YAML.
|
327
|
-
def dump(obj, anIO = nil, limit = nil)
|
328
|
-
if anIO and limit.nil?
|
329
|
-
anIO = anIO.to_io if anIO.respond_to?(:to_io)
|
330
|
-
unless anIO.respond_to?(:write)
|
331
|
-
limit = anIO
|
332
|
-
anIO = nil
|
333
|
-
end
|
334
|
-
end
|
335
|
-
limit ||= 0
|
336
|
-
result = generate(obj, :allow_nan => true, :max_nesting => limit)
|
337
|
-
if anIO
|
338
|
-
anIO.write result
|
339
|
-
anIO
|
340
|
-
else
|
341
|
-
result
|
342
|
-
end
|
343
|
-
rescue JSON::NestingError
|
344
|
-
raise ArgumentError, "exceed depth limit"
|
345
|
-
end
|
346
|
-
|
347
|
-
# Swap consecutive bytes of _string_ in place.
|
348
|
-
def self.swap!(string) # :nodoc:
|
349
|
-
0.upto(string.size / 2) do |i|
|
350
|
-
break unless string[2 * i + 1]
|
351
|
-
string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
|
352
|
-
end
|
353
|
-
string
|
354
|
-
end
|
355
|
-
|
356
|
-
# Shortuct for iconv.
|
357
|
-
if ::String.method_defined?(:encode)
|
358
|
-
# Encodes string using Ruby's _String.encode_
|
359
|
-
def self.iconv(to, from, string)
|
360
|
-
string.encode(to, from)
|
361
|
-
end
|
362
|
-
else
|
363
|
-
require 'iconv'
|
364
|
-
# Encodes string using _iconv_ library
|
365
|
-
def self.iconv(to, from, string)
|
366
|
-
Iconv.conv(to, from, string)
|
367
|
-
end
|
368
|
-
end
|
369
|
-
|
370
|
-
if ::Object.method(:const_defined?).arity == 1
|
371
|
-
def self.const_defined_in?(modul, constant)
|
372
|
-
modul.const_defined?(constant)
|
373
|
-
end
|
374
|
-
else
|
375
|
-
def self.const_defined_in?(modul, constant)
|
376
|
-
modul.const_defined?(constant, false)
|
377
|
-
end
|
378
|
-
end
|
379
|
-
end
|
380
|
-
|
381
|
-
module ::Kernel
|
382
|
-
private
|
383
|
-
|
384
|
-
# Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
|
385
|
-
# one line.
|
386
|
-
def j(*objs)
|
387
|
-
objs.each do |obj|
|
388
|
-
puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
|
389
|
-
end
|
390
|
-
nil
|
391
|
-
end
|
392
|
-
|
393
|
-
# Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
|
394
|
-
# indentation and over many lines.
|
395
|
-
def jj(*objs)
|
396
|
-
objs.each do |obj|
|
397
|
-
puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
|
398
|
-
end
|
399
|
-
nil
|
400
|
-
end
|
401
|
-
|
402
|
-
# If _object_ is string-like parse the string and return the parsed result as
|
403
|
-
# a Ruby data structure. Otherwise generate a JSON text from the Ruby data
|
404
|
-
# structure object and return it.
|
405
|
-
#
|
406
|
-
# The _opts_ argument is passed through to generate/parse respectively, see
|
407
|
-
# generate and parse for their documentation.
|
408
|
-
def JSON(object, *args)
|
409
|
-
if object.respond_to? :to_str
|
410
|
-
JSON.parse(object.to_str, args.first)
|
411
|
-
else
|
412
|
-
JSON.generate(object, args.first)
|
413
|
-
end
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
# Extends any Class to include _json_creatable?_ method.
|
418
|
-
class ::Class
|
419
|
-
# Returns true, if this class can be used to create an instance
|
420
|
-
# from a serialised JSON string. The class has to implement a class
|
421
|
-
# method _json_create_ that expects a hash as first parameter, which includes
|
422
|
-
# the required data.
|
423
|
-
def json_creatable?
|
424
|
-
respond_to?(:json_create)
|
425
|
-
end
|
426
|
-
end
|