Class: Inflect::AbstractService

Inherits:
Object
  • Object
show all
Includes:
Comparable, Responsive, ServiceProviderMethods, Singleton
Defined in:
lib/inflect/abstract_service.rb

Overview

Acts as an specification or standard required for a Service Class to be consumed by the application. A Service Class is just a wrapper for any possible service you’d like to give support to, just by having four prerequisites.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ServiceProviderMethods

included

Methods included from Responsive

#respond, #serve

Constructor Details

#initializeAbstractService

Returns a new instance of AbstractService.



25
26
27
28
# File 'lib/inflect/abstract_service.rb', line 25

def initialize
  @priority = 0
  @words    = []
end

Instance Attribute Details

#priorityObject (readonly)

In case there are modules that provide similar contents the one with most priority is picked.



23
24
25
# File 'lib/inflect/abstract_service.rb', line 23

def priority
  @priority
end

#wordsObject (readonly)

A words Array constant with the key words of the Service.

Examples:

Array for New York Times service

words = %W[ NEWS TODAY NEW\ YORK\ TIMES]


19
20
21
# File 'lib/inflect/abstract_service.rb', line 19

def words
  @words
end

Instance Method Details

#<=>(other_service) ⇒ Object

Implement Comparable in order to be sortable.



31
32
33
# File 'lib/inflect/abstract_service.rb', line 31

def <=>(other_service)
  priority <=> other_service.priority
end

#actionsObject

Virtual attributes for the service actions. Every action must have its matching method.



67
68
69
# File 'lib/inflect/abstract_service.rb', line 67

def actions
  words[1..-1]
end

#handle(request) ⇒ Object

Returns a Hash with retrieved data by routing from the request to the method specified by the request’s action attribute.

Parameters:

  • Inflect::Request

Returns:

  • Inflect::Response



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inflect/abstract_service.rb', line 48

def handle(request)
  if action_defined(request.action) && !action_implemented(request.action)
    no_method_error request
  else
    if request.arguments.empty?
      send request.action
    else
      send request.action, request.arguments
    end
  end
end

#keywordObject

Virtual attribute for the services keyword.



61
62
63
# File 'lib/inflect/abstract_service.rb', line 61

def keyword
  words.first
end

#valid?(request) ⇒ Boolean

Receives a Request and returns true if the service can handle the request given.

Parameters:

  • Inflect::Request

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/inflect/abstract_service.rb', line 39

def valid?(request)
  (self.keyword.eql? request.keyword) &&
  action_defined(request.action)
end