tzispa 0.5.13 → 0.5.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fc85a39ec42b71cb36319ccf24e814ca72c2512
4
- data.tar.gz: d648e30afbfea5e4de07da024019235836532487
3
+ metadata.gz: 679fca5266d73401ce64876f16bf8d6a867eacf3
4
+ data.tar.gz: 5a4fb29bec5b1e0110de07ee18e38aba806c88f9
5
5
  SHA512:
6
- metadata.gz: 2db1ce9275f3a90b2518b1814d555131550792cd7de4356bb711a17c29e329f7bbf03585690853f46c7cceb366bc56a0a5158a2763d78dfba00db641cce96b98
7
- data.tar.gz: 1ef6d906982dd5be0eb4f60f08ecac5ad25f6657006a2465d856531a52c25e0f9bc93ec5560d2ce3d2540329531949d9c2168ac86becc7e830011e76948fe14a
6
+ metadata.gz: b13878b1fc41fb1ecc5b69366ca7802ada0751fe4d880247e5f1eb7fd680da6b057950b97b9e77cba1e02c8ec4ef269737843e007075985c019de47e67545dc9
7
+ data.tar.gz: 7e1f29257c2764a4761e49051fce89869cfe2faa51b4aeac0880a03a3ce667dea15de69e4286b2b7474c1f9ebb9db97a726e05c344b9617235f31feeaec6f60e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@ Tzispa
2
2
 
3
3
  General purpose web framework
4
4
 
5
+ ## v0.5.14
6
+ - populate json response when there are handler errros
7
+ - api error messages now managed by i18n files
8
+ - code fixes for replacing TzString with String refinement
9
+
5
10
  ## v0.5.13
6
11
  - update code creation templates in cli to reflect DSL changes
7
12
  - remove not used gem dependencies
@@ -2,8 +2,10 @@
2
2
 
3
3
  require 'forwardable'
4
4
  require 'json'
5
+ require 'i18n'
5
6
  require 'tzispa/helpers/provider'
6
7
  require 'tzispa/helpers/sign_requirer'
8
+ require 'tzispa/utils/string'
7
9
 
8
10
  module Tzispa
9
11
  module Api
@@ -21,34 +23,32 @@ module Tzispa
21
23
  include Tzispa::Helpers::SignRequirer
22
24
  extend Forwardable
23
25
 
26
+ using Tzispa::Utils
27
+
24
28
  attr_reader :context, :response_verb, :data, :status
25
29
  def_delegators :@context, :request, :response, :app, :repository, :config
26
30
 
27
- HANDLED_UNDEFINED = nil
28
- HANDLED_OK = 1
29
- HANDLED_MISSING_PARAMETER = 98
30
- HANDLED_ERROR = 99
31
- HANDLED_RESULT = 200
31
+ HANDLER_STATUS_UNDEFINED = nil
32
+ HANDLER_STATUS_OK = :ok
33
+ HANDLER_MISSING_PARAMETER = :missing_parameter
32
34
 
33
- HANDLED_MESSAGES = {
34
- HANDLED_OK => 'La operación se ha realizado correctamente',
35
- HANDLED_MISSING_PARAMETER => 'Error: faltan parámetros para realizar la operación',
36
- HANDLED_ERROR => 'Error indeterminado: la operación no se ha podido realizar'
37
- }
38
35
 
39
36
  def initialize(context)
40
37
  @context = context
41
38
  end
42
39
 
43
- def result(response_verb:, data: nil, status: nil, error: nil)
44
- @status = status if status
40
+ def result(response_verb:, data: nil, status: HANDLER_STATUS_UNDEFINED)
45
41
  @response_verb = response_verb
42
+ @status = status if status
46
43
  @data = data
47
- @error = error
48
44
  end
49
45
 
50
- def result_json(data, status: nil, error: nil)
51
- result response_verb: :json, data: data, status: status, error: error
46
+ def error?
47
+ status && status != HANDLER_STATUS_OK
48
+ end
49
+
50
+ def result_json(data, status: nil)
51
+ result response_verb: :json, data: data, status: status
52
52
  end
53
53
 
54
54
  def result_download(data, status: nil)
@@ -60,17 +60,10 @@ module Tzispa
60
60
  end
61
61
 
62
62
  def message
63
- case ss = status.to_i
64
- when ss >= HANDLED_OK && ss <= HANDLED_ERROR
65
- HANDLED_MESSAGES[status]
66
- when ss > HANDLED_ERROR && ss < HANDLED_RESULT
67
- error_message status
68
- when ss > HANDLED_RESULT
69
- result_messages status
70
- end
63
+ I18n.t("#{self.class.name.dottize}.#{status}ss", default: "#{status}") if status
71
64
  end
72
65
 
73
- def call(verb, predicate=nil)
66
+ def run!(verb, predicate=nil)
74
67
  raise UnknownHandlerVerb.new(verb, self.class.name) unless provides? verb
75
68
  raise InvalidSign.new if sign_required? && !sign_valid?
76
69
  # process compound predicates
@@ -82,23 +75,12 @@ module Tzispa
82
75
  @status = value
83
76
  end
84
77
 
85
-
86
78
  protected
87
79
 
88
80
  def static_path_sign?
89
81
  context.path_sign? context.router_params[:sign], context.router_params[:handler], context.router_params[:verb], context.router_params[:predicate]
90
82
  end
91
83
 
92
- private
93
-
94
- def result_messages(status)
95
- self.class::RESULT_MESSAGES[status] if (defined?( self.class::RESULT_MESSAGES ) && self.class::RESULT_MESSAGES.is_a?(Hash))
96
- end
97
-
98
- def error_message(status)
99
- "#{self.class::ERROR_MESSAGES[status]}#{': '+@error.to_s if @error}" if (defined?( self.class::ERROR_MESSAGES ) && self.class::ERROR_MESSAGES.is_a?(Hash))
100
- end
101
-
102
84
 
103
85
  end
104
86
  end
@@ -10,6 +10,8 @@ module Tzispa
10
10
 
11
11
  class App
12
12
 
13
+ using Tzispa::Utils
14
+
13
15
  APP_STRUCTURE = [
14
16
  'api',
15
17
  'locales',
@@ -40,7 +42,7 @@ module Tzispa
40
42
  private
41
43
 
42
44
  def app_class_name
43
- @app_class_name ||= "#{TzString.camelize domain.name}App"
45
+ @app_class_name ||= "#{domain.name.camelize}App"
44
46
  end
45
47
 
46
48
  def update_project
@@ -15,26 +15,9 @@ module Tzispa
15
15
 
16
16
  class Api < Base
17
17
 
18
- include Tzispa::Helpers::Response
19
-
18
+ using Tzispa::Utils
20
19
 
21
- def self.generate_handler(domain, name)
22
- raise "The handler '#{name}' already exist" if File.exist?(handler_class_file)
23
- File.open(handler_class_file(domain, name), "w") { |f|
24
- handler_code = TzString.new
25
- f.puts handler_code.indenter("require 'tzispa/api/handler'\n\n")
26
- level = 0
27
- handler_namespace.split('::').each { |ns|
28
- f.puts handler_code.indenter("module #{ns}\n", level > 0 ? 2 : 0).to_s
29
- level += 1
30
- }
31
- f.puts handler_code.indenter("\nclass #{handler_class_name} < Tzispa::Api::Handler\n\n", 2)
32
- f.puts handler_code.indenter("end\n\n")
33
- handler_namespace.split('::').each { |ns|
34
- f.puts handler_code.unindenter("end\n", 2)
35
- }
36
- }
37
- end
20
+ include Tzispa::Helpers::Response
38
21
 
39
22
  def dispatch!
40
23
  handler_name, domain_name = context.router_params[:handler].split('.').reverse
@@ -42,48 +25,52 @@ module Tzispa
42
25
  verb = context.router_params[:verb]
43
26
  predicate = context.router_params[:predicate]
44
27
  handler = self.class.handler_class(domain, handler_name).new(context)
45
- handler.call verb, predicate
28
+ handler.run! verb, predicate
46
29
  send(handler.response_verb, handler) if handler.response_verb
47
30
  response.finish
48
31
  end
49
32
 
50
- def redirect(target)
51
- url = if target.data && !target.data.strip.empty?
52
- target.data.start_with?('#') ? "#{request.referer}#{target.data}" : target.data
33
+ def redirect(handler)
34
+ url = if handler.data && !handler.data.strip.empty?
35
+ handler.data.start_with?('#') ? "#{request.referer}#{handler.data}" : handler.data
53
36
  else
54
37
  request.referer
55
38
  end
56
- context.flash << target.message if config.sessions&.enabled
39
+ puts "#{handler.error?} -> #{handler.message}"
40
+ context.flash << handler.message if config.sessions&.enabled && handler.error?
57
41
  context.redirect url, config.absolute_redirects, response
58
42
  end
59
43
 
60
- def html(content)
44
+ def html(handler)
61
45
  content_type :htm
62
- response.body << content.data
63
- set_api_headers content.status
46
+ context.flash << handler.message if config.sessions&.enabled && handler.error?
47
+ response.body << handler.data
48
+ set_api_headers handler.status
64
49
  end
65
50
 
66
- def json(content)
51
+ def json(handler)
67
52
  content_type :json
68
- data = ::Hash === content.data || ::Array === content.data ? content.data : JSON.parse(content.data)
69
- response.body << data.to_json
70
- set_api_headers content.status
53
+ data = ::String === handler.data ? JSON.parse(handler.data) : handler.data.to_json
54
+ response.body << data
55
+ response.body << Hash[:__error, true, :__msg_error, handler.message].to_json if handler.error?
56
+ set_api_headers handler.status
71
57
  end
72
58
 
73
- def text(content)
59
+ def text(handler)
74
60
  content_type :text
75
- response.body << content.data
76
- set_api_headers content.status
61
+ context.flash << handler.message if config.sessions&.enabled && handler.error?
62
+ response.body << handler.data
63
+ set_api_headers handler.status
77
64
  end
78
65
 
79
- def download(content)
80
- send_file content.data[:path], content.data
66
+ def download(handler)
67
+ send_file handler.data[:path], handler.data
81
68
  end
82
69
 
83
70
  class << self
84
71
 
85
72
  def handler_class_name(handler_name)
86
- "#{TzString.camelize handler_name}Handler"
73
+ "#{handler_name.camelize}Handler"
87
74
  end
88
75
 
89
76
  def handler_class_file(domain, handler_name)
@@ -91,12 +78,30 @@ module Tzispa
91
78
  end
92
79
 
93
80
  def handler_namespace(domain)
94
- "#{TzString.camelize domain.name }::Api"
81
+ "#{domain.name.to_s.camelize}::Api"
95
82
  end
96
83
 
97
84
  def handler_class(domain, handler_name)
98
85
  domain.require "api/#{handler_name}"
99
- TzString.constantize "#{handler_namespace domain}::#{handler_class_name handler_name}"
86
+ "#{handler_namespace domain}::#{handler_class_name handler_name}".constantize
87
+ end
88
+
89
+ def generate_handler(domain, name)
90
+ raise "The handler '#{name}' already exist" if File.exist?(handler_class_file)
91
+ File.open(handler_class_file(domain, name), "w") { |f|
92
+ handler_code = String.new
93
+ f.puts handler_code.indenter("require 'tzispa/api/handler'\n\n")
94
+ level = 0
95
+ handler_namespace.split('::').each { |ns|
96
+ f.puts handler_code.indenter("module #{ns}\n", level > 0 ? 2 : 0).to_s
97
+ level += 1
98
+ }
99
+ f.puts handler_code.indenter("\nclass #{handler_class_name} < Tzispa::Api::Handler\n\n", 2)
100
+ f.puts handler_code.indenter("end\n\n")
101
+ handler_namespace.split('::').each { |ns|
102
+ f.puts handler_code.unindenter("end\n", 2)
103
+ }
104
+ }
100
105
  end
101
106
 
102
107
  end
@@ -105,7 +110,7 @@ module Tzispa
105
110
 
106
111
  def set_api_headers(status)
107
112
  response['X-API'] = "#{context.router_params[:handler]}:#{context.router_params[:verb]}:#{context.router_params[:predicate]}"
108
- response['X-API-STATE'] = "#{status.to_i}"
113
+ response['X-API-STATE'] = "#{status}"
109
114
  end
110
115
 
111
116
  end
data/lib/tzispa/routes.rb CHANGED
@@ -10,6 +10,8 @@ module Tzispa
10
10
 
11
11
  class Routes
12
12
 
13
+ using Tzispa::Utils
14
+
13
15
  CONTROLLERS_BASE = 'Tzispa::Controller'
14
16
 
15
17
  attr_reader :router, :map_path
@@ -29,7 +31,7 @@ module Tzispa
29
31
  spec_control, callmethod = controller.to_s.split(':')
30
32
  mpath = spec_control.split('#')
31
33
  req_controller = mpath.pop
32
- controller = TzString.camelize(req_controller).to_s
34
+ controller = req_controller.camelize
33
35
  if mpath.count > 1
34
36
  controller_module = mpath.collect!{ |w| w.capitalize }.join('::')
35
37
  require_relative "./controller/#{req_controller}"
@@ -38,7 +40,7 @@ module Tzispa
38
40
  require "tzispa/controller/#{req_controller}"
39
41
  end
40
42
  @router.add(path).tap { |rule|
41
- rule.to TzString.constantize("#{controller_module}::#{controller}").new(@app, callmethod)
43
+ rule.to "#{controller_module}::#{controller}".constantize.new(@app, callmethod)
42
44
  rule.name = route_id
43
45
  rule.add_request_method(methods) if methods
44
46
  rule.add_match_with(matching) if matching
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tzispa
4
- VERSION = '0.5.13'
4
+ VERSION = '0.5.14'
5
5
  FRAMEWORK_NAME = 'Tzispa'
6
6
  GEM_NAME = 'tzispa'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzispa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.13
4
+ version: 0.5.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Antonio Piñero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-15 00:00:00.000000000 Z
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  version: '0'
176
176
  requirements: []
177
177
  rubyforge_project:
178
- rubygems_version: 2.5.1
178
+ rubygems_version: 2.5.2
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: A sparkling web framework