komoju 0.0.4 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +13 -10
- data/bin/generate-client +6 -2
- data/komoju.gemspec +2 -6
- data/lib/komoju.rb +1 -0
- data/lib/komoju/client.rb +541 -111
- data/lib/komoju/version.rb +1 -1
- metadata +8 -74
- data/vendor/heroics/lib/heroics.rb +0 -22
- data/vendor/heroics/lib/heroics/cli.rb +0 -88
- data/vendor/heroics/lib/heroics/client.rb +0 -109
- data/vendor/heroics/lib/heroics/client_generator.rb +0 -99
- data/vendor/heroics/lib/heroics/command.rb +0 -67
- data/vendor/heroics/lib/heroics/errors.rb +0 -6
- data/vendor/heroics/lib/heroics/link.rb +0 -120
- data/vendor/heroics/lib/heroics/naming.rb +0 -19
- data/vendor/heroics/lib/heroics/resource.rb +0 -30
- data/vendor/heroics/lib/heroics/schema.rb +0 -444
- data/vendor/heroics/lib/heroics/version.rb +0 -3
- data/vendor/heroics/test.rb +0 -42
- data/vendor/heroics/test/cli_test.rb +0 -236
- data/vendor/heroics/test/client_generator_test.rb +0 -34
- data/vendor/heroics/test/client_test.rb +0 -215
- data/vendor/heroics/test/command_test.rb +0 -214
- data/vendor/heroics/test/helper.rb +0 -204
- data/vendor/heroics/test/link_test.rb +0 -398
- data/vendor/heroics/test/naming_test.rb +0 -45
- data/vendor/heroics/test/resource_test.rb +0 -35
- data/vendor/heroics/test/schema_test.rb +0 -287
- data/vendor/heroics/test/version_test.rb +0 -9
data/lib/komoju/version.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: komoju
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Ramsden
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: heroics
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '2.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '2.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: excon
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
17
|
+
- - '='
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
19
|
+
version: 0.0.21
|
34
20
|
type: :runtime
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
|
-
- -
|
24
|
+
- - '='
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 0.0.21
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: moneta
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,34 +38,6 @@ dependencies:
|
|
52
38
|
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
40
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: multi_json
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 1.9.2
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 1.9.2
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: netrc
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
41
|
- !ruby/object:Gem::Dependency
|
84
42
|
name: bundler
|
85
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -158,28 +116,6 @@ files:
|
|
158
116
|
- lib/komoju/version.rb
|
159
117
|
- test/client_test.rb
|
160
118
|
- test/helper.rb
|
161
|
-
- vendor/heroics/lib/heroics.rb
|
162
|
-
- vendor/heroics/lib/heroics/cli.rb
|
163
|
-
- vendor/heroics/lib/heroics/client.rb
|
164
|
-
- vendor/heroics/lib/heroics/client_generator.rb
|
165
|
-
- vendor/heroics/lib/heroics/command.rb
|
166
|
-
- vendor/heroics/lib/heroics/errors.rb
|
167
|
-
- vendor/heroics/lib/heroics/link.rb
|
168
|
-
- vendor/heroics/lib/heroics/naming.rb
|
169
|
-
- vendor/heroics/lib/heroics/resource.rb
|
170
|
-
- vendor/heroics/lib/heroics/schema.rb
|
171
|
-
- vendor/heroics/lib/heroics/version.rb
|
172
|
-
- vendor/heroics/test.rb
|
173
|
-
- vendor/heroics/test/cli_test.rb
|
174
|
-
- vendor/heroics/test/client_generator_test.rb
|
175
|
-
- vendor/heroics/test/client_test.rb
|
176
|
-
- vendor/heroics/test/command_test.rb
|
177
|
-
- vendor/heroics/test/helper.rb
|
178
|
-
- vendor/heroics/test/link_test.rb
|
179
|
-
- vendor/heroics/test/naming_test.rb
|
180
|
-
- vendor/heroics/test/resource_test.rb
|
181
|
-
- vendor/heroics/test/schema_test.rb
|
182
|
-
- vendor/heroics/test/version_test.rb
|
183
119
|
homepage: https://komoju.com
|
184
120
|
licenses:
|
185
121
|
- MIT
|
@@ -188,7 +124,6 @@ post_install_message:
|
|
188
124
|
rdoc_options: []
|
189
125
|
require_paths:
|
190
126
|
- lib
|
191
|
-
- vendor/heroics/lib
|
192
127
|
required_ruby_version: !ruby/object:Gem::Requirement
|
193
128
|
requirements:
|
194
129
|
- - ">="
|
@@ -201,11 +136,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
136
|
version: '0'
|
202
137
|
requirements: []
|
203
138
|
rubyforge_project:
|
204
|
-
rubygems_version: 2.
|
139
|
+
rubygems_version: 2.5.1
|
205
140
|
signing_key:
|
206
141
|
specification_version: 4
|
207
142
|
summary: Komoju Ruby Bindings
|
208
143
|
test_files:
|
209
144
|
- test/client_test.rb
|
210
145
|
- test/helper.rb
|
211
|
-
has_rdoc:
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'erubis'
|
3
|
-
require 'excon'
|
4
|
-
require 'moneta'
|
5
|
-
require 'multi_json'
|
6
|
-
require 'uri'
|
7
|
-
require 'zlib'
|
8
|
-
|
9
|
-
# Heroics is an HTTP client for an API described by a JSON schema.
|
10
|
-
module Heroics
|
11
|
-
end
|
12
|
-
|
13
|
-
require 'heroics/version'
|
14
|
-
require 'heroics/errors'
|
15
|
-
require 'heroics/naming'
|
16
|
-
require 'heroics/link'
|
17
|
-
require 'heroics/resource'
|
18
|
-
require 'heroics/client'
|
19
|
-
require 'heroics/schema'
|
20
|
-
require 'heroics/command'
|
21
|
-
require 'heroics/cli'
|
22
|
-
require 'heroics/client_generator'
|
@@ -1,88 +0,0 @@
|
|
1
|
-
module Heroics
|
2
|
-
class CLI
|
3
|
-
# Instantiate a CLI for an API described by a JSON schema.
|
4
|
-
#
|
5
|
-
# @param name [String] The name of the CLI.
|
6
|
-
# @param schema [Schema] The JSON schema describing the API.
|
7
|
-
# @param client [Client] A client generated from the JSON schema.
|
8
|
-
# @param output [IO] The stream to write to.
|
9
|
-
def initialize(name, commands, output)
|
10
|
-
@name = name
|
11
|
-
@commands = commands
|
12
|
-
@output = output
|
13
|
-
end
|
14
|
-
|
15
|
-
# Run a command.
|
16
|
-
#
|
17
|
-
# @param parameters [Array] The parameters to use when running the
|
18
|
-
# command. The first parameters is the name of the command and the
|
19
|
-
# remaining parameters are passed to it.
|
20
|
-
def run(*parameters)
|
21
|
-
name = parameters.shift
|
22
|
-
if name.nil? || name == 'help'
|
23
|
-
if command_name = parameters.first
|
24
|
-
command = @commands[command_name]
|
25
|
-
command.usage
|
26
|
-
else
|
27
|
-
usage
|
28
|
-
end
|
29
|
-
else
|
30
|
-
command = @commands[name]
|
31
|
-
if command.nil?
|
32
|
-
@output.write("There is no command called '#{name}'.\n")
|
33
|
-
else
|
34
|
-
command.run(*parameters)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
# Write usage information to the output stream.
|
42
|
-
def usage
|
43
|
-
if @commands.empty?
|
44
|
-
@output.write 'No commands are available.'
|
45
|
-
return
|
46
|
-
end
|
47
|
-
|
48
|
-
@output.write <<-USAGE
|
49
|
-
Usage: #{@name} <command> [<parameter> [...]] [<body>]
|
50
|
-
|
51
|
-
Help topics, type "#{@name} help <topic>" for more details:
|
52
|
-
|
53
|
-
USAGE
|
54
|
-
|
55
|
-
name_width = @commands.keys.max_by { |key| key.size }.size
|
56
|
-
@commands.sort.each do |name, command|
|
57
|
-
name = name.ljust(name_width)
|
58
|
-
description = command.description
|
59
|
-
@output.puts(" #{name} #{description}")
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# Create a CLI from a JSON schema.
|
65
|
-
#
|
66
|
-
# @param name [String] The name of the CLI.
|
67
|
-
# @param output [IO] The stream to write to.
|
68
|
-
# @param schema [Hash] The JSON schema to use with the CLI.
|
69
|
-
# @param url [String] The URL used by the generated CLI when it makes
|
70
|
-
# requests.
|
71
|
-
# @param options [Hash] Configuration for links. Possible keys include:
|
72
|
-
# - default_headers: Optionally, a set of headers to include in every
|
73
|
-
# request made by the CLI. Default is no custom headers.
|
74
|
-
# - cache: Optionally, a Moneta-compatible cache to store ETags. Default
|
75
|
-
# is no caching.
|
76
|
-
# @return [CLI] A CLI with commands generated from the JSON schema.
|
77
|
-
def self.cli_from_schema(name, output, schema, url, options={})
|
78
|
-
client = client_from_schema(schema, url, options)
|
79
|
-
commands = {}
|
80
|
-
schema.resources.each do |resource_schema|
|
81
|
-
resource_schema.links.each do |link_schema|
|
82
|
-
command = Command.new(name, link_schema, client, output)
|
83
|
-
commands[command.name] = command
|
84
|
-
end
|
85
|
-
end
|
86
|
-
CLI.new(name, commands, output)
|
87
|
-
end
|
88
|
-
end
|
@@ -1,109 +0,0 @@
|
|
1
|
-
module Heroics
|
2
|
-
# An HTTP client with methods mapped to API resources.
|
3
|
-
class Client
|
4
|
-
# Instantiate an HTTP client.
|
5
|
-
#
|
6
|
-
# @param resources [Hash<String,Resource>] A hash that maps method names
|
7
|
-
# to resources.
|
8
|
-
# @param url [String] The URL used by this client.
|
9
|
-
def initialize(resources, url)
|
10
|
-
@resources = resources
|
11
|
-
@url = url
|
12
|
-
end
|
13
|
-
|
14
|
-
# Find a resource.
|
15
|
-
#
|
16
|
-
# @param name [String] The name of the resource to find.
|
17
|
-
# @raise [NoMethodError] Raised if the name doesn't match a known resource.
|
18
|
-
# @return [Resource] The resource matching the name.
|
19
|
-
def method_missing(name)
|
20
|
-
resource = @resources[name.to_s]
|
21
|
-
if resource.nil?
|
22
|
-
raise NoMethodError.new("undefined method `#{name}' for #{to_s}")
|
23
|
-
end
|
24
|
-
resource
|
25
|
-
end
|
26
|
-
|
27
|
-
# Get a simple human-readable representation of this client instance.
|
28
|
-
def inspect
|
29
|
-
url = URI.parse(@url)
|
30
|
-
unless url.password.nil?
|
31
|
-
url.password = 'REDACTED'
|
32
|
-
end
|
33
|
-
"#<Heroics::Client url=\"#{url.to_s}\">"
|
34
|
-
end
|
35
|
-
alias to_s inspect
|
36
|
-
end
|
37
|
-
|
38
|
-
# Create an HTTP client from a JSON schema.
|
39
|
-
#
|
40
|
-
# @param schema [Schema] The JSON schema to build an HTTP client for.
|
41
|
-
# @param url [String] The URL the generated client should use when making
|
42
|
-
# requests. Include the username and password to use with HTTP basic
|
43
|
-
# auth.
|
44
|
-
# @param options [Hash] Configuration for links. Possible keys include:
|
45
|
-
# - default_headers: Optionally, a set of headers to include in every
|
46
|
-
# request made by the client. Default is no custom headers.
|
47
|
-
# - cache: Optionally, a Moneta-compatible cache to store ETags. Default
|
48
|
-
# is no caching.
|
49
|
-
# @return [Client] A client with resources and links from the JSON schema.
|
50
|
-
def self.client_from_schema(schema, url, options={})
|
51
|
-
resources = {}
|
52
|
-
schema.resources.each do |resource_schema|
|
53
|
-
links = {}
|
54
|
-
resource_schema.links.each do |link_schema|
|
55
|
-
links[link_schema.name] = Link.new(url, link_schema, options)
|
56
|
-
end
|
57
|
-
resources[resource_schema.name] = Resource.new(links)
|
58
|
-
end
|
59
|
-
Client.new(resources, url)
|
60
|
-
end
|
61
|
-
|
62
|
-
# Create an HTTP client with OAuth credentials from a JSON schema.
|
63
|
-
#
|
64
|
-
# @param oauth_token [String] The OAuth token to pass using the `Bearer`
|
65
|
-
# authorization mechanism.
|
66
|
-
# @param schema [Schema] The JSON schema to build an HTTP client for.
|
67
|
-
# @param url [String] The URL the generated client should use when making
|
68
|
-
# requests.
|
69
|
-
# @param options [Hash] Configuration for links. Possible keys include:
|
70
|
-
# - default_headers: Optionally, a set of headers to include in every
|
71
|
-
# request made by the client. Default is no custom headers.
|
72
|
-
# - cache: Optionally, a Moneta-compatible cache to store ETags. Default
|
73
|
-
# is no caching.
|
74
|
-
# @return [Client] A client with resources and links from the JSON schema.
|
75
|
-
def self.oauth_client_from_schema(oauth_token, schema, url, options={})
|
76
|
-
authorization = "Bearer #{oauth_token}"
|
77
|
-
# Don't mutate user-supplied data.
|
78
|
-
options = Marshal.load(Marshal.dump(options))
|
79
|
-
if !options.has_key?(:default_headers)
|
80
|
-
options[:default_headers] = {}
|
81
|
-
end
|
82
|
-
options[:default_headers].merge!({"Authorization" => authorization})
|
83
|
-
client_from_schema(schema, url, options)
|
84
|
-
end
|
85
|
-
|
86
|
-
# Create an HTTP client with Token credentials from a JSON schema.
|
87
|
-
#
|
88
|
-
# @param oauth_token [String] The token to pass using the `Bearer`
|
89
|
-
# authorization mechanism.
|
90
|
-
# @param schema [Schema] The JSON schema to build an HTTP client for.
|
91
|
-
# @param url [String] The URL the generated client should use when making
|
92
|
-
# requests.
|
93
|
-
# @param options [Hash] Configuration for links. Possible keys include:
|
94
|
-
# - default_headers: Optionally, a set of headers to include in every
|
95
|
-
# request made by the client. Default is no custom headers.
|
96
|
-
# - cache: Optionally, a Moneta-compatible cache to store ETags. Default
|
97
|
-
# is no caching.
|
98
|
-
# @return [Client] A client with resources and links from the JSON schema.
|
99
|
-
def self.token_client_from_schema(token, schema, url, options={})
|
100
|
-
authorization = "Token token=#{token}"
|
101
|
-
# Don't mutate user-supplied data.
|
102
|
-
options = Marshal.load(Marshal.dump(options))
|
103
|
-
if !options.has_key?(:default_headers)
|
104
|
-
options[:default_headers] = {}
|
105
|
-
end
|
106
|
-
options[:default_headers].merge!({"Authorization" => authorization})
|
107
|
-
client_from_schema(schema, url, options)
|
108
|
-
end
|
109
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
module Heroics
|
2
|
-
# Generate a static client that uses Heroics under the hood. This is a good
|
3
|
-
# option if you want to ship a gem or generate API documentation using Yard.
|
4
|
-
#
|
5
|
-
# @param module_name [String] The name of the module, as rendered in a Ruby
|
6
|
-
# source file, to use for the generated client.
|
7
|
-
# @param schema [Schema] The schema instance to generate the client from.
|
8
|
-
# @param url [String] The URL for the API service.
|
9
|
-
# @param options [Hash] Configuration for links. Possible keys include:
|
10
|
-
# - default_headers: Optionally, a set of headers to include in every
|
11
|
-
# request made by the client. Default is no custom headers.
|
12
|
-
# - cache: Optionally, a Moneta-compatible cache to store ETags. Default
|
13
|
-
# is no caching.
|
14
|
-
def self.generate_client(module_name, schema, url, options)
|
15
|
-
filename = File.dirname(__FILE__) + '/views/client.erb'
|
16
|
-
eruby = Erubis::Eruby.new(File.read(filename))
|
17
|
-
context = build_context(module_name, schema, url, options)
|
18
|
-
eruby.evaluate(context)
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
# Process the schema to build up the context needed to render the source
|
24
|
-
# template.
|
25
|
-
def self.build_context(module_name, schema, url, options)
|
26
|
-
resources = []
|
27
|
-
schema.resources.each do |resource_schema|
|
28
|
-
links = []
|
29
|
-
resource_schema.links.each do |link_schema|
|
30
|
-
links << GeneratorLink.new(link_schema.name.gsub('-', '_'),
|
31
|
-
link_schema.description,
|
32
|
-
link_schema.parameter_details)
|
33
|
-
end
|
34
|
-
resources << GeneratorResource.new(resource_schema.name.gsub('-', '_'),
|
35
|
-
resource_schema.description,
|
36
|
-
links)
|
37
|
-
end
|
38
|
-
|
39
|
-
{
|
40
|
-
module_name: module_name,
|
41
|
-
url: url,
|
42
|
-
default_headers: options.fetch(:default_headers, {}),
|
43
|
-
cache: options.fetch(:cache, {}),
|
44
|
-
description: schema.description,
|
45
|
-
schema: MultiJson.dump(schema.schema, pretty:true),
|
46
|
-
resources: resources
|
47
|
-
}
|
48
|
-
end
|
49
|
-
|
50
|
-
# A representation of a resource for use when generating source code in the
|
51
|
-
# template.
|
52
|
-
class GeneratorResource
|
53
|
-
attr_reader :name, :description, :links
|
54
|
-
|
55
|
-
def initialize(name, description, links)
|
56
|
-
@name = name
|
57
|
-
@description = description
|
58
|
-
@links = links
|
59
|
-
end
|
60
|
-
|
61
|
-
# The name of the resource class in generated code.
|
62
|
-
def class_name
|
63
|
-
Heroics.camel_case(name)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# A representation of a link for use when generating source code in the
|
68
|
-
# template.
|
69
|
-
class GeneratorLink
|
70
|
-
attr_reader :name, :description, :parameters
|
71
|
-
|
72
|
-
def initialize(name, description, parameters)
|
73
|
-
@name = name
|
74
|
-
@description = description
|
75
|
-
@parameters = parameters
|
76
|
-
end
|
77
|
-
|
78
|
-
# List of parameters for the method signature
|
79
|
-
def signatures
|
80
|
-
@parameters.map { |info| info.signature }.join(', ')
|
81
|
-
end
|
82
|
-
|
83
|
-
# The list of parameters to render in generated source code for the method
|
84
|
-
# signature for the link.
|
85
|
-
def parameter_list
|
86
|
-
@parameters.map { |info| info.name }.join(', ')
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# Convert a lower_case_name to CamelCase.
|
91
|
-
def self.camel_case(text)
|
92
|
-
return text if text !~ /_/ && text =~ /[A-Z]+.*/
|
93
|
-
text = text.split('_').map{ |element| element.capitalize }.join
|
94
|
-
[/^Ssl/, /^Http/, /^Xml/].each do |replace|
|
95
|
-
text.sub!(replace) { |match| match.upcase }
|
96
|
-
end
|
97
|
-
text
|
98
|
-
end
|
99
|
-
end
|