Class: WeaselDiesel

Inherits:
Object
  • Object
show all
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 =

Since:

  • 0.0.3

/(.*?)[\/\(\.]/
SERVICE_ACTION_REGEXP =

Since:

  • 0.0.3

/[\/\(\.]([a-z0-9_]+)[\/\(\.\?]/i
SERVICE_RESTFUL_SHOW_REGEXP =

Since:

  • 0.0.3

/\/:[a-z0-9_]+\.\w{3}$/
VERSION =
"1.3.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ WeaselDiesel

Service constructor which is usually used via Kernel#describe_service

Parameters:

  • url (String)

    Service’s url ( the url will automatically be prepended a slash if it doesn’t already contain one.

See Also:

  • See how this class is usually initialized using `describe_service`

Since:

  • 0.0.3



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

#actionString

Name of the controller action associated with the service

Returns:

  • (String)

Since:

  • 0.0.3



86
87
88
# File 'lib/weasel_diesel.rb', line 86

def action
  @action
end

#auth_requiredBoolean (readonly)

Is authentication required?

Returns:

  • (Boolean)

Since:

  • 0.0.3



110
111
112
# File 'lib/weasel_diesel.rb', line 110

def auth_required
  @auth_required
end

#controllerWSController (readonly)

Controller instance associated with the service

Returns:

  • (WSController)

Since:

  • 0.0.3



80
81
82
# File 'lib/weasel_diesel.rb', line 80

def controller
  @controller
end

#controller_nameString

Name of the controller associated with the service

Returns:

  • (String)

Since:

  • 0.0.3



92
93
94
# File 'lib/weasel_diesel.rb', line 92

def controller_name
  @controller_name
end

#defined_paramsArray<WeaselDiesel::Params> (readonly)

List of all the service params

Returns:

Since:

  • 0.0.3



56
57
58
# File 'lib/weasel_diesel.rb', line 56

def defined_params
  @defined_params
end

#docWeaselDiesel::Documentation (readonly)

Documentation instance containing all the service doc

Returns:

Since:

  • 0.0.3



62
63
64
# File 'lib/weasel_diesel.rb', line 62

def doc
  @doc
end

#extraHash (readonly)

Extra placeholder to store data in based on developer’s discretion.

Returns:

  • (Hash)

    A hash storing extra data based.

Since:

  • 0.1



117
118
119
# File 'lib/weasel_diesel.rb', line 117

def extra
  @extra
end

#nameString (readonly)

Name of the service

Returns:

  • (String)

Since:

  • 0.0.3



98
99
100
# File 'lib/weasel_diesel.rb', line 98

def name
  @name
end

#sslBoolean (readonly)

Is SSL required?

Returns:

  • (Boolean)

Since:

  • 0.0.3



104
105
106
# File 'lib/weasel_diesel.rb', line 104

def ssl
  @ssl
end

#urlString (readonly)

Returns the service url

Returns:

  • (String)

    The service url

Since:

  • 0.0.3



50
51
52
# File 'lib/weasel_diesel.rb', line 50

def url
  @url
end

#verbSymbol (readonly)

The HTTP verb supported

Returns:

  • (Symbol)

Since:

  • 0.0.3



68
69
70
# File 'lib/weasel_diesel.rb', line 68

def verb
  @verb
end

#versionString (readonly)

Service’s version

Returns:

  • (String)

Since:

  • 0.0.3



74
75
76
# File 'lib/weasel_diesel.rb', line 74

def version
  @version
end

Class Method Details

.use_controller_dispatchBoolean

Deprecated.

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.

Returns:

  • (Boolean)

    The updated value, default to false

Since:

  • 0.3.0



176
177
178
# File 'lib/weasel_diesel.rb', line 176

def self.use_controller_dispatch
  @controller_dispatch
end

.use_controller_dispatch=(val) ⇒ Boolean

Deprecated.

Sets a WeaselDiesel global flag so the controller settings can be generated Setting this flag will automatically set the controller/action names.

Parameters:

  • True (Boolean)

    if the controllers are pluralized, False otherwise.

Returns:

  • (Boolean)

    The updated value

Since:

  • 0.1.1



188
189
190
# File 'lib/weasel_diesel.rb', line 188

def self.use_controller_dispatch=(val)
  @controller_dispatch = val
end

.use_pluralized_controllersBoolean

Checks the WeaselDiesel flag to see if the controller names are pluralized.

Returns:

  • (Boolean)

    The updated value, default to false

Since:

  • 0.1.1



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.

Parameters:

  • True (Boolean)

    if the controllers are pluralized, False otherwise.

Returns:

  • (Boolean)

    The updated value

Since:

  • 0.1.1



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

Since:

  • 0.0.3



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.

Returns:

  • (Nil)

Since:

  • 0.0.3



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.

Deprecated.

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

Parameters:

  • app (Object)

    Reference object such as a Sinatra::Application to be passed to the controller.

Returns:

  • (#to_s)

    The response from the controller action

Since:

  • 0.0.3



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_authBoolean

Mark that the service doesn’t require authentication. Note: Authentication is turned on by default

Returns:

  • (Boolean)

Since:

  • 0.0.3



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

Yields:

Returns:

Since:

  • 0.0.3



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_sslBoolean

Mark that the service requires a SSL connection

Returns:

  • (Boolean)

Since:

  • 0.0.3



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

Parameters:

  • f_types (String, Symbol)

    Format type supported, such as :xml

Returns:

  • (Array<Symbol>)

    List of supported formats

Since:

  • 0.0.3



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.

Returns:

  • (String, Symbol)

Since:

  • 0.0.3



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.

Since:

  • 0.0.3



392
393
# File 'lib/weasel_diesel.rb', line 392

def implementation(&block)
end

#nested_paramsArray<WeaselDiesel::Params>

Returns an array of namespaced params

Returns:

See Also:

Since:

  • 0.0.3



260
261
262
# File 'lib/weasel_diesel.rb', line 260

def nested_params
  @defined_params.namespaced_params
end

#optional_rulesArray<WeaselDiesel::Params::Rule>

Returns an array of optional param rules

Returns:

Since:

  • 0.0.3



251
252
253
# File 'lib/weasel_diesel.rb', line 251

def optional_rules
  @defined_params.list_optional
end

#paramsWeaselDiesel::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.

Returns:

See Also:

Since:

  • 0.0.3



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

Returns:

  • (Boolean)

Since:

  • 0.0.3



235
236
237
# File 'lib/weasel_diesel.rb', line 235

def params?
  !(required_rules.empty? && optional_rules.empty? && nested_params.empty?)
end

#required_rulesArray<WeaselDiesel::Params::Rule>

Returns an array of required param rules

Returns:

Since:

  • 0.0.3



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

Yields:

  • The service response object

Returns:

Since:

  • 0.0.3



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_pointInteger

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.

Returns:

  • (Integer)

    point value

Since:

  • 0.0.3



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_docObject

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.

Since:

  • 0.0.3



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.options[:doc]) if rule.options[:doc]
      end
      prms.list_required.each do |rule|
        ns.param(rule.name, rule.options[:doc]) if rule.options[:doc]
      end
    end
  end

  defined_params.list_optional.each do |rule|
    doc.param(rule.name, rule.options[:doc]) if rule.options[:doc]
  end

  defined_params.list_required.each do |rule|
    doc.param(rule.name, rule.options[:doc]) if rule.options[:doc]
  end
end