heroics 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db15e29f26b3dba405af8b01a48edcad2d2d7acd
4
- data.tar.gz: 3854c7caff8cd64471b6b57b58c2ab807d971a48
3
+ metadata.gz: 179b0e5aaa24933d18882a76ec0959fa652daaf0
4
+ data.tar.gz: cc6aa87e66ff2668dea1564bcc618734f0b4759f
5
5
  SHA512:
6
- metadata.gz: a01a0b5e5f9662efd580ab1f4c869907c017baa43ca5faa0c0179c6593547d58a01fd33b4d285215420ebc01c0819f2ade58153368e0bfb304c9f268de6146f0
7
- data.tar.gz: e47f14886442b6739220abb5fccd561db5e73885daab3b595c034ef6540a21e9265bf6837690f49cb6b964b953c27422c2794e8c84dc4ec1471c1cade5006208
6
+ metadata.gz: e9bc244a410cf64600f6dfe4bf6f7c07e23aed919586383f9a9cc30f8f543beb48c3adee94a535fe6d810fad376677a6269da86512611a2e2c35ea498e208c65
7
+ data.tar.gz: 8be7d88e12c9fe28174e4d1ae9854de0f9f04631025155e6243f2ef8a6dc9c07d02b7783ecba849e359414a34431f2d491b2e6ca7fe3219102414b1199f009f4
@@ -1,8 +1,6 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 1.9.3
5
- - 2.0.0-p648
6
- - 2.1.8
7
- - 2.2.4
8
- - 2.3.0
4
+ - 2.1.10
5
+ - 2.2.6
6
+ - 2.3.3
data/README.md CHANGED
@@ -19,6 +19,31 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
+ ### Configuration File
23
+
24
+ If you don't want to pass config to the CLI, you can provide a Ruby config file to the `heroics-generate` script as a single parameter.
25
+
26
+ The form of this configuration file is shown below.
27
+
28
+ ```ruby
29
+ require 'heroics'
30
+
31
+ Heroics.default_configuration do |config|
32
+ config.base_url = 'https://example.com'
33
+ config.module_name = 'ExampleClient'
34
+ config.schema_filepath = 'schema.json'
35
+
36
+ config.headers = { 'Accept' => 'application/vnd.example+json; version=1' }
37
+ config.cache_path = "#{Dir.home}/.heroics/example"
38
+ end
39
+ ```
40
+
41
+ #### Optional configuration
42
+
43
+ `config.headers` and `config.cache_path` are optional, but all other config shown above is required.
44
+
45
+ For further details on config file usage, see the `example/` directory in this repo.
46
+
22
47
  ### Generating a client
23
48
 
24
49
  Heroics generates an HTTP client from a JSON schema that describes your API.
@@ -27,7 +52,13 @@ JSON schema. When you have a JSON schema prepared you can generate a client
27
52
  for your API:
28
53
 
29
54
  ```
30
- bin/heroics-generate MyApp schema.json https://api.myapp.com > client.rb
55
+ heroics-generate MyApp schema.json https://api.myapp.com > client.rb
56
+ ```
57
+
58
+ If you are using a configuration file, per above, just pass the path to it:
59
+
60
+ ```
61
+ heroics-generate my-config-file.rb > client.rb
31
62
  ```
32
63
 
33
64
  ### Passing custom headers
@@ -6,7 +6,11 @@ require 'open-uri'
6
6
 
7
7
  options = {headers: {}, cache_path: nil}
8
8
  option_parser = OptionParser.new do |opts|
9
- opts.banner = 'Usage: heroics-generate module_name schema_filepath url'
9
+ opts.banner = <<-EOS
10
+ Usage: heroics-generate module_name schema_filepath url
11
+ heroics-generate configuration-file-path.rb
12
+ # Params only allowed when using the first form:
13
+ EOS
10
14
 
11
15
  opts.on('-h', '--help', 'Display this screen') do
12
16
  puts opts
@@ -26,15 +30,32 @@ option_parser = OptionParser.new do |opts|
26
30
  end
27
31
 
28
32
  option_parser.parse!
29
- if ARGV.length != 3
30
- puts option_parser
31
- else
32
- module_name, schema_filepath, url = ARGV
33
- schema = Heroics::Schema.new(MultiJson.decode(open(schema_filepath).read))
34
- cache = 'Moneta.new(:Memory)'
35
- if options[:cache_path]
36
- cache = "Moneta.new(:File, dir: \"#{options[:cache_path]}\")"
33
+
34
+ case ARGV.length
35
+ when 1 # Configuration file passed
36
+ config_file = ARGV[0]
37
+ require config_file
38
+
39
+ puts Heroics.generate_client
40
+ when 3 # Traditional module name, schema and base URL params passed
41
+ module_name, schema_filepath, base_url = ARGV
42
+
43
+ Heroics.default_configuration do |config|
44
+ config.module_name = module_name
45
+ config.schema_filepath = schema_filepath
46
+
47
+ config.base_url = base_url
48
+
49
+ if options[:cache_path]
50
+ config.cache_path = options[:cache_path]
51
+ end
52
+
53
+ if options[:headers]
54
+ config.headers = options[:headers]
55
+ end
37
56
  end
38
- options = {default_headers: options[:headers], cache: cache}
39
- puts Heroics.generate_client(module_name, schema, url, options)
57
+
58
+ puts Heroics.generate_client
59
+ else # incorrect or no CLI params, show help
60
+ puts option_parser
40
61
  end
@@ -15,19 +15,20 @@ Gem::Specification.new do |spec|
15
15
  spec.homepage = 'https://github.com/interagent/heroics'
16
16
  spec.license = 'MIT'
17
17
 
18
- spec.files = `git ls-files`.split($/)
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|example)/}) }
19
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.test_files = spec.files.grep('^(test|spec|features)/')
21
21
  spec.require_paths = ['lib']
22
22
 
23
23
  spec.add_development_dependency 'bundler', '~> 1.3'
24
24
  spec.add_development_dependency 'minitest', '4.7.5'
25
+ spec.add_development_dependency 'moneta'
26
+ spec.add_development_dependency 'netrc'
25
27
  spec.add_development_dependency 'rake'
26
28
  spec.add_development_dependency 'turn'
29
+ spec.add_development_dependency 'yard'
27
30
 
28
31
  spec.add_dependency 'erubis', '~> 2.0'
29
32
  spec.add_dependency 'excon'
30
- spec.add_dependency 'moneta'
31
33
  spec.add_dependency 'multi_json', '>= 1.9.2'
32
- spec.add_dependency 'netrc'
33
34
  end
@@ -2,17 +2,23 @@
2
2
  require 'base64'
3
3
  require 'erubis'
4
4
  require 'excon'
5
- require 'moneta'
6
5
  require 'multi_json'
7
6
  require 'uri'
8
7
  require 'zlib'
9
8
 
10
9
  # Heroics is an HTTP client for an API described by a JSON schema.
11
10
  module Heroics
11
+ extend self
12
+
13
+ def default_configuration(&block)
14
+ block ||= lambda { |c| }
15
+ Heroics::Configuration.defaults.tap(&block)
16
+ end
12
17
  end
13
18
 
14
19
  require 'heroics/version'
15
20
  require 'heroics/errors'
21
+ require 'heroics/configuration'
16
22
  require 'heroics/naming'
17
23
  require 'heroics/link'
18
24
  require 'heroics/resource'
@@ -2,20 +2,13 @@
2
2
  module Heroics
3
3
  # Generate a static client that uses Heroics under the hood. This is a good
4
4
  # option if you want to ship a gem or generate API documentation using Yard.
5
- #
6
- # @param module_name [String] The name of the module, as rendered in a Ruby
7
- # source file, to use for the generated client.
8
- # @param schema [Schema] The schema instance to generate the client from.
9
- # @param url [String] The URL for the API service.
10
- # @param options [Hash] Configuration for links. Possible keys include:
11
- # - default_headers: Optionally, a set of headers to include in every
12
- # request made by the client. Default is no custom headers.
13
- # - cache: Optionally, a Moneta-compatible cache to store ETags. Default
14
- # is no caching.
15
- def self.generate_client(module_name, schema, url, options)
5
+ def self.generate_client
16
6
  filename = File.dirname(__FILE__) + '/views/client.erb'
17
7
  eruby = Erubis::Eruby.new(File.read(filename))
18
- context = build_context(module_name, schema, url, options)
8
+ context = build_context(Heroics::Configuration.defaults.module_name,
9
+ Heroics::Configuration.defaults.schema,
10
+ Heroics::Configuration.defaults.base_url,
11
+ Heroics::Configuration.defaults.options)
19
12
  eruby.evaluate(context)
20
13
  end
21
14
 
@@ -23,7 +16,7 @@ module Heroics
23
16
 
24
17
  # Process the schema to build up the context needed to render the source
25
18
  # template.
26
- def self.build_context(module_name, schema, url, options)
19
+ def self.build_context(module_name, schema, base_url, options)
27
20
  resources = []
28
21
  schema.resources.each do |resource_schema|
29
22
  links = []
@@ -40,7 +33,7 @@ module Heroics
40
33
 
41
34
  {
42
35
  module_name: module_name,
43
- url: url,
36
+ url: base_url,
44
37
  default_headers: options.fetch(:default_headers, {}),
45
38
  cache: options.fetch(:cache, {}),
46
39
  description: schema.description,
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Heroics
4
+ # Attempts to load configuration, provides defaults, and provide helpers to access that data
5
+ class Configuration
6
+ attr_reader :base_url, :cache_path, :module_name, :schema, :options
7
+
8
+ def self.defaults
9
+ @defaults ||= Configuration.new
10
+ end
11
+
12
+ def initialize
13
+ @options = {}
14
+ @options[:cache] = 'Moneta.new(:Memory)'
15
+ @options[:default_headers] = {}
16
+
17
+ yield self if block_given?
18
+ end
19
+
20
+ def schema=(schema)
21
+ @schema = schema
22
+ end
23
+
24
+ def schema_filepath=(schema_filepath)
25
+ @schema = Heroics::Schema.new(MultiJson.decode(open(schema_filepath).read))
26
+ end
27
+
28
+ def module_name=(module_name)
29
+ @module_name = module_name
30
+ end
31
+
32
+ def base_url=(base_url)
33
+ @base_url = base_url
34
+ end
35
+
36
+ def cache_path=(cache_path)
37
+ @options[:cache] = "Moneta.new(:File, dir: \"#{cache_path}\")"
38
+ end
39
+
40
+ def headers=(headers)
41
+ raise "Must provide a hash of headers" unless headers.is_a?(Hash)
42
+ @options[:default_headers] = headers
43
+ end
44
+ end
45
+ end
@@ -17,7 +17,7 @@ module Heroics
17
17
  @root_url, @path_prefix = unpack_url(url)
18
18
  @link_schema = link_schema
19
19
  @default_headers = options[:default_headers] || {}
20
- @cache = options[:cache] || Moneta.new(:Null)
20
+ @cache = options[:cache] || {}
21
21
  end
22
22
 
23
23
  # Make a request to the server.
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Heroics
3
- VERSION = '0.0.17'
3
+ VERSION = '0.0.18'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - geemus
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-06-10 00:00:00.000000000 Z
12
+ date: 2017-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -40,7 +40,7 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  version: 4.7.5
42
42
  - !ruby/object:Gem::Dependency
43
- name: rake
43
+ name: moneta
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
@@ -54,7 +54,7 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: turn
57
+ name: netrc
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
@@ -68,27 +68,27 @@ dependencies:
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
- name: erubis
71
+ name: rake
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - "~>"
74
+ - - ">="
75
75
  - !ruby/object:Gem::Version
76
- version: '2.0'
77
- type: :runtime
76
+ version: '0'
77
+ type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - "~>"
81
+ - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: '2.0'
83
+ version: '0'
84
84
  - !ruby/object:Gem::Dependency
85
- name: excon
85
+ name: turn
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - ">="
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
- type: :runtime
91
+ type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
@@ -96,13 +96,13 @@ dependencies:
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  - !ruby/object:Gem::Dependency
99
- name: moneta
99
+ name: yard
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - ">="
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
- type: :runtime
105
+ type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
@@ -110,21 +110,21 @@ dependencies:
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  - !ruby/object:Gem::Dependency
113
- name: multi_json
113
+ name: erubis
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
- - - ">="
116
+ - - "~>"
117
117
  - !ruby/object:Gem::Version
118
- version: 1.9.2
118
+ version: '2.0'
119
119
  type: :runtime
120
120
  prerelease: false
121
121
  version_requirements: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - ">="
123
+ - - "~>"
124
124
  - !ruby/object:Gem::Version
125
- version: 1.9.2
125
+ version: '2.0'
126
126
  - !ruby/object:Gem::Dependency
127
- name: netrc
127
+ name: excon
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - ">="
@@ -137,6 +137,20 @@ dependencies:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: multi_json
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 1.9.2
147
+ type: :runtime
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: 1.9.2
140
154
  description: A Ruby client generator for HTTP APIs described with a JSON schema
141
155
  email:
142
156
  - geemus@gmail.com
@@ -164,6 +178,7 @@ files:
164
178
  - lib/heroics/client.rb
165
179
  - lib/heroics/client_generator.rb
166
180
  - lib/heroics/command.rb
181
+ - lib/heroics/configuration.rb
167
182
  - lib/heroics/errors.rb
168
183
  - lib/heroics/link.rb
169
184
  - lib/heroics/naming.rb
@@ -172,16 +187,6 @@ files:
172
187
  - lib/heroics/version.rb
173
188
  - lib/heroics/views/client.erb
174
189
  - test.rb
175
- - test/cli_test.rb
176
- - test/client_generator_test.rb
177
- - test/client_test.rb
178
- - test/command_test.rb
179
- - test/helper.rb
180
- - test/link_test.rb
181
- - test/naming_test.rb
182
- - test/resource_test.rb
183
- - test/schema_test.rb
184
- - test/version_test.rb
185
190
  homepage: https://github.com/interagent/heroics
186
191
  licenses:
187
192
  - MIT
@@ -1,237 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'helper'
3
- require 'stringio'
4
-
5
- class CLITest < MiniTest::Unit::TestCase
6
- include ExconHelper
7
-
8
- # CLI.run displays usage information when no arguments are provided.
9
- def test_run_without_arguments
10
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
11
- client = Heroics::client_from_schema(schema, 'https://example.com')
12
- output = StringIO.new
13
- command1 = Heroics::Command.new(
14
- 'cli', schema.resource('resource').link('list'), client, output)
15
- command2 = Heroics::Command.new(
16
- 'cli', schema.resource('resource').link('info'), client, output)
17
- cli = Heroics::CLI.new('cli', {'resource:list' => command1,
18
- 'resource:info' => command2}, output)
19
- cli.run
20
- expected = <<-USAGE
21
- Usage: cli <command> [<parameter> [...]] [<body>]
22
-
23
- Help topics, type "cli help <topic>" for more details:
24
-
25
- resource:info Show a sample resource
26
- resource:list Show all sample resources
27
- USAGE
28
- assert_equal(expected, output.string)
29
- end
30
-
31
- # CLI.run displays usage information when the help command is specified.
32
- def test_run_with_help_command
33
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
34
- client = Heroics::client_from_schema(schema, 'https://example.com')
35
- output = StringIO.new
36
- command1 = Heroics::Command.new(
37
- 'cli', schema.resource('resource').link('list'), client, output)
38
- command2 = Heroics::Command.new(
39
- 'cli', schema.resource('resource').link('info'), client, output)
40
- cli = Heroics::CLI.new('cli', {'resource:list' => command1,
41
- 'resource:info' => command2}, output)
42
- cli.run('help')
43
- expected = <<-USAGE
44
- Usage: cli <command> [<parameter> [...]] [<body>]
45
-
46
- Help topics, type "cli help <topic>" for more details:
47
-
48
- resource:info Show a sample resource
49
- resource:list Show all sample resources
50
- USAGE
51
- assert_equal(expected, output.string)
52
- end
53
-
54
- # CLI.run displays command-specific help when a command name is included
55
- # with the 'help' command.
56
- def test_run_with_help_command_and_explicit_command_name
57
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
58
- client = Heroics::client_from_schema(schema, 'https://example.com')
59
- output = StringIO.new
60
- command1 = Heroics::Command.new(
61
- 'cli', schema.resource('resource').link('list'), client, output)
62
- command2 = Heroics::Command.new(
63
- 'cli', schema.resource('resource').link('info'), client, output)
64
- cli = Heroics::CLI.new('cli', {'resource:list' => command1,
65
- 'resource:info' => command2}, output)
66
- cli.run('help', 'resource:info')
67
- expected = <<-USAGE
68
- Usage: cli resource:info <uuid_field>
69
-
70
- Description:
71
- Show a sample resource
72
- USAGE
73
- assert_equal(expected, output.string)
74
- end
75
-
76
- # CLI.run displays an error message when no commands have been registered.
77
- def test_run_without_commands
78
- output = StringIO.new
79
- cli = Heroics::CLI.new('cli', {}, output)
80
- cli.run('help')
81
- assert_equal('No commands are available.', output.string)
82
- end
83
-
84
- # CLI.run displays an error message when an unknown command name is used.
85
- def test_run_with_unknown_name
86
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
87
- client = Heroics::client_from_schema(schema, 'https://example.com')
88
- output = StringIO.new
89
- command = Heroics::Command.new(
90
- 'cli', schema.resource('resource').link('list'), client, output)
91
- cli = Heroics::CLI.new('cli', {'resource:list' => command}, output)
92
- cli.run('unknown:command')
93
- assert_equal("There is no command called 'unknown:command'.\n",
94
- output.string)
95
- end
96
-
97
- # CLI.run runs the command matching the specified name.
98
- def test_run_with_dashed_command_name
99
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
100
- client = Heroics::client_from_schema(schema, 'https://example.com')
101
- output = StringIO.new
102
- command = Heroics::Command.new(
103
- 'cli', schema.resource('resource').link('identify_resource'), client,
104
- output)
105
- cli = Heroics::CLI.new('cli', {'resource:identify-resource' => command},
106
- output)
107
-
108
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
109
- Excon.stub(method: :get) do |request|
110
- assert_equal("/resource/#{uuid}", request[:path])
111
- Excon.stubs.pop
112
- {status: 200}
113
- end
114
-
115
- cli.run('resource:identify-resource', uuid)
116
- assert_equal('', output.string)
117
- end
118
-
119
- # CLI.run runs the resource matching the specified name.
120
- def test_run_with_dashed_resource_name
121
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
122
- client = Heroics::client_from_schema(schema, 'https://example.com')
123
- output = StringIO.new
124
- command = Heroics::Command.new(
125
- 'cli', schema.resource('another-resource').link('list'), client, output)
126
- cli = Heroics::CLI.new('cli', {'another-resource:list' => command},
127
- output)
128
-
129
- result = {'Hello' => 'World!'}
130
- Excon.stub(method: :get) do |request|
131
- assert_equal("/another-resource", request[:path])
132
- Excon.stubs.pop
133
- {status: 200, headers: {'Content-Type' => 'application/json'},
134
- body: MultiJson.dump(result)}
135
- end
136
-
137
- cli.run('another-resource:list')
138
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
139
- end
140
-
141
- # CLI.run runs the command matching the specified name and passes parameters
142
- # to it.
143
- def test_run_with_parameters
144
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
145
- client = Heroics::client_from_schema(schema, 'https://example.com')
146
- output = StringIO.new
147
- command = Heroics::Command.new(
148
- 'cli', schema.resource('resource').link('update'), client, output)
149
- cli = Heroics::CLI.new('cli', {'resource:update' => command}, output)
150
-
151
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
152
- body = {'Hello' => 'World!'}
153
- result = {'Goodbye' => 'Universe!'}
154
- Excon.stub(method: :patch) do |request|
155
- assert_equal("/resource/#{uuid}", request[:path])
156
- assert_equal('application/json', request[:headers]['Content-Type'])
157
- assert_equal(body, MultiJson.load(request[:body]))
158
- Excon.stubs.pop
159
- {status: 200, headers: {'Content-Type' => 'application/json'},
160
- body: MultiJson.dump(result)}
161
- end
162
-
163
- cli.run('resource:update', uuid, body)
164
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
165
- end
166
- end
167
-
168
- class CLIFromSchemaTest < MiniTest::Unit::TestCase
169
- include ExconHelper
170
-
171
- # cli_from_schema returns a CLI generated from the specified schema.
172
- def test_cli_from_schema
173
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
174
- body = {'Hello' => 'World!'}
175
- result = {'Goodbye' => 'Universe!'}
176
- Excon.stub(method: :patch) do |request|
177
- assert_equal("/resource/#{uuid}", request[:path])
178
- assert_equal('application/json', request[:headers]['Content-Type'])
179
- assert_equal(body, MultiJson.load(request[:body]))
180
- Excon.stubs.pop
181
- {status: 200, headers: {'Content-Type' => 'application/json'},
182
- body: MultiJson.dump(result)}
183
- end
184
-
185
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
186
- output = StringIO.new
187
- cli = Heroics.cli_from_schema('cli', output, schema, 'https://example.com')
188
- cli.run('resource:update', uuid, body)
189
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
190
- end
191
-
192
- # cli_from_schema returns a CLI that can make requests to APIs mounted under
193
- # a prefix, such as http://example.com/api, for example.
194
- def test_client_from_schema_with_url_prefix
195
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
196
- body = {'Hello' => 'World!'}
197
- result = {'Goodbye' => 'Universe!'}
198
- Excon.stub(method: :patch) do |request|
199
- assert_equal("/api/resource/#{uuid}", request[:path])
200
- assert_equal('application/json', request[:headers]['Content-Type'])
201
- assert_equal(body, MultiJson.load(request[:body]))
202
- Excon.stubs.pop
203
- {status: 200, headers: {'Content-Type' => 'application/json'},
204
- body: MultiJson.dump(result)}
205
- end
206
-
207
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
208
- output = StringIO.new
209
- cli = Heroics.cli_from_schema('cli', output, schema,
210
- 'https://example.com/api')
211
- cli.run('resource:update', uuid, body)
212
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
213
- end
214
-
215
- # cli_from_schema optionally accepts custom headers to pass with every
216
- # request made by the generated CLI.
217
- def test_cli_from_schema_with_custom_headers
218
- uuid = '1ab1c589-df46-40aa-b786-60e83b1efb10'
219
- body = {'Hello' => 'World!'}
220
- result = {'Goodbye' => 'Universe!'}
221
- Excon.stub(method: :patch) do |request|
222
- assert_equal('application/vnd.heroku+json; version=3',
223
- request[:headers]['Accept'])
224
- Excon.stubs.pop
225
- {status: 200, headers: {'Content-Type' => 'application/json'},
226
- body: MultiJson.dump(result)}
227
- end
228
-
229
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
230
- output = StringIO.new
231
- cli = Heroics.cli_from_schema(
232
- 'cli', output, schema, 'https://example.com',
233
- default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'})
234
- cli.run('resource:update', uuid, body)
235
- assert_equal(MultiJson.dump(result, pretty: true) + "\n", output.string)
236
- end
237
- end