Module: HaveAPI::Spec::ApiBuilder
- Defined in:
- lib/haveapi/spec/api_builder.rb
Overview
Contains methods for specification of API to be used in description block.
Instance Method Summary collapse
-
#action_state(backend) ⇒ Object
Set action state backend to mount HaveAPI::Resources::ActionState.
-
#action_state_auth(mode) ⇒ Object
Set action state authentication mode.
-
#api(mod = nil) { ... } ⇒ Object
Set an API module or create the API using DSL.
-
#auth_chain(chain) ⇒ Object
Set authentication chain.
-
#available_locales(locales) ⇒ Object
Set locales available to the API server.
-
#default_locale(locale) ⇒ Object
Set default response locale.
-
#default_version(v) ⇒ Object
Set default API version.
-
#empty_api ⇒ Object
Uses an empty module as the source for the API.
-
#locale(&block) ⇒ Object
Set custom locale resolver.
-
#locale_header(header) ⇒ Object
Set request header used to negotiate the response locale.
-
#login(*credentials) ⇒ Object
Login using HTTP basic.
-
#mount_to(path) ⇒ Object
Set a custom mount path.
- #opt(name, v) ⇒ Object
- #opts ⇒ Object
-
#parameter_i18n_scope(scope) ⇒ Object
Set the translation scope for action parameter labels/descriptions.
-
#use_version(v) ⇒ Object
Select API versions to be used.
-
#validation_error_http_status(status) ⇒ Object
Set HTTP status for action validation errors.
Instance Method Details
#action_state(backend) ⇒ Object
Set action state backend to mount HaveAPI::Resources::ActionState
53 54 55 |
# File 'lib/haveapi/spec/api_builder.rb', line 53 def action_state(backend) opt(:action_state, backend) end |
#action_state_auth(mode) ⇒ Object
Set action state authentication mode.
58 59 60 |
# File 'lib/haveapi/spec/api_builder.rb', line 58 def action_state_auth(mode) opt(:action_state_auth, mode) end |
#api(mod = nil) { ... } ⇒ Object
Set an API module or create the API using DSL.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/haveapi/spec/api_builder.rb', line 14 def api(mod = nil, &block) unless mod mod = Module.new do def self.define_resource(name, superclass: HaveAPI::Resource, &block) return false if const_defined?(name) cls = Class.new(superclass) const_set(name, cls) cls.class_exec(&block) if block cls end module_eval(&block) end const_set(:ApiModule, mod) end opt(:api_module, mod) end |
#auth_chain(chain) ⇒ Object
Set authentication chain.
36 37 38 |
# File 'lib/haveapi/spec/api_builder.rb', line 36 def auth_chain(chain) opt(:auth_chain, chain) end |
#available_locales(locales) ⇒ Object
Set locales available to the API server.
73 74 75 |
# File 'lib/haveapi/spec/api_builder.rb', line 73 def available_locales(locales) opt(:available_locales, locales) end |
#default_locale(locale) ⇒ Object
Set default response locale.
68 69 70 |
# File 'lib/haveapi/spec/api_builder.rb', line 68 def default_locale(locale) opt(:default_locale, locale) end |
#default_version(v) ⇒ Object
Set default API version.
48 49 50 |
# File 'lib/haveapi/spec/api_builder.rb', line 48 def default_version(v) opt(:default_version, v) end |
#empty_api ⇒ Object
Uses an empty module as the source for the API. It will have no resources,
no actions. Version default to 1.
6 7 8 9 |
# File 'lib/haveapi/spec/api_builder.rb', line 6 def empty_api api(Module.new) default_version(1) end |
#locale(&block) ⇒ Object
Set custom locale resolver.
83 84 85 |
# File 'lib/haveapi/spec/api_builder.rb', line 83 def locale(&block) opt(:locale, block) end |
#locale_header(header) ⇒ Object
Set request header used to negotiate the response locale.
78 79 80 |
# File 'lib/haveapi/spec/api_builder.rb', line 78 def locale_header(header) opt(:locale_header, header) end |
#login(*credentials) ⇒ Object
Login using HTTP basic.
98 99 100 101 102 |
# File 'lib/haveapi/spec/api_builder.rb', line 98 def login(*credentials) before do (*credentials) end end |
#mount_to(path) ⇒ Object
Set a custom mount path.
93 94 95 |
# File 'lib/haveapi/spec/api_builder.rb', line 93 def mount_to(path) opt(:mount, path) end |
#opt(name, v) ⇒ Object
110 111 112 113 |
# File 'lib/haveapi/spec/api_builder.rb', line 110 def opt(name, v) @opts ||= {} @opts[name] = v end |
#opts ⇒ Object
105 106 107 |
# File 'lib/haveapi/spec/api_builder.rb', line 105 def opts @opts end |
#parameter_i18n_scope(scope) ⇒ Object
Set the translation scope for action parameter labels/descriptions.
88 89 90 |
# File 'lib/haveapi/spec/api_builder.rb', line 88 def parameter_i18n_scope(scope) opt(:parameter_i18n_scope, scope) end |
#use_version(v) ⇒ Object
Select API versions to be used.
41 42 43 44 45 |
# File 'lib/haveapi/spec/api_builder.rb', line 41 def use_version(v) before do self.class.opt(:versions, v) end end |
#validation_error_http_status(status) ⇒ Object
Set HTTP status for action validation errors. Nil preserves legacy 200.
63 64 65 |
# File 'lib/haveapi/spec/api_builder.rb', line 63 def validation_error_http_status(status) opt(:validation_error_http_status, status) end |