Class: WeaselDiesel
- Inherits:
-
Object
- Object
- WeaselDiesel
- Defined in:
- lib/weasel_diesel.rb,
lib/params.rb,
lib/response.rb,
lib/documentation.rb,
lib/weasel_diesel/cli.rb,
lib/weasel_diesel/dsl.rb,
lib/weasel_diesel/version.rb
Overview
Extending the top level module to add some helpers
Defined Under Namespace
Modules: DSL Classes: CLI, Documentation, Params, Response
Constant Summary collapse
- SERVICE_ROOT_REGEXP =
/(.*?)[\/\(\.]/
- SERVICE_ACTION_REGEXP =
/[\/\(\.]([a-z0-9_]+)[\/\(\.\?]/i
- SERVICE_RESTFUL_SHOW_REGEXP =
/\/:[a-z0-9_]+\.\w{3}$/
- VERSION =
"1.3.0"
Instance Attribute Summary collapse
-
#action ⇒ String
Name of the controller action associated with the service.
-
#auth_required ⇒ Boolean
readonly
Is authentication required?.
-
#controller ⇒ WSController
readonly
Controller instance associated with the service.
-
#controller_name ⇒ String
Name of the controller associated with the service.
-
#defined_params ⇒ Array<WeaselDiesel::Params>
readonly
List of all the service params.
-
#doc ⇒ WeaselDiesel::Documentation
readonly
Documentation instance containing all the service doc.
-
#extra ⇒ Hash
readonly
Extra placeholder to store data in based on developer’s discretion.
-
#name ⇒ String
readonly
Name of the service.
-
#ssl ⇒ Boolean
readonly
Is SSL required?.
-
#url ⇒ String
readonly
Returns the service url.
-
#verb ⇒ Symbol
readonly
The HTTP verb supported.
-
#version ⇒ String
readonly
Service’s version.
Class Method Summary collapse
- .use_controller_dispatch ⇒ Boolean deprecated Deprecated.
- .use_controller_dispatch=(val) ⇒ Boolean deprecated Deprecated.
-
.use_pluralized_controllers ⇒ Boolean
Checks the WeaselDiesel flag to see if the controller names are pluralized.
-
.use_pluralized_controllers=(val) ⇒ Boolean
Sets a WeaselDiesel global flag so all controller names will be automatically pluralized.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare two services using the route loading point.
-
#accept_no_params! ⇒ Nil
Mark the current service as not accepting any params.
- #controller_dispatch(app) ⇒ #to_s deprecated private Deprecated.
-
#disable_auth ⇒ Boolean
Mark that the service doesn’t require authentication.
-
#documentation {|WeaselDiesel::Documentation| ... } ⇒ WeaselDiesel::Documentation
Yields and returns the documentation object.
-
#enable_ssl ⇒ Boolean
Mark that the service requires a SSL connection.
-
#formats(*f_types) ⇒ Array<Symbol>
Sets or returns the supported formats.
-
#http_verb(s_verb = nil) ⇒ String, Symbol
Sets the accepted HTTP verbs or return it if nothing is passed.
-
#implementation(&block) ⇒ Object
Left for generators to implement.
-
#initialize(url) ⇒ WeaselDiesel
constructor
Service constructor which is usually used via Kernel#describe_service.
-
#nested_params ⇒ Array<WeaselDiesel::Params>
Returns an array of namespaced params.
-
#optional_rules ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of optional param rules.
-
#params ⇒ WeaselDiesel::Params
(also: #param)
Returns the defined params for DSL use only! To keep the distinction between the request params and the service params using the
defined_params
accessor is recommended. -
#params? ⇒ Boolean
Returns true if the DSL defined any params.
-
#required_rules ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of required param rules.
-
#response { ... } ⇒ WeaselDiesel::Response
Returns the service response.
-
#route_loading_point ⇒ Integer
Assign a route loading point to compare two routes.
-
#sync_input_param_doc ⇒ Object
private
Takes input param documentation and copy it over to the document object.
Constructor Details
#initialize(url) ⇒ WeaselDiesel
Service constructor which is usually used via Kernel#describe_service
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/weasel_diesel.rb', line 124 def initialize(url) @url = url.start_with?('/') ? url : "/#{url}" @defined_params = WeaselDiesel::Params.new @doc = WeaselDiesel::Documentation.new @response = WeaselDiesel::Response.new # TODO: extract to its own optional lib if WeaselDiesel.use_controller_dispatch @name = extract_service_root_name(url) if WeaselDiesel.use_pluralized_controllers base_name = ExtlibCopy::Inflection.pluralize(ExtlibCopy::Inflection.singular(name)) @controller_name = "#{ExtlibCopy.classify(base_name)}Controller" else @controller_name = "#{ExtlibCopy.classify(name)}Controller" end @action = extract_service_action(url) end # @verb = :get @formats = [] @version = '0.1' @ssl = false @auth_required = true @extra = {} end |
Instance Attribute Details
#action ⇒ String
Name of the controller action associated with the service
86 87 88 |
# File 'lib/weasel_diesel.rb', line 86 def action @action end |
#auth_required ⇒ Boolean (readonly)
Is authentication required?
110 111 112 |
# File 'lib/weasel_diesel.rb', line 110 def auth_required @auth_required end |
#controller ⇒ WSController (readonly)
Controller instance associated with the service
80 81 82 |
# File 'lib/weasel_diesel.rb', line 80 def controller @controller end |
#controller_name ⇒ String
Name of the controller associated with the service
92 93 94 |
# File 'lib/weasel_diesel.rb', line 92 def controller_name @controller_name end |
#defined_params ⇒ Array<WeaselDiesel::Params> (readonly)
List of all the service params
56 57 58 |
# File 'lib/weasel_diesel.rb', line 56 def defined_params @defined_params end |
#doc ⇒ WeaselDiesel::Documentation (readonly)
Documentation instance containing all the service doc
62 63 64 |
# File 'lib/weasel_diesel.rb', line 62 def doc @doc end |
#extra ⇒ Hash (readonly)
Extra placeholder to store data in based on developer’s discretion.
117 118 119 |
# File 'lib/weasel_diesel.rb', line 117 def extra @extra end |
#name ⇒ String (readonly)
Name of the service
98 99 100 |
# File 'lib/weasel_diesel.rb', line 98 def name @name end |
#ssl ⇒ Boolean (readonly)
Is SSL required?
104 105 106 |
# File 'lib/weasel_diesel.rb', line 104 def ssl @ssl end |
#url ⇒ String (readonly)
Returns the service url
50 51 52 |
# File 'lib/weasel_diesel.rb', line 50 def url @url end |
#verb ⇒ Symbol (readonly)
The HTTP verb supported
68 69 70 |
# File 'lib/weasel_diesel.rb', line 68 def verb @verb end |
#version ⇒ String (readonly)
Service’s version
74 75 76 |
# File 'lib/weasel_diesel.rb', line 74 def version @version end |
Class Method Details
.use_controller_dispatch ⇒ Boolean
Checks the WeaselDiesel flag to see if controller are used to dispatch requests. This allows apps to use this DSL but route to controller/actions.
176 177 178 |
# File 'lib/weasel_diesel.rb', line 176 def self.use_controller_dispatch @controller_dispatch end |
.use_controller_dispatch=(val) ⇒ Boolean
Sets a WeaselDiesel global flag so the controller settings can be generated Setting this flag will automatically set the controller/action names.
188 189 190 |
# File 'lib/weasel_diesel.rb', line 188 def self.use_controller_dispatch=(val) @controller_dispatch = val end |
.use_pluralized_controllers ⇒ Boolean
Checks the WeaselDiesel flag to see if the controller names are pluralized.
154 155 156 |
# File 'lib/weasel_diesel.rb', line 154 def self.use_pluralized_controllers @pluralized_controllers ||= false end |
.use_pluralized_controllers=(val) ⇒ Boolean
Sets a WeaselDiesel global flag so all controller names will be automatically pluralized.
165 166 167 |
# File 'lib/weasel_diesel.rb', line 165 def self.use_pluralized_controllers=(val) @pluralized_controllers = val end |
Instance Method Details
#<=>(other) ⇒ Object
Compare two services using the route loading point
360 361 362 |
# File 'lib/weasel_diesel.rb', line 360 def <=> (other) route_loading_point <=> other.route_loading_point end |
#accept_no_params! ⇒ Nil
Mark the current service as not accepting any params. This is purely for expressing the developer’s objective since by default an error is raise if no params are defined and some params are sent.
288 289 290 291 |
# File 'lib/weasel_diesel.rb', line 288 def accept_no_params! # no op operation since this is the default behavior # unless params get defined. Makes sense for documentation tho. end |
#controller_dispatch(app) ⇒ #to_s
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Offers a way to dispatch the service at runtime Basically, it dispatches the request to the defined controller/action The full request cycle looks like that: client -> webserver -> rack -> env -> [service dispatcher] -> controller action -> rack -> webserver -> client
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/weasel_diesel.rb', line 201 def controller_dispatch(app) unless @controller klass = @controller_name.split("::") begin @controller = klass.inject(Object) { |const,k| const.const_get(k) } rescue NameError => e raise "The #{@controller_name} class was not found" end end # We are passing the service object to the controller so the # param verification could be done when the controller gets initialized. @controller.new(app, self).send(@action) end |
#disable_auth ⇒ Boolean
Mark that the service doesn’t require authentication. Note: Authentication is turned on by default
269 270 271 |
# File 'lib/weasel_diesel.rb', line 269 def disable_auth @auth_required = false end |
#documentation {|WeaselDiesel::Documentation| ... } ⇒ WeaselDiesel::Documentation
Yields and returns the documentation object
334 335 336 337 338 339 340 |
# File 'lib/weasel_diesel.rb', line 334 def documentation if block_given? yield(doc) else doc end end |
#enable_ssl ⇒ Boolean
Mark that the service requires a SSL connection
277 278 279 |
# File 'lib/weasel_diesel.rb', line 277 def enable_ssl @ssl = true end |
#formats(*f_types) ⇒ Array<Symbol>
Sets or returns the supported formats
311 312 313 314 |
# File 'lib/weasel_diesel.rb', line 311 def formats(*f_types) f_types.each{|f| @formats << f unless @formats.include?(f) } @formats end |
#http_verb(s_verb = nil) ⇒ String, Symbol
Sets the accepted HTTP verbs or return it if nothing is passed.
320 321 322 323 324 325 326 327 |
# File 'lib/weasel_diesel.rb', line 320 def http_verb(s_verb=nil) return @verb if s_verb.nil? @verb = s_verb.to_sym # Depending on the service settings and url, the service action might need to be updated. # This is how we can support restful routes where a PUT request automatically uses the update method. update_restful_action(@verb) @verb end |
#implementation(&block) ⇒ Object
Left for generators to implement. It’s empty because WD itself isn’t concerned with implementation, but needs it defined so doc generation can read WD web service definitions.
392 393 |
# File 'lib/weasel_diesel.rb', line 392 def implementation(&block) end |
#nested_params ⇒ Array<WeaselDiesel::Params>
Returns an array of namespaced params
260 261 262 |
# File 'lib/weasel_diesel.rb', line 260 def nested_params @defined_params.namespaced_params end |
#optional_rules ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of optional param rules
251 252 253 |
# File 'lib/weasel_diesel.rb', line 251 def optional_rules @defined_params.list_optional end |
#params ⇒ WeaselDiesel::Params Also known as: param
Returns the defined params for DSL use only! To keep the distinction between the request params and the service params using the defined_params
accessor is recommended.
223 224 225 226 227 228 229 |
# File 'lib/weasel_diesel.rb', line 223 def params if block_given? yield(@defined_params) else @defined_params end end |
#params? ⇒ Boolean
Returns true if the DSL defined any params
235 236 237 |
# File 'lib/weasel_diesel.rb', line 235 def params? !(required_rules.empty? && optional_rules.empty? && nested_params.empty?) end |
#required_rules ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of required param rules
243 244 245 |
# File 'lib/weasel_diesel.rb', line 243 def required_rules @defined_params.list_required end |
#response { ... } ⇒ WeaselDiesel::Response
Returns the service response
298 299 300 301 302 303 304 |
# File 'lib/weasel_diesel.rb', line 298 def response if block_given? yield(@response) else @response end end |
#route_loading_point ⇒ Integer
Assign a route loading point to compare two routes. Using this point value, one can load routes with the more globbing routes later than short routes.
347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/weasel_diesel.rb', line 347 def route_loading_point url =~ /(.*?):(.*?)[\/\.](.*)/ return url.size if $1.nil? # The shortest the prepend, the further the service should be loaded prepend = $1.size # The shortest the placeholder, the further it should be in the queue place_holder = $2.size # The shortest the trail, the further it should be in the queue trail = $3.size prepend + place_holder + trail end |
#sync_input_param_doc ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Takes input param documentation and copy it over to the document object. We need to do that so the params can be both documented when a param is defined and in the documentation block.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/weasel_diesel.rb', line 368 def sync_input_param_doc defined_params.namespaced_params.each do |prms| doc.namespace(prms.space_name.name) do |ns| prms.list_optional.each do |rule| ns.param(rule.name, rule.[:doc]) if rule.[:doc] end prms.list_required.each do |rule| ns.param(rule.name, rule.[:doc]) if rule.[:doc] end end end defined_params.list_optional.each do |rule| doc.param(rule.name, rule.[:doc]) if rule.[:doc] end defined_params.list_required.each do |rule| doc.param(rule.name, rule.[:doc]) if rule.[:doc] end end |