heroics 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0effae3c006baa9114cb3878ae9c7fc2fdc8f9b7
4
+ data.tar.gz: 8f0504e1fb8b25ac738fd90cf7aadc3a8b5d281e
5
+ SHA512:
6
+ metadata.gz: 3a6e38662083ef292abbee6120036e74ebc865fb02728f47e6eaf1a65fdf9b6b45333db618de163f8c1e7775b4c4dff4b54a713d230ac1946c10ea2f5dbcc0c3
7
+ data.tar.gz: b56f94aa12117967c28116830637e0650c5ef1b5d1b45b7d67618344400e6ba28f4f19ae77ff0e1bd0a6648edae11665ee9c76de033a3498dca42e117f61a85c
data/README.md CHANGED
@@ -18,29 +18,12 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- ### Generate a client from a JSON schema
21
+ ### Instantiate a client from a JSON schema
22
22
 
23
- Heroics generates an HTTP client from a JSON schema. The simplest way
24
- to get started is to provide the URL to the schema you want to base
25
- the client on:
26
-
27
- ```ruby
28
- require 'cgi'
29
- require 'heroics'
30
-
31
- username = CGI.escape('username')
32
- token = 'token'
33
- url = "https://#{username}:#{token}@api.heroku.com/schema"
34
- options = {default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'}}
35
- client = Heroics.client_from_schema_url(url, options)
36
- ```
37
-
38
- The client will make requests to the API using the credentials from
39
- the URL. The default headers will also be included in all requests
40
- (including the one to download the schema and all subsequent
41
- requests).
42
-
43
- You can also create a client from an in-memory schema object:
23
+ Heroics instantiates an HTTP client from a JSON schema. The client
24
+ will make requests to the API using the credentials from the URL. The
25
+ default headers will also be included in all requests (including the
26
+ one to download the schema and all subsequent requests).
44
27
 
45
28
  ```ruby
46
29
  require 'cgi'
@@ -62,12 +45,14 @@ Heroics handles ETags and will cache data on the client if you provide
62
45
  a [Moneta](https://github.com/minad/moneta) cache instance.
63
46
 
64
47
  ```ruby
65
- username = 'username'
48
+ username = CGI.escape('username')
66
49
  token = 'token'
67
50
  url = "https://#{username}:#{token}@api.heroku.com/schema"
68
51
  options = {default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'},
69
52
  cache: Moneta.new(:File, dir: "#{Dir.home}/.heroics/heroku-api")}
70
- client = Heroics.client_from_schema_url(url, options)
53
+ data = JSON.parse(File.read('schema.json'))
54
+ schema = Heroics::Schema.new(data)
55
+ client = Heroics.client_from_schema(schema, url, options)
71
56
  ```
72
57
 
73
58
  ### Making requests
@@ -104,7 +89,9 @@ url = "https://#{username}:#{token}@api.heroku.com/schema"
104
89
  options = {
105
90
  default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'},
106
91
  cache: Moneta.new(:File, dir: "#{Dir.home}/.heroics/heroku-api")}
107
- cli = Heroics.cli_from_schema_url('heroku-api', STDOUT, url, options)
92
+ data = JSON.parse(File.read('schema.json'))
93
+ schema = Heroics::Schema.new(data)
94
+ cli = Heroics.cli_from_schema('heroku-api', STDOUT, schema, url, options)
108
95
  cli.run(*ARGV)
109
96
  ```
110
97
 
@@ -7,11 +7,15 @@ netrc = Netrc.read
7
7
  username, token = netrc['api.heroku.com']
8
8
  if username && token
9
9
  username = username.split('@').first
10
- url = "https://#{username}:#{token}@api.heroku.com/schema"
11
10
  options = {
12
11
  default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'},
13
12
  cache: Moneta.new(:File, dir: "#{Dir.home}/.heroics/heroku-api")}
14
- cli = Heroics.cli_from_schema_url('heroku-api', STDOUT, url, options)
13
+
14
+ schema_url = "https://#{username}:#{token}@api.heroku.com/schema"
15
+ schema = Heroics.download_schema(schema_url, options)
16
+
17
+ url = "https://#{username}:#{token}@heroku-api.heroku.com/prefix"
18
+ cli = Heroics.cli_from_schema('heroku-api', STDOUT, schema, url, options)
15
19
  cli.run(*ARGV)
16
20
  else
17
21
  puts "ERROR Couldn't find credentials for api.heroku.com in ~/.netrc."
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ['lib']
22
22
 
23
23
  spec.add_development_dependency 'bundler', '~> 1.3'
24
- spec.add_development_dependency 'minitest', '4.7.4'
24
+ spec.add_development_dependency 'minitest', '4.7.5'
25
25
  spec.add_development_dependency 'rake'
26
26
  spec.add_development_dependency 'turn'
27
27
 
@@ -75,7 +75,7 @@ USAGE
75
75
  # is no caching.
76
76
  # @return [CLI] A CLI with commands generated from the JSON schema.
77
77
  def self.cli_from_schema(name, output, schema, url, options={})
78
- client = client_from_schema(schema, URI::join(url, '/').to_s, options)
78
+ client = client_from_schema(schema, url, options)
79
79
  commands = {}
80
80
  schema.resources.each do |resource_schema|
81
81
  resource_schema.links.each do |link_schema|
@@ -85,21 +85,4 @@ USAGE
85
85
  end
86
86
  CLI.new(name, commands, output)
87
87
  end
88
-
89
- # Download a JSON schema and create a CLI with it.
90
- #
91
- # @param name [String] The name of the CLI.
92
- # @param output [IO] The stream to write to.
93
- # @param url [String] The URL for the schema. The URL will be used by the
94
- # generated CLI when it makes requests.
95
- # @param options [Hash] Configuration for links. Possible keys include:
96
- # - default_headers: Optionally, a set of headers to include in every
97
- # request made by the CLI. Default is no custom headers.
98
- # - cache: Optionally, a Moneta-compatible cache to store ETags. Default
99
- # is no caching.
100
- # @return [CLI] A CLI with commands generated from the JSON schema.
101
- def self.cli_from_schema_url(name, output, url, options={})
102
- schema = download_schema(url, options)
103
- cli_from_schema(name, output, schema, url, options)
104
- end
105
88
  end
@@ -16,7 +16,7 @@ module Heroics
16
16
  # @return [Resource] The resource matching the name.
17
17
  def method_missing(name)
18
18
  name = name.to_s.gsub('_', '-')
19
- resource = @resources[name.to_s]
19
+ resource = @resources[name]
20
20
  if resource.nil?
21
21
  # TODO(jkakar) Do we care about resource names in the schema specified
22
22
  # with underscores? If so, we should check to make sure the name
@@ -51,19 +51,4 @@ module Heroics
51
51
  end
52
52
  Client.new(resources)
53
53
  end
54
-
55
- # Download a JSON schema and create an HTTP client with it.
56
- #
57
- # @param url [String] The URL for the schema. The URL will be used by the
58
- # generated client when it makes requests.
59
- # @param options [Hash] Configuration for links. Possible keys include:
60
- # - default_headers: Optionally, a set of headers to include in every
61
- # request made by the client. Default is no custom headers.
62
- # - cache: Optionally, a Moneta-compatible cache to store ETags. Default
63
- # is no caching.
64
- # @return [Client] A client with resources and links from the JSON schema.
65
- def self.client_from_schema_url(url, options={})
66
- schema = download_schema(url, options)
67
- client_from_schema(schema, URI::join(url, '/').to_s, options)
68
- end
69
54
  end
@@ -1,3 +1,3 @@
1
1
  module Heroics
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -234,72 +234,3 @@ class CLIFromSchemaTest < MiniTest::Unit::TestCase
234
234
  assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
235
235
  end
236
236
  end
237
-
238
- class CLIFromSchemaURLTest < MiniTest::Unit::TestCase
239
- include ExconHelper
240
-
241
- # client_from_schema_url downloads a schema and returns a Client generated
242
- # from it.
243
- def test_cli_from_schema_url
244
- Excon.stub(method: :get) do |request|
245
- assert_equal('example.com', request[:host])
246
- assert_equal('/schema', request[:path])
247
- Excon.stubs.pop
248
- {status: 200, headers: {'Content-Type' => 'application/json'},
249
- body: MultiJson.dump(SAMPLE_SCHEMA)}
250
- end
251
-
252
- output = StringIO.new
253
- cli = Heroics.cli_from_schema_url('cli', output,
254
- 'https://example.com/schema')
255
-
256
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
257
- body = {'Hello' => 'World!'}
258
- result = {'Goodbye' => 'Universe!'}
259
- Excon.stub(method: :patch) do |request|
260
- assert_equal("/resource/#{uuid}", request[:path])
261
- assert_equal('application/json', request[:headers]['Content-Type'])
262
- assert_equal(body, MultiJson.load(request[:body]))
263
- Excon.stubs.pop
264
- {status: 200, headers: {'Content-Type' => 'application/json'},
265
- body: MultiJson.dump(result)}
266
- end
267
-
268
- cli.run('resource:update', uuid, body)
269
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
270
- end
271
-
272
- # cli_from_schema_url optionally accepts custom headers to include in the
273
- # request to download the schema. The same headers are passed in requests
274
- # made by the generated CLI.
275
- def test_cli_from_schema_url_with_custom_headers
276
- Excon.stub(method: :get) do |request|
277
- assert_equal('example.com', request[:host])
278
- assert_equal('/schema', request[:path])
279
- assert_equal('application/vnd.heroku+json; version=3',
280
- request[:headers]['Accept'])
281
- Excon.stubs.pop
282
- {status: 200, headers: {'Content-Type' => 'application/json'},
283
- body: MultiJson.dump(SAMPLE_SCHEMA)}
284
- end
285
-
286
- output = StringIO.new
287
- cli = Heroics.cli_from_schema_url(
288
- 'cli', output, 'https://example.com/schema',
289
- default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'})
290
-
291
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
292
- body = {'Hello' => 'World!'}
293
- result = {'Goodbye' => 'Universe!'}
294
- Excon.stub(method: :patch) do |request|
295
- assert_equal('application/vnd.heroku+json; version=3',
296
- request[:headers]['Accept'])
297
- Excon.stubs.pop
298
- {status: 200, headers: {'Content-Type' => 'application/json'},
299
- body: MultiJson.dump(result)}
300
- end
301
-
302
- cli.run('resource:update', uuid, body)
303
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
304
- end
305
- end
@@ -127,71 +127,3 @@ class ClientFromSchemaTest < MiniTest::Unit::TestCase
127
127
  assert_equal(body, client.resource.list)
128
128
  end
129
129
  end
130
-
131
- class ClientFromSchemaURLTest < MiniTest::Unit::TestCase
132
- include ExconHelper
133
-
134
- # client_from_schema_url downloads a schema and returns a Client generated
135
- # from it.
136
- def test_client_from_schema_url
137
- Excon.stub(method: :get) do |request|
138
- assert_equal('example.com', request[:host])
139
- assert_equal('/schema', request[:path])
140
- Excon.stubs.pop
141
- {status: 200, headers: {'Content-Type' => 'application/json'},
142
- body: MultiJson.dump(SAMPLE_SCHEMA)}
143
- end
144
-
145
- client = Heroics::client_from_schema_url('https://example.com/schema')
146
- body = {'Hello' => 'World!'}
147
- Excon.stub(method: :post) do |request|
148
- assert_equal('example.com', request[:host])
149
- assert_equal('/resource', request[:path])
150
- Excon.stubs.pop
151
- {status: 200, headers: {'Content-Type' => 'application/json'},
152
- body: MultiJson.dump(body)}
153
- end
154
- assert_equal(body, client.resource.create)
155
- end
156
-
157
- # client_from_schema_url optionally accepts custom headers to include in the
158
- # request to download the schema. The same headers are passed in requests
159
- # made by the generated client.
160
- def test_client_from_schema_url_with_custom_headers
161
- Excon.stub(method: :get) do |request|
162
- assert_equal('example.com', request[:host])
163
- assert_equal('/schema', request[:path])
164
- assert_equal('application/vnd.heroku+json; version=3',
165
- request[:headers]['Accept'])
166
- Excon.stubs.pop
167
- {status: 200, headers: {'Content-Type' => 'application/json'},
168
- body: MultiJson.dump(SAMPLE_SCHEMA)}
169
- end
170
-
171
- client = Heroics::client_from_schema_url(
172
- 'https://example.com/schema',
173
- default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'})
174
- body = {'Hello' => 'World!'}
175
- Excon.stub(method: :post) do |request|
176
- assert_equal('application/vnd.heroku+json; version=3',
177
- request[:headers]['Accept'])
178
- Excon.stubs.pop
179
- {status: 200, headers: {'Content-Type' => 'application/json'},
180
- body: MultiJson.dump(body)}
181
- end
182
- assert_equal(body, client.resource.create)
183
- end
184
-
185
- # client_from_schema_url raises an Excon error when the request to download
186
- # the schema fails.
187
- def test_client_from_schema_url_with_failed_request
188
- Excon.stub(method: :get) do |request|
189
- Excon.stubs.pop
190
- {status: 404}
191
- end
192
-
193
- assert_raises Excon::Errors::NotFound do
194
- Heroics::client_from_schema_url('https://example.com/schema')
195
- end
196
- end
197
- end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - geemus
@@ -10,12 +9,11 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2014-01-31 00:00:00.000000000 Z
12
+ date: 2014-03-12 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: bundler
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ~>
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,6 @@ dependencies:
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ~>
29
26
  - !ruby/object:Gem::Version
@@ -31,113 +28,99 @@ dependencies:
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: minitest
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
32
  - - '='
37
33
  - !ruby/object:Gem::Version
38
- version: 4.7.4
34
+ version: 4.7.5
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
39
  - - '='
45
40
  - !ruby/object:Gem::Version
46
- version: 4.7.4
41
+ version: 4.7.5
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: rake
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
- - - ! '>='
46
+ - - '>='
53
47
  - !ruby/object:Gem::Version
54
48
  version: '0'
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
- - - ! '>='
53
+ - - '>='
61
54
  - !ruby/object:Gem::Version
62
55
  version: '0'
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: turn
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
- - - ! '>='
60
+ - - '>='
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
63
  type: :development
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
- - - ! '>='
67
+ - - '>='
77
68
  - !ruby/object:Gem::Version
78
69
  version: '0'
79
70
  - !ruby/object:Gem::Dependency
80
71
  name: excon
81
72
  requirement: !ruby/object:Gem::Requirement
82
- none: false
83
73
  requirements:
84
- - - ! '>='
74
+ - - '>='
85
75
  - !ruby/object:Gem::Version
86
76
  version: '0'
87
77
  type: :runtime
88
78
  prerelease: false
89
79
  version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
80
  requirements:
92
- - - ! '>='
81
+ - - '>='
93
82
  - !ruby/object:Gem::Version
94
83
  version: '0'
95
84
  - !ruby/object:Gem::Dependency
96
85
  name: netrc
97
86
  requirement: !ruby/object:Gem::Requirement
98
- none: false
99
87
  requirements:
100
- - - ! '>='
88
+ - - '>='
101
89
  - !ruby/object:Gem::Version
102
90
  version: '0'
103
91
  type: :runtime
104
92
  prerelease: false
105
93
  version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
94
  requirements:
108
- - - ! '>='
95
+ - - '>='
109
96
  - !ruby/object:Gem::Version
110
97
  version: '0'
111
98
  - !ruby/object:Gem::Dependency
112
99
  name: moneta
113
100
  requirement: !ruby/object:Gem::Requirement
114
- none: false
115
101
  requirements:
116
- - - ! '>='
102
+ - - '>='
117
103
  - !ruby/object:Gem::Version
118
104
  version: '0'
119
105
  type: :runtime
120
106
  prerelease: false
121
107
  version_requirements: !ruby/object:Gem::Requirement
122
- none: false
123
108
  requirements:
124
- - - ! '>='
109
+ - - '>='
125
110
  - !ruby/object:Gem::Version
126
111
  version: '0'
127
112
  - !ruby/object:Gem::Dependency
128
113
  name: multi_json
129
114
  requirement: !ruby/object:Gem::Requirement
130
- none: false
131
115
  requirements:
132
- - - ! '>='
116
+ - - '>='
133
117
  - !ruby/object:Gem::Version
134
118
  version: '0'
135
119
  type: :runtime
136
120
  prerelease: false
137
121
  version_requirements: !ruby/object:Gem::Requirement
138
- none: false
139
122
  requirements:
140
- - - ! '>='
123
+ - - '>='
141
124
  - !ruby/object:Gem::Version
142
125
  version: '0'
143
126
  description: A Ruby client for HTTP APIs described using a JSON schema
@@ -181,32 +164,25 @@ files:
181
164
  homepage: ''
182
165
  licenses:
183
166
  - MIT
167
+ metadata: {}
184
168
  post_install_message:
185
169
  rdoc_options: []
186
170
  require_paths:
187
171
  - lib
188
172
  required_ruby_version: !ruby/object:Gem::Requirement
189
- none: false
190
173
  requirements:
191
- - - ! '>='
174
+ - - '>='
192
175
  - !ruby/object:Gem::Version
193
176
  version: '0'
194
- segments:
195
- - 0
196
- hash: 1762076871697657264
197
177
  required_rubygems_version: !ruby/object:Gem::Requirement
198
- none: false
199
178
  requirements:
200
- - - ! '>='
179
+ - - '>='
201
180
  - !ruby/object:Gem::Version
202
181
  version: '0'
203
- segments:
204
- - 0
205
- hash: 1762076871697657264
206
182
  requirements: []
207
183
  rubyforge_project:
208
- rubygems_version: 1.8.23
184
+ rubygems_version: 2.0.14
209
185
  signing_key:
210
- specification_version: 3
186
+ specification_version: 4
211
187
  summary: A Ruby client for HTTP APIs described using a JSON schema
212
188
  test_files: []