Module: Heroics
- Extended by:
- Heroics
- Included in:
- Heroics
- Defined in:
- lib/heroics.rb,
lib/heroics/cli.rb,
lib/heroics/link.rb,
lib/heroics/client.rb,
lib/heroics/errors.rb,
lib/heroics/naming.rb,
lib/heroics/schema.rb,
lib/heroics/command.rb,
lib/heroics/version.rb,
lib/heroics/resource.rb,
lib/heroics/configuration.rb,
lib/heroics/client_generator.rb
Overview
Heroics is an HTTP client for an API described by a JSON schema.
Defined Under Namespace
Modules: NullRateLimit Classes: BodyParameter, CLI, Client, Command, Configuration, GeneratorLink, GeneratorResource, Link, LinkSchema, Parameter, ParameterChoice, Resource, ResourceSchema, Schema, SchemaError
Constant Summary collapse
- VERSION =
'0.1.2'
Class Method Summary collapse
-
.cli_from_schema(name, output, schema, url, options = {}) ⇒ CLI
Create a CLI from a JSON schema.
-
.client_from_schema(schema, url, options = {}) ⇒ Client
Create an HTTP client from a JSON schema.
-
.download_schema(url, options = {}) ⇒ Schema
Download a JSON schema from a URL.
-
.generate_client ⇒ Object
Generate a static client that uses Heroics under the hood.
-
.oauth_client_from_schema(oauth_token, schema, url, options = {}) ⇒ Client
Create an HTTP client with OAuth credentials from a JSON schema.
-
.pretty_name(name) ⇒ String
Process a name to make it suitable for use as a pretty command name.
-
.ruby_name(name) ⇒ String
Process a name to make it suitable for use as a Ruby method name.
-
.token_client_from_schema(token, schema, url, options = {}) ⇒ Client
Create an HTTP client with Token credentials from a JSON schema.
Instance Method Summary collapse
Class Method Details
.cli_from_schema(name, output, schema, url, options = {}) ⇒ CLI
Create a CLI from a JSON schema.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/heroics/cli.rb', line 78 def self.cli_from_schema(name, output, schema, url, ={}) client = client_from_schema(schema, url, ) commands = {} schema.resources.each do |resource_schema| resource_schema.links.each do |link_schema| command = Command.new(name, link_schema, client, output) commands[command.name] = command end end CLI.new(name, commands, output) end |
.client_from_schema(schema, url, options = {}) ⇒ Client
Create an HTTP client from a JSON schema.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/heroics/client.rb', line 59 def self.client_from_schema(schema, url, ={}) resources = {} schema.resources.each do |resource_schema| links = {} resource_schema.links.each do |link_schema| links[link_schema.name] = Link.new(url, link_schema, ) end resources[resource_schema.name] = Resource.new(links) end Client.new(resources, url) end |
.download_schema(url, options = {}) ⇒ Schema
Download a JSON schema from a URL.
356 357 358 359 360 |
# File 'lib/heroics/schema.rb', line 356 def self.download_schema(url, ={}) default_headers = .fetch(:default_headers, {}) response = Excon.get(url, headers: default_headers, expects: [200, 201]) Schema.new(MultiJson.load(response.body)) end |
.generate_client ⇒ Object
Generate a static client that uses Heroics under the hood. This is a good option if you want to ship a gem or generate API documentation using Yard.
5 6 7 8 9 10 11 12 13 |
# File 'lib/heroics/client_generator.rb', line 5 def self.generate_client filename = File.dirname(__FILE__) + '/views/client.erb' eruby = Erubis::Eruby.new(File.read(filename)) context = build_context(Heroics::Configuration.defaults.module_name, Heroics::Configuration.defaults.schema, Heroics::Configuration.defaults.base_url, Heroics::Configuration.defaults.) eruby.evaluate(context) end |
.oauth_client_from_schema(oauth_token, schema, url, options = {}) ⇒ Client
Create an HTTP client with OAuth credentials from a JSON schema.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/heroics/client.rb', line 84 def self.oauth_client_from_schema(oauth_token, schema, url, ={}) = "Bearer #{oauth_token}" # Don't mutate user-supplied data. = Marshal.load(Marshal.dump()) if !.has_key?(:default_headers) [:default_headers] = {} end [:default_headers].merge!({"Authorization" => }) client_from_schema(schema, url, ) end |
.pretty_name(name) ⇒ String
Process a name to make it suitable for use as a pretty command name.
26 27 28 |
# File 'lib/heroics/naming.rb', line 26 def self.pretty_name(name) name.downcase.gsub(/[_ ]/, '-') end |
.ruby_name(name) ⇒ String
Process a name to make it suitable for use as a Ruby method name.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/heroics/naming.rb', line 10 def self.ruby_name(name) patterns = Heroics::Configuration.defaults.ruby_name_replacement_patterns ruby_name = patterns.reduce(name.downcase) do |memo, (regex, replacement)| memo.gsub(regex, replacement) end raise SchemaError.new("Name '#{name}' converts to invalid Ruby name '#{ruby_name}'.") if ruby_name =~ /\W/ ruby_name end |
.token_client_from_schema(token, schema, url, options = {}) ⇒ Client
Create an HTTP client with Token credentials from a JSON schema.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/heroics/client.rb', line 108 def self.token_client_from_schema(token, schema, url, ={}) = "Token token=#{token}" # Don't mutate user-supplied data. = Marshal.load(Marshal.dump()) if !.has_key?(:default_headers) [:default_headers] = {} end [:default_headers].merge!({"Authorization" => }) client_from_schema(schema, url, ) end |
Instance Method Details
#default_configuration(&block) ⇒ Object
14 15 16 17 |
# File 'lib/heroics.rb', line 14 def default_configuration(&block) block ||= lambda { |c| } Heroics::Configuration.defaults.tap(&block) end |