inflect 0.2.6 → 0.3.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 +4 -4
- data/README.md +2 -2
- data/lib/generators/service_generator.rb +5 -0
- data/lib/inflect/configuration.rb +9 -1
- data/lib/inflect/director.rb +2 -1
- data/lib/inflect/i18n.rb +2 -1
- data/lib/inflect/loader.rb +2 -1
- data/lib/inflect/response.rb +10 -2
- data/lib/inflect/responsive.rb +19 -13
- data/lib/inflect/version.rb +1 -1
- data/lib/inflect.rb +6 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc4e321d2369326f998bf8b688106b8e559a4dde
|
4
|
+
data.tar.gz: 64906cd1b3ec23e8b30557813ed61b57ea7cfcbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ede50fc483b8cafea83a2ed1220deabc2f7235af2a81c862334212cd8183978e2f7e0af380b5efa8b1ba971aca5602474687a7f4bdabcf143ae20905b4fdaf1
|
7
|
+
data.tar.gz: ffb68bcf35ace2b363c46e0ca6befe380fa739c9b74e6c5d2db5389e41b0a9c6fd5362cd78297314b3e6741fa7502a34c9d5c8b7fec0e3b06076805bd0b20407
|
data/README.md
CHANGED
@@ -28,9 +28,9 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
Generate your Service:
|
30
30
|
|
31
|
-
$
|
31
|
+
$ inflect generate:service weather
|
32
32
|
|
33
|
-
Implement at least the
|
33
|
+
Implement at least the WeatherService#handle method:
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
class WeatherService < Inflect::AbstractService
|
@@ -1,18 +1,23 @@
|
|
1
1
|
require "thor"
|
2
2
|
|
3
|
+
# Service Generator Class.
|
3
4
|
class ServiceGenerator < Thor::Group
|
4
5
|
include Thor::Actions
|
5
6
|
|
7
|
+
# Defines the templates path.
|
6
8
|
def self.source_root
|
7
9
|
File.join(File.dirname(__FILE__), 'templates')
|
8
10
|
end
|
9
11
|
|
10
12
|
argument :name
|
13
|
+
# Responsible to generate the named service inside lib/services path
|
14
|
+
# of working directory.
|
11
15
|
def generate
|
12
16
|
template('service.tt', "lib/services/#{name}_service.rb")
|
13
17
|
end
|
14
18
|
end
|
15
19
|
|
20
|
+
# CLI command registrator.
|
16
21
|
class CLI < Thor
|
17
22
|
register ServiceGenerator, "generate:service", "generate:service <service_name>", "Generates a service skeleton."
|
18
23
|
end
|
@@ -3,10 +3,13 @@ module Inflect
|
|
3
3
|
attr_reader :configuration
|
4
4
|
end
|
5
5
|
|
6
|
+
# Configuration instance reference.
|
7
|
+
# @return [Inflect::Configuration]
|
6
8
|
def self.configuration
|
7
9
|
@configuration ||= Configuration.new
|
8
10
|
end
|
9
11
|
|
12
|
+
# Method that allows configuration via block.
|
10
13
|
def self.configure
|
11
14
|
yield configuration if block_given?
|
12
15
|
end
|
@@ -14,7 +17,12 @@ module Inflect
|
|
14
17
|
# The class in charge of centralizing the application's
|
15
18
|
# configuration.
|
16
19
|
class Configuration
|
17
|
-
|
20
|
+
# Location of the services directory.
|
21
|
+
attr_accessor :services_path
|
22
|
+
|
23
|
+
# Location of the locale file.
|
24
|
+
attr_accessor :locale_path
|
25
|
+
|
18
26
|
|
19
27
|
def initialize
|
20
28
|
@services_path = File.join('lib', 'services')
|
data/lib/inflect/director.rb
CHANGED
@@ -5,6 +5,7 @@ module Inflect
|
|
5
5
|
# The class in charge of managing the access
|
6
6
|
# and selection of the services.
|
7
7
|
class Director
|
8
|
+
# List of the services loaded successfully sorted by priority.
|
8
9
|
attr_reader :services
|
9
10
|
|
10
11
|
# @param services_path [String]
|
@@ -20,7 +21,7 @@ module Inflect
|
|
20
21
|
# @param words [Array<String, Symbol>]
|
21
22
|
def handle(words)
|
22
23
|
selected_service = select_service(words)
|
23
|
-
selected_service.
|
24
|
+
selected_service.serve(words) unless selected_service.nil?
|
24
25
|
end
|
25
26
|
|
26
27
|
private
|
data/lib/inflect/i18n.rb
CHANGED
@@ -3,7 +3,8 @@ require 'inflect/loader'
|
|
3
3
|
module Inflect
|
4
4
|
# Responsible of internationalization logic in the app.
|
5
5
|
module I18n
|
6
|
-
#
|
6
|
+
# Returns error message inside the locale file.
|
7
|
+
# @param key [Symbol, String] The key to error message.
|
7
8
|
# @return [String] Translation of the error message.
|
8
9
|
def self.errors(key)
|
9
10
|
Loader::locale['errors'][key.to_s]
|
data/lib/inflect/loader.rb
CHANGED
@@ -6,7 +6,6 @@ module Inflect
|
|
6
6
|
# Responsable for loading all the services for Inflect
|
7
7
|
# to comunicate with them and decide wich one will handle
|
8
8
|
# the request.
|
9
|
-
#
|
10
9
|
module Loader
|
11
10
|
using Inflector
|
12
11
|
# Loads all the services from the given path, sorted by
|
@@ -27,6 +26,8 @@ module Inflect
|
|
27
26
|
services.sort
|
28
27
|
end
|
29
28
|
|
29
|
+
# Loads the locale file from the given path
|
30
|
+
# Default in Inflect::Configuration.
|
30
31
|
def self.locale(path = nil)
|
31
32
|
@@locale ||= YAML.load_file(path || Inflect.configuration.locale_path)
|
32
33
|
end
|
data/lib/inflect/response.rb
CHANGED
@@ -2,9 +2,16 @@ require 'inflect/loader'
|
|
2
2
|
require 'inflect/i18n'
|
3
3
|
|
4
4
|
module Inflect
|
5
|
-
#Responsible of encapsulate all the content of the service response
|
5
|
+
#Responsible of encapsulate all the content of the service response.
|
6
6
|
class Response
|
7
|
-
|
7
|
+
# Response content as itself.
|
8
|
+
attr_reader :content
|
9
|
+
# Response attributes like served_by and query_words.
|
10
|
+
attr_reader :attributes
|
11
|
+
# Response timestamp.
|
12
|
+
attr_reader :timestamp
|
13
|
+
#If is not a valid Response, errors lists the not valid attributes.
|
14
|
+
attr_reader :errors
|
8
15
|
|
9
16
|
# @param content [String, Hash] The response of the service. Required.
|
10
17
|
# @param description [Hash] Contains all the description of service response.
|
@@ -19,6 +26,7 @@ module Inflect
|
|
19
26
|
@attributes = extract_attributes(description)
|
20
27
|
end
|
21
28
|
|
29
|
+
# Keys of the required attributes.
|
22
30
|
def self.attribute_keys
|
23
31
|
@@attribute_keys = [:served_by, :query_words]
|
24
32
|
end
|
data/lib/inflect/responsive.rb
CHANGED
@@ -3,21 +3,25 @@ require 'inflect/response'
|
|
3
3
|
module Inflect
|
4
4
|
# Allows services to respond an Inflect::Response instance.
|
5
5
|
module Responsive
|
6
|
-
|
7
6
|
# Method that creates Response instance.
|
8
|
-
# @param
|
9
|
-
# @return [Inflect::Response
|
10
|
-
def
|
11
|
-
|
7
|
+
# @param words [Array<String>] The queried words.
|
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
12
|
validate_response(Inflect::Response.new(content, opts))
|
13
13
|
end
|
14
14
|
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# @
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
# Supply more expressiveness and flexibility to the interface
|
16
|
+
# by allowing multiple ways of responding.
|
17
|
+
# @example Only String
|
18
|
+
# respond 'String Response'
|
19
|
+
# @example As a String with options
|
20
|
+
# respond 'String Response', opt: 'Extra options'
|
21
|
+
# @example Or as a Hash with options
|
22
|
+
# respond({content: 'Hashed Response'}, {opt: 'Extra options'})
|
23
|
+
def respond(content, options= {})
|
24
|
+
[content, options]
|
21
25
|
end
|
22
26
|
|
23
27
|
private
|
@@ -26,8 +30,10 @@ module Inflect
|
|
26
30
|
response.valid? ? response : nil
|
27
31
|
end
|
28
32
|
|
29
|
-
def merge_options(options)
|
30
|
-
|
33
|
+
def merge_options(*options)
|
34
|
+
options.compact.inject(default_options) do |combined_options, current_options|
|
35
|
+
combined_options.merge current_options
|
36
|
+
end
|
31
37
|
end
|
32
38
|
|
33
39
|
# Set a Hash with required +option+ parameters for Inflect::Response.
|
data/lib/inflect/version.rb
CHANGED
data/lib/inflect.rb
CHANGED
@@ -3,14 +3,19 @@ require "inflect/director"
|
|
3
3
|
require "inflect/configuration"
|
4
4
|
require "inflect/abstract_service"
|
5
5
|
|
6
|
+
# Module thats starts the service loading.
|
7
|
+
# It is used as facade to handle queried words.
|
6
8
|
module Inflect
|
7
9
|
class << self
|
10
|
+
# Entry point to handle queried words.
|
11
|
+
# @return [nil, Inflect::Response]
|
8
12
|
def handle(words)
|
9
13
|
director.handle(words)
|
10
14
|
end
|
11
15
|
|
12
16
|
private
|
13
|
-
|
17
|
+
# Method that returns the Director instance reference.
|
18
|
+
# @return [Inflect::Director]
|
14
19
|
def director
|
15
20
|
@@director ||= Director.new(self.configuration.services_path)
|
16
21
|
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
|
+
version: 0.3.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-03-
|
12
|
+
date: 2016-03-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|