startapp 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/COPYRIGHT +1 -0
  3. data/LICENSE +11 -0
  4. data/README.md +95 -0
  5. data/Rakefile +6 -0
  6. data/autocomplete/rhc_bash +1672 -0
  7. data/bin/app +37 -0
  8. data/conf/express.conf +8 -0
  9. data/features/assets/deploy.tar.gz +0 -0
  10. data/features/core_feature.rb +191 -0
  11. data/features/deployments_feature.rb +129 -0
  12. data/features/domains_feature.rb +58 -0
  13. data/features/keys_feature.rb +37 -0
  14. data/features/members_feature.rb +166 -0
  15. data/lib/rhc/auth/basic.rb +64 -0
  16. data/lib/rhc/auth/token.rb +102 -0
  17. data/lib/rhc/auth/token_store.rb +53 -0
  18. data/lib/rhc/auth.rb +5 -0
  19. data/lib/rhc/autocomplete.rb +66 -0
  20. data/lib/rhc/autocomplete_templates/bash.erb +39 -0
  21. data/lib/rhc/cartridge_helpers.rb +118 -0
  22. data/lib/rhc/cli.rb +40 -0
  23. data/lib/rhc/command_runner.rb +185 -0
  24. data/lib/rhc/commands/account.rb +25 -0
  25. data/lib/rhc/commands/alias.rb +124 -0
  26. data/lib/rhc/commands/app.rb +726 -0
  27. data/lib/rhc/commands/apps.rb +20 -0
  28. data/lib/rhc/commands/authorization.rb +115 -0
  29. data/lib/rhc/commands/base.rb +174 -0
  30. data/lib/rhc/commands/cartridge.rb +329 -0
  31. data/lib/rhc/commands/clone.rb +66 -0
  32. data/lib/rhc/commands/configure.rb +20 -0
  33. data/lib/rhc/commands/create.rb +100 -0
  34. data/lib/rhc/commands/delete.rb +19 -0
  35. data/lib/rhc/commands/deploy.rb +32 -0
  36. data/lib/rhc/commands/deployment.rb +82 -0
  37. data/lib/rhc/commands/domain.rb +172 -0
  38. data/lib/rhc/commands/env.rb +142 -0
  39. data/lib/rhc/commands/force_stop.rb +17 -0
  40. data/lib/rhc/commands/git_clone.rb +34 -0
  41. data/lib/rhc/commands/logout.rb +51 -0
  42. data/lib/rhc/commands/logs.rb +21 -0
  43. data/lib/rhc/commands/member.rb +148 -0
  44. data/lib/rhc/commands/port_forward.rb +197 -0
  45. data/lib/rhc/commands/reload.rb +17 -0
  46. data/lib/rhc/commands/restart.rb +17 -0
  47. data/lib/rhc/commands/scp.rb +54 -0
  48. data/lib/rhc/commands/server.rb +40 -0
  49. data/lib/rhc/commands/setup.rb +60 -0
  50. data/lib/rhc/commands/show.rb +43 -0
  51. data/lib/rhc/commands/snapshot.rb +137 -0
  52. data/lib/rhc/commands/ssh.rb +51 -0
  53. data/lib/rhc/commands/sshkey.rb +97 -0
  54. data/lib/rhc/commands/start.rb +17 -0
  55. data/lib/rhc/commands/stop.rb +17 -0
  56. data/lib/rhc/commands/tail.rb +47 -0
  57. data/lib/rhc/commands/threaddump.rb +14 -0
  58. data/lib/rhc/commands/tidy.rb +17 -0
  59. data/lib/rhc/commands.rb +396 -0
  60. data/lib/rhc/config.rb +321 -0
  61. data/lib/rhc/context_helper.rb +121 -0
  62. data/lib/rhc/core_ext.rb +202 -0
  63. data/lib/rhc/coverage_helper.rb +33 -0
  64. data/lib/rhc/deployment_helpers.rb +111 -0
  65. data/lib/rhc/exceptions.rb +256 -0
  66. data/lib/rhc/git_helpers.rb +106 -0
  67. data/lib/rhc/help_formatter.rb +55 -0
  68. data/lib/rhc/helpers.rb +481 -0
  69. data/lib/rhc/highline_extensions.rb +479 -0
  70. data/lib/rhc/json.rb +51 -0
  71. data/lib/rhc/output_helpers.rb +260 -0
  72. data/lib/rhc/rest/activation.rb +11 -0
  73. data/lib/rhc/rest/alias.rb +42 -0
  74. data/lib/rhc/rest/api.rb +87 -0
  75. data/lib/rhc/rest/application.rb +348 -0
  76. data/lib/rhc/rest/attributes.rb +36 -0
  77. data/lib/rhc/rest/authorization.rb +8 -0
  78. data/lib/rhc/rest/base.rb +79 -0
  79. data/lib/rhc/rest/cartridge.rb +162 -0
  80. data/lib/rhc/rest/client.rb +650 -0
  81. data/lib/rhc/rest/deployment.rb +18 -0
  82. data/lib/rhc/rest/domain.rb +98 -0
  83. data/lib/rhc/rest/environment_variable.rb +15 -0
  84. data/lib/rhc/rest/gear_group.rb +16 -0
  85. data/lib/rhc/rest/httpclient.rb +145 -0
  86. data/lib/rhc/rest/key.rb +44 -0
  87. data/lib/rhc/rest/membership.rb +105 -0
  88. data/lib/rhc/rest/mock.rb +1042 -0
  89. data/lib/rhc/rest/user.rb +32 -0
  90. data/lib/rhc/rest.rb +148 -0
  91. data/lib/rhc/scp_helpers.rb +27 -0
  92. data/lib/rhc/ssh_helpers.rb +380 -0
  93. data/lib/rhc/tar_gz.rb +51 -0
  94. data/lib/rhc/usage_templates/command_help.erb +51 -0
  95. data/lib/rhc/usage_templates/command_syntax_help.erb +11 -0
  96. data/lib/rhc/usage_templates/help.erb +61 -0
  97. data/lib/rhc/usage_templates/missing_help.erb +1 -0
  98. data/lib/rhc/usage_templates/options_help.erb +12 -0
  99. data/lib/rhc/vendor/okjson.rb +600 -0
  100. data/lib/rhc/vendor/parseconfig.rb +178 -0
  101. data/lib/rhc/vendor/sshkey.rb +253 -0
  102. data/lib/rhc/vendor/zliby.rb +628 -0
  103. data/lib/rhc/version.rb +5 -0
  104. data/lib/rhc/wizard.rb +637 -0
  105. data/lib/rhc.rb +34 -0
  106. data/spec/coverage_helper.rb +82 -0
  107. data/spec/direct_execution_helper.rb +339 -0
  108. data/spec/keys/example.pem +23 -0
  109. data/spec/keys/example_private.pem +27 -0
  110. data/spec/keys/server.pem +19 -0
  111. data/spec/rest_spec_helper.rb +31 -0
  112. data/spec/rhc/assets/cert.crt +22 -0
  113. data/spec/rhc/assets/cert_key_rsa +27 -0
  114. data/spec/rhc/assets/empty.txt +0 -0
  115. data/spec/rhc/assets/env_vars.txt +7 -0
  116. data/spec/rhc/assets/env_vars_2.txt +1 -0
  117. data/spec/rhc/assets/foo.txt +1 -0
  118. data/spec/rhc/assets/targz_corrupted.tar.gz +1 -0
  119. data/spec/rhc/assets/targz_sample.tar.gz +0 -0
  120. data/spec/rhc/auth_spec.rb +442 -0
  121. data/spec/rhc/cli_spec.rb +186 -0
  122. data/spec/rhc/command_spec.rb +435 -0
  123. data/spec/rhc/commands/account_spec.rb +42 -0
  124. data/spec/rhc/commands/alias_spec.rb +333 -0
  125. data/spec/rhc/commands/app_spec.rb +777 -0
  126. data/spec/rhc/commands/apps_spec.rb +39 -0
  127. data/spec/rhc/commands/authorization_spec.rb +157 -0
  128. data/spec/rhc/commands/cartridge_spec.rb +665 -0
  129. data/spec/rhc/commands/clone_spec.rb +41 -0
  130. data/spec/rhc/commands/deployment_spec.rb +327 -0
  131. data/spec/rhc/commands/domain_spec.rb +401 -0
  132. data/spec/rhc/commands/env_spec.rb +493 -0
  133. data/spec/rhc/commands/git_clone_spec.rb +102 -0
  134. data/spec/rhc/commands/logout_spec.rb +86 -0
  135. data/spec/rhc/commands/member_spec.rb +247 -0
  136. data/spec/rhc/commands/port_forward_spec.rb +217 -0
  137. data/spec/rhc/commands/scp_spec.rb +77 -0
  138. data/spec/rhc/commands/server_spec.rb +69 -0
  139. data/spec/rhc/commands/setup_spec.rb +118 -0
  140. data/spec/rhc/commands/snapshot_spec.rb +179 -0
  141. data/spec/rhc/commands/ssh_spec.rb +163 -0
  142. data/spec/rhc/commands/sshkey_spec.rb +188 -0
  143. data/spec/rhc/commands/tail_spec.rb +81 -0
  144. data/spec/rhc/commands/threaddump_spec.rb +84 -0
  145. data/spec/rhc/config_spec.rb +407 -0
  146. data/spec/rhc/helpers_spec.rb +531 -0
  147. data/spec/rhc/highline_extensions_spec.rb +314 -0
  148. data/spec/rhc/json_spec.rb +30 -0
  149. data/spec/rhc/rest_application_spec.rb +258 -0
  150. data/spec/rhc/rest_client_spec.rb +752 -0
  151. data/spec/rhc/rest_spec.rb +740 -0
  152. data/spec/rhc/targz_spec.rb +55 -0
  153. data/spec/rhc/wizard_spec.rb +756 -0
  154. data/spec/spec_helper.rb +575 -0
  155. data/spec/wizard_spec_helper.rb +330 -0
  156. metadata +469 -0
@@ -0,0 +1,61 @@
1
+ Usage: <%= Array(program :name).first %> [--help] [--version] [--debug] <command> [<args>]
2
+
3
+ <%= Array(program :description).first %>
4
+
5
+ <%
6
+ remaining = Hash[commands.dup.select{ |name, command| not command.summary.blank? }]
7
+ basic = remaining.slice!(
8
+ 'setup',
9
+ 'create',
10
+ 'delete',
11
+ 'clone',
12
+ 'show',
13
+ 'apps',
14
+ 'cartridges',
15
+ 'logout')
16
+ remaining.delete_if{ |name, command| alias?(name) }
17
+ begin -%>
18
+ Getting started:
19
+ <%= table(basic.map{ |arr| arr[1] = arr[1].summary || arr[1].description; arr }, table_args(' ', 18)).join("\n") %>
20
+ <% end unless basic.empty? %>
21
+ <%
22
+
23
+ debugging = remaining.slice!(
24
+ 'add-cartridge',
25
+ 'set-env',
26
+ 'start',
27
+ 'stop',
28
+ 'force-stop',
29
+ 'reload',
30
+ 'restart',
31
+ 'configure',
32
+ 'tidy',
33
+ 'logs',
34
+ 'port-forward',
35
+ 'threaddump',
36
+ 'snapshot',
37
+ 'deploy',
38
+ 'clone')
39
+ begin -%>
40
+ Working with apps:
41
+ <%= table(debugging.map{ |arr| arr[1] = arr[1].summary || arr[1].description; arr }, table_args(' ', 18)).join("\n") %>
42
+ <% end unless debugging.empty? %>
43
+ Management commands:
44
+ <%
45
+ management = remaining.select {|item| item !~ /tail|git-clone|app|server/i }
46
+ begin -%>
47
+ <%= table(management.sort.select{ |arr| arr[1].root? and arr[1].summary.present? }.map do |arr|
48
+ arr[1] = arr[1].summary || arr[1].description
49
+ arr
50
+ end, table_args(' ', 18)).join("\n") %>
51
+ <% end unless management.empty? %>
52
+ Some management commands have nested actions - run '<%= Array(program :name).first %> help <command>' for more info.
53
+
54
+ See '<%= Array(program :name).first %> help <command>' for more information on a specific command. You can see a list of all commands with '<%= Array(program :name).first %> help commands'. See '<%= Array(program :name).first %> help options' for a list of global command-line options and information about the config file.
55
+ <% if program :help -%>
56
+ <% for title, body in program(:help) %>
57
+ <%= $terminal.color title.to_s.upcase, :bold %>:
58
+
59
+ <%= body %>
60
+ <% end -%>
61
+ <% end -%>
@@ -0,0 +1 @@
1
+ See '<%= Array(program :name).first %> help' for a list of available commands.
@@ -0,0 +1,12 @@
1
+ The following options can be passed to any command:
2
+
3
+ <%= table(options.map{ |option| [option[:switches].join(', '), option[:description]] }, table_args(' ', 26)).join("\n") %>
4
+
5
+ The following configuration options are stored in the <%= RHC::Helpers.system_path('.openshift/express.conf') %> file in your home directory. Passing any of these options to 'rhc setup' will save that value to the file:
6
+
7
+ <%= table(
8
+ RHC::Config::OPTIONS.select{ |key,value| not value.nil? }.map do |key,value|
9
+ ["--#{key.to_s.gsub('_','-')}", value[0] || key]
10
+ end,
11
+ table_args(' ', 26).merge(:header => ['Option', 'In Config'])
12
+ ).join("\n") -%>
@@ -0,0 +1,600 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011, 2012 Keith Rarick
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ # See https://github.com/kr/okjson for updates.
24
+
25
+ require 'stringio'
26
+
27
+ # Some parts adapted from
28
+ # http://golang.org/src/pkg/json/decode.go and
29
+ # http://golang.org/src/pkg/utf8/utf8.go
30
+ module RHC
31
+ module Vendor
32
+ module OkJson
33
+ extend self
34
+
35
+
36
+ # Decodes a json document in string s and
37
+ # returns the corresponding ruby value.
38
+ # String s must be valid UTF-8. If you have
39
+ # a string in some other encoding, convert
40
+ # it first.
41
+ #
42
+ # String values in the resulting structure
43
+ # will be UTF-8.
44
+ def decode(s)
45
+ ts = lex(s)
46
+ v, ts = textparse(ts)
47
+ if ts.length > 0
48
+ raise Error, 'trailing garbage'
49
+ end
50
+ v
51
+ end
52
+
53
+
54
+ # Parses a "json text" in the sense of RFC 4627.
55
+ # Returns the parsed value and any trailing tokens.
56
+ # Note: this is almost the same as valparse,
57
+ # except that it does not accept atomic values.
58
+ def textparse(ts)
59
+ if ts.length < 0
60
+ raise Error, 'empty'
61
+ end
62
+
63
+ typ, _, val = ts[0]
64
+ case typ
65
+ when '{' then objparse(ts)
66
+ when '[' then arrparse(ts)
67
+ else
68
+ raise Error, "unexpected #{val.inspect}"
69
+ end
70
+ end
71
+
72
+
73
+ # Parses a "value" in the sense of RFC 4627.
74
+ # Returns the parsed value and any trailing tokens.
75
+ def valparse(ts)
76
+ if ts.length < 0
77
+ raise Error, 'empty'
78
+ end
79
+
80
+ typ, _, val = ts[0]
81
+ case typ
82
+ when '{' then objparse(ts)
83
+ when '[' then arrparse(ts)
84
+ when :val,:str then [val, ts[1..-1]]
85
+ else
86
+ raise Error, "unexpected #{val.inspect}"
87
+ end
88
+ end
89
+
90
+
91
+ # Parses an "object" in the sense of RFC 4627.
92
+ # Returns the parsed value and any trailing tokens.
93
+ def objparse(ts)
94
+ ts = eat('{', ts)
95
+ obj = {}
96
+
97
+ if ts[0][0] == '}'
98
+ return obj, ts[1..-1]
99
+ end
100
+
101
+ k, v, ts = pairparse(ts)
102
+ obj[k] = v
103
+
104
+ if ts[0][0] == '}'
105
+ return obj, ts[1..-1]
106
+ end
107
+
108
+ loop do
109
+ ts = eat(',', ts)
110
+
111
+ k, v, ts = pairparse(ts)
112
+ obj[k] = v
113
+
114
+ if ts[0][0] == '}'
115
+ return obj, ts[1..-1]
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ # Parses a "member" in the sense of RFC 4627.
122
+ # Returns the parsed values and any trailing tokens.
123
+ def pairparse(ts)
124
+ (typ, _, k), ts = ts[0], ts[1..-1]
125
+ if typ != :str
126
+ raise Error, "unexpected #{k.inspect}"
127
+ end
128
+ ts = eat(':', ts)
129
+ v, ts = valparse(ts)
130
+ [k, v, ts]
131
+ end
132
+
133
+
134
+ # Parses an "array" in the sense of RFC 4627.
135
+ # Returns the parsed value and any trailing tokens.
136
+ def arrparse(ts)
137
+ ts = eat('[', ts)
138
+ arr = []
139
+
140
+ if ts[0][0] == ']'
141
+ return arr, ts[1..-1]
142
+ end
143
+
144
+ v, ts = valparse(ts)
145
+ arr << v
146
+
147
+ if ts[0][0] == ']'
148
+ return arr, ts[1..-1]
149
+ end
150
+
151
+ loop do
152
+ ts = eat(',', ts)
153
+
154
+ v, ts = valparse(ts)
155
+ arr << v
156
+
157
+ if ts[0][0] == ']'
158
+ return arr, ts[1..-1]
159
+ end
160
+ end
161
+ end
162
+
163
+
164
+ def eat(typ, ts)
165
+ if ts[0][0] != typ
166
+ raise Error, "expected #{typ} (got #{ts[0].inspect})"
167
+ end
168
+ ts[1..-1]
169
+ end
170
+
171
+
172
+ # Scans s and returns a list of json tokens,
173
+ # excluding white space (as defined in RFC 4627).
174
+ def lex(s)
175
+ ts = []
176
+ while s.length > 0
177
+ typ, lexeme, val = tok(s)
178
+ if typ == nil
179
+ raise Error, "invalid character at #{s[0,10].inspect}"
180
+ end
181
+ if typ != :space
182
+ ts << [typ, lexeme, val]
183
+ end
184
+ s = s[lexeme.length..-1]
185
+ end
186
+ ts
187
+ end
188
+
189
+
190
+ # Scans the first token in s and
191
+ # returns a 3-element list, or nil
192
+ # if s does not begin with a valid token.
193
+ #
194
+ # The first list element is one of
195
+ # '{', '}', ':', ',', '[', ']',
196
+ # :val, :str, and :space.
197
+ #
198
+ # The second element is the lexeme.
199
+ #
200
+ # The third element is the value of the
201
+ # token for :val and :str, otherwise
202
+ # it is the lexeme.
203
+ def tok(s)
204
+ case s[0]
205
+ when ?{ then ['{', s[0,1], s[0,1]]
206
+ when ?} then ['}', s[0,1], s[0,1]]
207
+ when ?: then [':', s[0,1], s[0,1]]
208
+ when ?, then [',', s[0,1], s[0,1]]
209
+ when ?[ then ['[', s[0,1], s[0,1]]
210
+ when ?] then [']', s[0,1], s[0,1]]
211
+ when ?n then nulltok(s)
212
+ when ?t then truetok(s)
213
+ when ?f then falsetok(s)
214
+ when ?" then strtok(s)
215
+ when Spc then [:space, s[0,1], s[0,1]]
216
+ when ?\t then [:space, s[0,1], s[0,1]]
217
+ when ?\n then [:space, s[0,1], s[0,1]]
218
+ when ?\r then [:space, s[0,1], s[0,1]]
219
+ else numtok(s)
220
+ end
221
+ end
222
+
223
+
224
+ def nulltok(s); s[0,4] == 'null' ? [:val, 'null', nil] : [] end
225
+ def truetok(s); s[0,4] == 'true' ? [:val, 'true', true] : [] end
226
+ def falsetok(s); s[0,5] == 'false' ? [:val, 'false', false] : [] end
227
+
228
+
229
+ def numtok(s)
230
+ m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
231
+ if m && m.begin(0) == 0
232
+ if m[3] && !m[2]
233
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
234
+ elsif m[2]
235
+ [:val, m[0], Float(m[0])]
236
+ else
237
+ [:val, m[0], Integer(m[0])]
238
+ end
239
+ else
240
+ []
241
+ end
242
+ end
243
+
244
+
245
+ def strtok(s)
246
+ m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
247
+ if ! m
248
+ raise Error, "invalid string literal at #{abbrev(s)}"
249
+ end
250
+ [:str, m[0], unquote(m[0])]
251
+ end
252
+
253
+
254
+ def abbrev(s)
255
+ t = s[0,10]
256
+ p = t['`']
257
+ t = t[0,p] if p
258
+ t = t + '...' if t.length < s.length
259
+ '`' + t + '`'
260
+ end
261
+
262
+
263
+ # Converts a quoted json string literal q into a UTF-8-encoded string.
264
+ # The rules are different than for Ruby, so we cannot use eval.
265
+ # Unquote will raise an error if q contains control characters.
266
+ def unquote(q)
267
+ q = q[1...-1]
268
+ a = q.dup # allocate a big enough string
269
+ rubydoesenc = false
270
+ # In ruby >= 1.9, a[w] is a codepoint, not a byte.
271
+ if a.class.method_defined?(:force_encoding)
272
+ a.force_encoding('UTF-8')
273
+ rubydoesenc = true
274
+ end
275
+ r, w = 0, 0
276
+ while r < q.length
277
+ c = q[r]
278
+ case true
279
+ when c == ?\\
280
+ r += 1
281
+ if r >= q.length
282
+ raise Error, "string literal ends with a \"\\\": \"#{q}\""
283
+ end
284
+
285
+ case q[r]
286
+ when ?",?\\,?/,?'
287
+ a[w] = q[r]
288
+ r += 1
289
+ w += 1
290
+ when ?b,?f,?n,?r,?t
291
+ a[w] = Unesc[q[r]]
292
+ r += 1
293
+ w += 1
294
+ when ?u
295
+ r += 1
296
+ uchar = begin
297
+ hexdec4(q[r,4])
298
+ rescue RuntimeError => e
299
+ raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
300
+ end
301
+ r += 4
302
+ if surrogate? uchar
303
+ if q.length >= r+6
304
+ uchar1 = hexdec4(q[r+2,4])
305
+ uchar = subst(uchar, uchar1)
306
+ if uchar != Ucharerr
307
+ # A valid pair; consume.
308
+ r += 6
309
+ end
310
+ end
311
+ end
312
+ if rubydoesenc
313
+ a[w] = '' << uchar
314
+ w += 1
315
+ else
316
+ w += ucharenc(a, w, uchar)
317
+ end
318
+ else
319
+ raise Error, "invalid escape char #{q[r]} in \"#{q}\""
320
+ end
321
+ when c == ?", c < Spc
322
+ raise Error, "invalid character in string literal \"#{q}\""
323
+ else
324
+ # Copy anything else byte-for-byte.
325
+ # Valid UTF-8 will remain valid UTF-8.
326
+ # Invalid UTF-8 will remain invalid UTF-8.
327
+ # In ruby >= 1.9, c is a codepoint, not a byte,
328
+ # in which case this is still what we want.
329
+ a[w] = c
330
+ r += 1
331
+ w += 1
332
+ end
333
+ end
334
+ a[0,w]
335
+ end
336
+
337
+
338
+ # Encodes unicode character u as UTF-8
339
+ # bytes in string a at position i.
340
+ # Returns the number of bytes written.
341
+ def ucharenc(a, i, u)
342
+ case true
343
+ when u <= Uchar1max
344
+ a[i] = (u & 0xff).chr
345
+ 1
346
+ when u <= Uchar2max
347
+ a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
348
+ a[i+1] = (Utagx | (u&Umaskx)).chr
349
+ 2
350
+ when u <= Uchar3max
351
+ a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
352
+ a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
353
+ a[i+2] = (Utagx | (u&Umaskx)).chr
354
+ 3
355
+ else
356
+ a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
357
+ a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
358
+ a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
359
+ a[i+3] = (Utagx | (u&Umaskx)).chr
360
+ 4
361
+ end
362
+ end
363
+
364
+
365
+ def hexdec4(s)
366
+ if s.length != 4
367
+ raise Error, 'short'
368
+ end
369
+ (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
370
+ end
371
+
372
+
373
+ def subst(u1, u2)
374
+ if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
375
+ return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
376
+ end
377
+ return Ucharerr
378
+ end
379
+
380
+
381
+ def surrogate?(u)
382
+ Usurr1 <= u && u < Usurr3
383
+ end
384
+
385
+
386
+ def nibble(c)
387
+ case true
388
+ when ?0 <= c && c <= ?9 then c.ord - ?0.ord
389
+ when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
390
+ when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
391
+ else
392
+ raise Error, "invalid hex code #{c}"
393
+ end
394
+ end
395
+
396
+
397
+ # Encodes x into a json text. It may contain only
398
+ # Array, Hash, String, Numeric, true, false, nil.
399
+ # (Note, this list excludes Symbol.)
400
+ # X itself must be an Array or a Hash.
401
+ # No other value can be encoded, and an error will
402
+ # be raised if x contains any other value, such as
403
+ # Nan, Infinity, Symbol, and Proc, or if a Hash key
404
+ # is not a String.
405
+ # Strings contained in x must be valid UTF-8.
406
+ def encode(x)
407
+ case x
408
+ when Hash then objenc(x)
409
+ when Array then arrenc(x)
410
+ else
411
+ raise Error, 'root value must be an Array or a Hash'
412
+ end
413
+ end
414
+
415
+
416
+ def valenc(x)
417
+ case x
418
+ when Hash then objenc(x)
419
+ when Array then arrenc(x)
420
+ when String then strenc(x)
421
+ when Numeric then numenc(x)
422
+ when true then "true"
423
+ when false then "false"
424
+ when nil then "null"
425
+ else
426
+ raise Error, "cannot encode #{x.class}: #{x.inspect}"
427
+ end
428
+ end
429
+
430
+
431
+ def objenc(x)
432
+ '{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
433
+ end
434
+
435
+
436
+ def arrenc(a)
437
+ '[' + a.map{|x| valenc(x)}.join(',') + ']'
438
+ end
439
+
440
+
441
+ def keyenc(k)
442
+ case k
443
+ when String then strenc(k)
444
+ else
445
+ raise Error, "Hash key is not a string: #{k.inspect}"
446
+ end
447
+ end
448
+
449
+
450
+ def strenc(s)
451
+ t = StringIO.new
452
+ t.putc(?")
453
+ r = 0
454
+
455
+ # In ruby >= 1.9, s[r] is a codepoint, not a byte.
456
+ rubydoesenc = s.class.method_defined?(:encoding)
457
+
458
+ while r < s.length
459
+ case s[r]
460
+ when ?" then t.print('\\"')
461
+ when ?\\ then t.print('\\\\')
462
+ when ?\b then t.print('\\b')
463
+ when ?\f then t.print('\\f')
464
+ when ?\n then t.print('\\n')
465
+ when ?\r then t.print('\\r')
466
+ when ?\t then t.print('\\t')
467
+ else
468
+ c = s[r]
469
+ case true
470
+ when rubydoesenc
471
+ begin
472
+ c.ord # will raise an error if c is invalid UTF-8
473
+ t.write(c)
474
+ rescue
475
+ t.write(Ustrerr)
476
+ end
477
+ when Spc <= c && c <= ?~
478
+ t.putc(c)
479
+ else
480
+ n = ucharcopy(t, s, r) # ensure valid UTF-8 output
481
+ r += n - 1 # r is incremented below
482
+ end
483
+ end
484
+ r += 1
485
+ end
486
+ t.putc(?")
487
+ t.string
488
+ end
489
+
490
+
491
+ def numenc(x)
492
+ if ((x.nan? || x.infinite?) rescue false)
493
+ raise Error, "Numeric cannot be represented: #{x}"
494
+ end
495
+ "#{x}"
496
+ end
497
+
498
+
499
+ # Copies the valid UTF-8 bytes of a single character
500
+ # from string s at position i to I/O object t, and
501
+ # returns the number of bytes copied.
502
+ # If no valid UTF-8 char exists at position i,
503
+ # ucharcopy writes Ustrerr and returns 1.
504
+ def ucharcopy(t, s, i)
505
+ n = s.length - i
506
+ raise Utf8Error if n < 1
507
+
508
+ c0 = s[i].ord
509
+
510
+ # 1-byte, 7-bit sequence?
511
+ if c0 < Utagx
512
+ t.putc(c0)
513
+ return 1
514
+ end
515
+
516
+ raise Utf8Error if c0 < Utag2 # unexpected continuation byte?
517
+
518
+ raise Utf8Error if n < 2 # need continuation byte
519
+ c1 = s[i+1].ord
520
+ raise Utf8Error if c1 < Utagx || Utag2 <= c1
521
+
522
+ # 2-byte, 11-bit sequence?
523
+ if c0 < Utag3
524
+ raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
525
+ t.putc(c0)
526
+ t.putc(c1)
527
+ return 2
528
+ end
529
+
530
+ # need second continuation byte
531
+ raise Utf8Error if n < 3
532
+
533
+ c2 = s[i+2].ord
534
+ raise Utf8Error if c2 < Utagx || Utag2 <= c2
535
+
536
+ # 3-byte, 16-bit sequence?
537
+ if c0 < Utag4
538
+ u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
539
+ raise Utf8Error if u <= Uchar2max
540
+ t.putc(c0)
541
+ t.putc(c1)
542
+ t.putc(c2)
543
+ return 3
544
+ end
545
+
546
+ # need third continuation byte
547
+ raise Utf8Error if n < 4
548
+ c3 = s[i+3].ord
549
+ raise Utf8Error if c3 < Utagx || Utag2 <= c3
550
+
551
+ # 4-byte, 21-bit sequence?
552
+ if c0 < Utag5
553
+ u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
554
+ raise Utf8Error if u <= Uchar3max
555
+ t.putc(c0)
556
+ t.putc(c1)
557
+ t.putc(c2)
558
+ t.putc(c3)
559
+ return 4
560
+ end
561
+
562
+ raise Utf8Error
563
+ rescue Utf8Error
564
+ t.write(Ustrerr)
565
+ return 1
566
+ end
567
+
568
+
569
+ class Utf8Error < ::StandardError
570
+ end
571
+
572
+
573
+ class Error < ::StandardError
574
+ end
575
+
576
+
577
+ Utagx = 0x80 # 1000 0000
578
+ Utag2 = 0xc0 # 1100 0000
579
+ Utag3 = 0xe0 # 1110 0000
580
+ Utag4 = 0xf0 # 1111 0000
581
+ Utag5 = 0xF8 # 1111 1000
582
+ Umaskx = 0x3f # 0011 1111
583
+ Umask2 = 0x1f # 0001 1111
584
+ Umask3 = 0x0f # 0000 1111
585
+ Umask4 = 0x07 # 0000 0111
586
+ Uchar1max = (1<<7) - 1
587
+ Uchar2max = (1<<11) - 1
588
+ Uchar3max = (1<<16) - 1
589
+ Ucharerr = 0xFFFD # unicode "replacement char"
590
+ Ustrerr = "\xef\xbf\xbd" # unicode "replacement char"
591
+ Usurrself = 0x10000
592
+ Usurr1 = 0xd800
593
+ Usurr2 = 0xdc00
594
+ Usurr3 = 0xe000
595
+
596
+ Spc = ' '[0]
597
+ Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
598
+ end
599
+ end
600
+ end