inflect 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8951661906e8fea2c53b241c2ed44d49d7f4155f
4
- data.tar.gz: e685535c15abc2f5f753f9c1ea42ed8d372edf89
3
+ metadata.gz: 0540c58850a8378d104c597d1082d334f153f907
4
+ data.tar.gz: 712d8e922f27c6f994dec37ea4542f69976002a7
5
5
  SHA512:
6
- metadata.gz: 25ef3d073b0ab588ac592a502767345cc8dfb4a8454f777d98aa210bd78d59d2cbcd8004f9ff06cc814f513122f76b2372d2d054c1229231ac6b0fc99ea7e4dd
7
- data.tar.gz: 8f181a6082230a3bf07145320597429314e6387dde91254603f7f9d441e0f547b6871155ddc01027ae7b1c4187d42e83d53a9adb75d5770bd0fad369f72349a6
6
+ metadata.gz: d5f79a0bd635f982577ae422d231b8a6af308fa736642522b7d7eae91114128a24d17505ffbfc698e3ee2c253db08f9cc291f8f094fe724c563289197b76046d
7
+ data.tar.gz: c7ef59e1152a2660d74047ca0e28919e9e113287e6ebe1d609089f8452db669e2e09c115d94c1639e586b8e07e7d193d6dc575414b90b5b940f0be3e245e74ab
data/README.md CHANGED
@@ -30,7 +30,7 @@ Generate your Service:
30
30
 
31
31
  $ inflect generate:service Weather
32
32
 
33
- IMPORTANT: service name must be in CamelCase, example:
33
+ IMPORTANT: service name must be in CamelCase, example:
34
34
 
35
35
  $ inflect generate:service MyWeatherService
36
36
 
@@ -43,8 +43,20 @@ class WeatherService < Inflect::AbstractService
43
43
  @words = %W[WEATHER TODAY]
44
44
  end
45
45
 
46
- def handle(words)
47
- respond "Weather for today is... very hot."
46
+ # This method will be executed whenever the action is not defined in the
47
+ # words query array.
48
+ def default
49
+ respond "This is the default weather cast"
50
+ end
51
+
52
+ # This is method will be called when the action TODAY is present in
53
+ # the words query array. It can optionally carry necessary arguments.
54
+ def today(location = nil)
55
+ if location.nil?
56
+ respond "Weather for today is... very hot."
57
+ else
58
+ respond "The weather for #{location} is... also very hot."
59
+ end
48
60
  end
49
61
  end
50
62
  ```
@@ -9,7 +9,7 @@ class ServiceGenerator < Thor::Group
9
9
  # Include Inflector for string management utils such
10
10
  # as camelize.
11
11
  using Inflect::Inflector
12
-
12
+
13
13
  # Defines the templates path.
14
14
  def self.source_root
15
15
  File.join(File.dirname(__FILE__), 'templates')
@@ -1,20 +1,27 @@
1
1
  class <%= name %>Service < Inflect::AbstractService
2
- # A WORDS Array constant with the key words of the Service.
2
+ # A WORDS Array constant with the key words of the Service,
3
+ # the first word of the array represents the keyword for the
4
+ # service, the second represents the action (method), and all the rest are
5
+ # the arguments to that action.
3
6
  # @example Array for New York Times service
4
- # words = %W[ NEWS TODAY NEW\ YORK\ TIMES]
7
+ # words = %W[NEWS TODAY NEW\ YORK\ TIMES]
8
+ #
9
+ # This would mean that the service would handle request with the
10
+ # keyword NEWS, in it's #today method with the arguments "new york times".
5
11
 
6
12
  # In case there are modules that provide similar contents the
7
13
  # one with most PRIORITY is picked.
8
14
  # Float::Infinity is the lowest priority.
9
15
  def initialize
10
16
  @priority = Float::INFINITY
11
- @words = %W[]
17
+ @words = %W[].freeze
12
18
  end
13
19
 
14
- # This is method is the only one needed for Inflect to work.
15
- # Implements how the service responds at the translated words.
16
- # Returns a Inflect::Response with retrieved data.
17
- def handle(words)
20
+ # This method is the only one needed for Inflect to work.
21
+ # Implements how the service responds to a request that has no
22
+ # action attribute.
23
+ # @return Inflect::Response
24
+ def default
18
25
  super
19
26
  end
20
27
  end
@@ -30,21 +30,68 @@ module Inflect
30
30
  priority <=> other_service.priority
31
31
  end
32
32
 
33
- # Receives an Array of words and returns true or false depending
34
- # if the Service can handle the request given by the words.
35
- #
36
- # @param words [Array] an Array of strings with key words.
33
+ # Receives a Request and returns true if the service can handle
34
+ # the request given.
35
+ # @param Inflect::Request
37
36
  # @return [Boolean]
38
- def valid?(words)
39
- (@words & words).any?
37
+ def valid?(request)
38
+ (self.keyword.eql? request.keyword) &&
39
+ action_defined(request.action)
40
40
  end
41
41
 
42
- # Returns a Hash with retrieved data.
43
- #
44
- # @param words [Array] an Array of strings with key words.
45
- def handle(words)
46
- message = "#{self.class} must implement handle method,
47
- for more information see AbstractService class."
42
+ # The +default+ is the method called when there is no action parameter inside
43
+ # the words query array.
44
+ def default
45
+ no_method_error
46
+ end
47
+
48
+ # Returns a Hash with retrieved data by routing from the request
49
+ # to the method specified by the request's action attribute.
50
+ # @param Inflect::Request
51
+ # @return Inflect::Response
52
+ def handle(request)
53
+ if action_defined(request.action) && !action_implemented(request.action)
54
+ no_method_error
55
+ else
56
+ if request.arguments.empty?
57
+ send request.action
58
+ else
59
+ send request.action, request.arguments
60
+ end
61
+ end
62
+ end
63
+
64
+ # Virtual attribute for the services keyword.
65
+ def keyword
66
+ words.first
67
+ end
68
+
69
+ # Virtual attributes for the service actions. Every action must
70
+ # have its matching method.
71
+ def actions
72
+ words[1..-1]
73
+ end
74
+
75
+ private
76
+
77
+ # Check if user defined the given action besides from the default_options
78
+ # action that is always present.
79
+ def action_defined(action)
80
+ (action.eql? :default) || (actions.include? action.to_s.upcase)
81
+ end
82
+
83
+ # Check if user really implemented the method corresponding to
84
+ # the action.
85
+ def action_implemented(action)
86
+ respond_to? action
87
+ end
88
+
89
+ # Method fired when users don't implement the method corresponding
90
+ # to the actions they define in the +words+ array.
91
+ def no_method_error
92
+ message = "#{self.class} declared the action #{request.action}
93
+ in #{self.words} but the method is missing, for more
94
+ information see Inflect::AbstractService class."
48
95
  raise NoMethodError.new message
49
96
  end
50
97
  end
@@ -1,5 +1,6 @@
1
1
  require 'inflect/loader'
2
2
  require 'inflect/configuration'
3
+ require 'inflect/request'
3
4
 
4
5
  module Inflect
5
6
  # The class in charge of managing the access
@@ -20,19 +21,20 @@ module Inflect
20
21
  # the work.
21
22
  # @param words [Array<String, Symbol>]
22
23
  def handle(words)
23
- selected_service = select_service(words)
24
+ request = Inflect::Request.new(words)
25
+ selected_service = select_service(request)
24
26
  if selected_service.nil?
25
- raise "No service can respond to #{words.first}"
27
+ raise "No service can respond to #{request.keyword}"
26
28
  else
27
- selected_service.serve(words)
29
+ selected_service.serve(request)
28
30
  end
29
31
  end
30
32
 
31
33
  private
32
34
 
33
- def select_service(words)
35
+ def select_service(request)
34
36
  services.find do |service|
35
- service.valid?(words)
37
+ service.valid? request
36
38
  end
37
39
  end
38
40
  end
@@ -0,0 +1,22 @@
1
+ module Inflect
2
+ # Class that parses the incoming data and builds the semantics
3
+ # around the request.
4
+ class Request
5
+ attr_reader :keyword, :action, :arguments, :query_words
6
+ #
7
+ # The request is built from an array of words.
8
+ # Depending on the size the arguments and the action are built in a different
9
+ # ways.
10
+ # @example
11
+ # %W[WEATHER] -> {service_key: weather, action: 'default'}
12
+ # %W[WEATHER TODAY] -> {service_key: weather, action: 'today'}
13
+ # %W[WEATHER TODAY BUENOS\ AIRES] -> {service_key: weather, action: 'today',
14
+ # args: ['Buenos Aires']}
15
+ def initialize(words)
16
+ @query_words = words.dup
17
+ @keyword = words.shift
18
+ @action = words.first.nil? ? :default : words.shift.downcase.to_sym
19
+ @arguments = words.map(&:downcase)
20
+ end
21
+ end
22
+ end
@@ -6,10 +6,10 @@ module Inflect
6
6
  # Method that creates Response instance.
7
7
  # @param words [Array<String>] The queried words.
8
8
  # @return [Inflect::Response | nil] Returns nil if response is not valid.
9
- def serve(words)
10
- content, options = handle(words)
11
- opts = merge_options(options, { query_words: words })
12
- response = validate_response(Inflect::Response.new(content, opts))
9
+ def serve(request)
10
+ content, options = handle(request)
11
+ opts = merge_options(options, { query_words: request.query_words })
12
+ validate_response(Inflect::Response.new(content, opts))
13
13
  end
14
14
 
15
15
  # Supply more expressiveness and flexibility to the interface
@@ -1,3 +1,3 @@
1
1
  module Inflect
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,27 @@
1
+ class RandomService < Inflect::AbstractService
2
+ # A WORDS Array constant with the key words of the Service,
3
+ # the first word of the array represents the keyword for the
4
+ # service, the second represents the action (method), and all the rest are
5
+ # the arguments to that action.
6
+ # @example Array for New York Times service
7
+ # words = %W[NEWS TODAY NEW\ YORK\ TIMES]
8
+ #
9
+ # This would mean that the service would handle request with the
10
+ # keyword NEWS, in it's #today method with the arguments "new york times".
11
+
12
+ # In case there are modules that provide similar contents the
13
+ # one with most PRIORITY is picked.
14
+ # Float::Infinity is the lowest priority.
15
+ def initialize
16
+ @priority = 1
17
+ @words = %W[RANDOM].freeze
18
+ end
19
+
20
+ # This method is the only one needed for Inflect to work.
21
+ # Implements how the service responds to a request that has no
22
+ # action attribute.
23
+ # @return Inflect::Response
24
+ def default
25
+ super
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inflect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Santiago Figueiras
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-09 00:00:00.000000000 Z
12
+ date: 2016-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -110,9 +110,11 @@ files:
110
110
  - lib/inflect/inflector.rb
111
111
  - lib/inflect/loader.rb
112
112
  - lib/inflect/locale/en.yml
113
+ - lib/inflect/request.rb
113
114
  - lib/inflect/response.rb
114
115
  - lib/inflect/responsive.rb
115
116
  - lib/inflect/version.rb
117
+ - lib/services/random_service.rb
116
118
  homepage: https://github.com/InflectProject/inflect
117
119
  licenses:
118
120
  - MIT
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  version: '0'
135
137
  requirements: []
136
138
  rubyforge_project:
137
- rubygems_version: 2.5.1
139
+ rubygems_version: 2.4.6
138
140
  signing_key:
139
141
  specification_version: 4
140
142
  summary: Backend service API support for the inflect-client app.