angus 0.0.13.beta3 → 0.0.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: 0186f917aedde0df96b6f5691bbeaf4ffd186d31
4
- data.tar.gz: b98f2b08e8316181f8d800e99ba54b299d07d398
3
+ metadata.gz: 53c6533eb8c6aabf5133445668c3e81ccd7eae54
4
+ data.tar.gz: 755b9b7b34752eef87997c145b0d917f160ae382
5
5
  SHA512:
6
- metadata.gz: 223c68ba16e11933c76a47605da7369741909bce4290cd37645a586b7a96fc57b76caebfae7a65bdc6e702438d1f205194c542538c0e31b4771a1b404216d849
7
- data.tar.gz: 4741ca50f180b0b28c929d4df6501004399830aa951cf74d4028062c625c61211acb5bb1eecfa75b8800279b264354cf28eb329692a91fe1e67e6607c8285919
6
+ metadata.gz: 1805ca73f7cdc9a3a32dcb0ff4467a271520505224f9442462db7049d578163304c7d328a3a6edf908569df37b0d86d0698fa5c0e2a06e94576c87057f4cb3b4
7
+ data.tar.gz: 4c32905edd8769db1e4d1ad5067749749868d84f322e22f6187527eeca9476413ad9490fce3f4d6c3f15ee066869bfe8967cfd1aade3bf90f36e0864f99ac48c
@@ -33,11 +33,14 @@ module Angus
33
33
 
34
34
  attr_accessor :default_doc_language
35
35
  attr_accessor :validate_params
36
+ attr_accessor :path_prefix
37
+ attr_accessor :root_path
36
38
 
37
39
  def initialize
38
40
  super
39
41
 
40
42
  @resources_definitions = []
43
+ @root_path ||= ''
41
44
  @version = FIRST_VERSION
42
45
  @name = self.class.name.downcase
43
46
  @configured = false
@@ -91,7 +94,15 @@ module Angus
91
94
  def configure!
92
95
  raise 'Already configured' if configured?
93
96
 
94
- @definitions = Angus::SDoc::DefinitionsReader.service_definition('definitions')
97
+ definitions_path = if @root_path.empty?
98
+ 'definitions'
99
+ else
100
+ File.join(@root_path, 'definitions')
101
+ end
102
+
103
+ @definitions = Angus::SDoc::DefinitionsReader.service_definition(definitions_path)
104
+
105
+ self.path_prefix ||= @definitions.code_name
95
106
 
96
107
  configure
97
108
 
@@ -107,13 +118,13 @@ module Angus
107
118
  end
108
119
 
109
120
  def register(resource_name, options = {})
110
- resource_definition = ResourceDefinition.new(resource_name, @definitions)
121
+ resource_definition = ResourceDefinition.new(@root_path, resource_name, @definitions)
111
122
 
112
123
  @resources_definitions << resource_definition
113
124
  end
114
125
 
115
126
  def base_path
116
- "/#{service_code_name}"
127
+ "/#@path_prefix"
117
128
  end
118
129
 
119
130
  def register_resource_routes(resource_definition)
@@ -1,7 +1,10 @@
1
1
  require 'yaml'
2
2
 
3
+ require_relative 'request/json_params'
4
+
3
5
  module Angus
4
6
  class BaseResource
7
+ include Angus::Request::JsonParams
5
8
 
6
9
  def self.inherited(subclass)
7
10
  self._before_filers.each { |before_filer| subclass._before_filers << before_filer }
@@ -1,8 +1,14 @@
1
1
  module Angus
2
2
  class ServerCommand < Thor::Group
3
+ include Thor::Actions
4
+
5
+ class_option :port, desc: 'use PORT (default: 9292)', type: :string, required: false, default: '9292'
6
+ class_option :host, desc: 'listen on HOST (default: localhost)', type: :string, required: false, default: '0.0.0.0'
3
7
 
4
8
  def server
5
- command_processor.run('rackup', verbose: false)
9
+ port_option = "-p #{options[:port]}" || ''
10
+ host_option = "--host #{options[:host]}" || ''
11
+ command_processor.run("rackup #{port_option} #{host_option}", verbose: false)
6
12
  end
7
13
 
8
14
  private
@@ -0,0 +1,11 @@
1
+ module Angus
2
+ module Exceptions
3
+
4
+ class InvalidRequestFormat < StandardError
5
+ def message
6
+ 'Invalid request format'
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Angus
2
+ module Exceptions
3
+
4
+ class InvalidValueError < StandardError
5
+ def initialize(param = nil, value = nil)
6
+ @param = param
7
+ @value = value
8
+ end
9
+
10
+ def message
11
+ "Invalid value #@value for param #@param"
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Angus
2
+ module Exceptions
3
+
4
+ class MissingParameterError < StandardError
5
+
6
+ def initialize(parameter_name = nil)
7
+ @parameter_name = parameter_name
8
+ end
9
+
10
+ def message
11
+ "The parameter #@parameter_name is missing."
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -88,6 +88,8 @@ module Angus
88
88
  def additional_message_attributes(error)
89
89
  error_definition = error_definition(error)
90
90
 
91
+ return {} unless error_definition
92
+
91
93
  error_definition.fields.inject({}) do |attributes, field|
92
94
  attributes.merge!({ field.name => error.send(field.name)})
93
95
  end
@@ -133,4 +135,4 @@ module Angus
133
135
 
134
136
  end
135
137
  end
136
- end
138
+ end
@@ -0,0 +1,86 @@
1
+ require_relative '../exceptions/invalid_request_format'
2
+ require_relative '../exceptions/invalid_value_error'
3
+ require_relative '../exceptions/missing_parameter_error'
4
+
5
+ module Angus
6
+ module Request
7
+ module JsonParams
8
+
9
+ # Returns the json read from the request body.
10
+ #
11
+ # The keys for the json are symbolized.
12
+ #
13
+ # @return [Hash] The parsed request body.
14
+ #
15
+ # @raise [InvalidRequestFormat] When an invalid json is received.
16
+ def json_body
17
+ @json_body ||= JSON(request.body.read, :symbolize_names => true)
18
+ rescue JSON::ParserError
19
+ raise Angus::Exceptions::InvalidRequestFormat
20
+ end
21
+
22
+ # Parses a time param.
23
+ #
24
+ # @param [String, Symbol] param Parameter name.
25
+ # @param [Boolean] required When true, raises a MissingParameterError if param does not exist.
26
+ #
27
+ # @raise (see #get_json_param)
28
+ # @raise [InvalidValueError] when datetime is not parseable.
29
+ #
30
+ # @return [Time]
31
+ def get_json_datetime(param, required = false)
32
+ value = get_json_param(param, required)
33
+ Time.parse(value)
34
+ rescue ArgumentError
35
+ raise Angus::Exceptions::InvalidValueError.new(param, value)
36
+ end
37
+
38
+ # Parses an integer param.
39
+ #
40
+ # @param [String, Symbol] param Parameter name.
41
+ # @param [Boolean] required When true, raises a MissingParameterError if param does not exist.
42
+ #
43
+ # @raise (see #get_json_param)
44
+ # @raise [InvalidValueError] when value is not parseable.
45
+ #
46
+ # @return [Numeric]
47
+ def get_json_integer(param, required = false)
48
+ value = get_json_param(param, required)
49
+ Integer(value)
50
+ rescue ArgumentError
51
+ raise Angus::Exceptions::InvalidValueError.new(param, value)
52
+ end
53
+
54
+ # Returns the value of a given param.
55
+ #
56
+ # @param [Symbol] param Param name
57
+ #
58
+ # @raise [MissingParameterError] when required param not found
59
+ #
60
+ # @return Param's value
61
+ def get_json_param(param, required = false)
62
+ value = json_body[param.to_sym]
63
+
64
+ raise Angus::Exceptions::MissingParameterError.new(param) if required && value.blank?
65
+
66
+ value
67
+ end
68
+
69
+ # Parses a boolean param.
70
+ #
71
+ # @param [String, Symbol] param Parameter name.
72
+ # @param [Boolean] required When true, raises a MissingParameterError if param does not exist.
73
+ #
74
+ # @return [Boolean]
75
+ def get_json_boolean(param, required = true)
76
+ value = json_body[param.to_sym]
77
+
78
+
79
+ raise Angus::Exceptions::MissingParameterError.new(param) if required && value.nil?
80
+
81
+ value == 'true' || value == true
82
+ end
83
+
84
+ end
85
+ end
86
+ end
@@ -3,10 +3,11 @@ require 'yaml'
3
3
  module Angus
4
4
  class ResourceDefinition
5
5
 
6
- def initialize(resource_name, representations)
7
- @resource_name = resource_name
8
- @resource_class_name = classify_resource(resource_name)
9
- @representations = representations || {}
6
+ def initialize(root_path, resource_name, representations)
7
+ @root_path = root_path
8
+ @resource_name = resource_name
9
+ @resource_class_name = classify_resource(resource_name)
10
+ @representations = representations || {}
10
11
  end
11
12
 
12
13
  def operations
@@ -25,7 +26,13 @@ module Angus
25
26
  end
26
27
 
27
28
  def resource_path
28
- File.join('resources', canonical_name)
29
+ resource_path = File.join('resources', canonical_name)
30
+
31
+ if @root_path.empty?
32
+ resource_path
33
+ else
34
+ File.join(@root_path, resource_path)
35
+ end
29
36
  end
30
37
 
31
38
  def build_response_metadata(response_representation)
@@ -36,6 +36,24 @@ RSpec::Matchers.define :be_success do
36
36
  end
37
37
  end
38
38
 
39
+ RSpec::Matchers.define :be_unauthorized do
40
+ match do |operation_response|
41
+ operation_response.unauthorized?
42
+ end
43
+
44
+ failure_message_for_should do |operation_response|
45
+ "expected that (#{operation_response.http_status_code}, #{operation_response.status}) would be (401, unauthorized)"
46
+ end
47
+
48
+ failure_message_for_should_not do |operation_response|
49
+ "expected that (#{operation_response.http_status_code}, #{operation_response.status}) would not be (401, unauthorized)"
50
+ end
51
+
52
+ description do
53
+ 'be a (401, unauthorized) response'
54
+ end
55
+ end
56
+
39
57
  RSpec::Matchers.define :be_forbidden do
40
58
  match do |operation_response|
41
59
  operation_response.forbidden?
@@ -58,7 +76,7 @@ RSpec::Matchers.define :contain_message do |level = nil, key = nil, description
58
76
  match do |operation_response|
59
77
  operation_response.messages.find do |message|
60
78
  level_and_key_matches = message['level'] == level.to_s && message['key'] == key
61
-
79
+
62
80
  description_matches = true
63
81
 
64
82
  if level_and_key_matches && description.present?
@@ -23,27 +23,32 @@ class OperationResponse
23
23
  end
24
24
 
25
25
  def success?
26
- @parsed_response['status'] == 'success' &&
26
+ @parsed_response['status'] == 'success' &&
27
27
  http_status_code == Angus::Responses::HTTP_STATUS_CODE_OK
28
28
  end
29
29
 
30
+ def unauthorized?
31
+ @parsed_response['status'] == 'error' &&
32
+ http_status_code == Angus::Responses::HTTP_STATUS_CODE_UNAUTHORIZED
33
+ end
34
+
30
35
  def forbidden?
31
- @parsed_response['status'] == 'error' &&
36
+ @parsed_response['status'] == 'error' &&
32
37
  http_status_code == Angus::Responses::HTTP_STATUS_CODE_FORBIDDEN
33
38
  end
34
39
 
35
40
  def not_found?
36
- @parsed_response['status'] == 'error' &&
41
+ @parsed_response['status'] == 'error' &&
37
42
  http_status_code == Angus::Responses::HTTP_STATUS_CODE_NOT_FOUND
38
43
  end
39
44
 
40
45
  def conflict?
41
- @parsed_response['status'] == 'error' &&
46
+ @parsed_response['status'] == 'error' &&
42
47
  http_status_code == Angus::Responses::HTTP_STATUS_CODE_CONFLICT
43
48
  end
44
49
 
45
50
  def unprocessable_entity?
46
- @parsed_response['status'] == 'error' &&
51
+ @parsed_response['status'] == 'error' &&
47
52
  http_status_code == Angus::Responses::HTTP_STATUS_CODE_UNPROCESSABLE_ENTITY
48
53
  end
49
54
 
@@ -4,6 +4,7 @@ module Angus
4
4
  # TODO remove HTTP_STATUS from all constants
5
5
  HTTP_STATUS_CODE_OK = 200
6
6
 
7
+ HTTP_STATUS_CODE_UNAUTHORIZED = 401
7
8
  HTTP_STATUS_CODE_FORBIDDEN = 403
8
9
  HTTP_STATUS_CODE_NOT_FOUND = 404
9
10
  HTTP_STATUS_CODE_CONFLICT = 409
@@ -1,3 +1,3 @@
1
1
  module Angus
2
- VERSION = '0.0.13.beta3'
2
+ VERSION = '0.0.14'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13.beta3
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Ifran
@@ -12,10 +12,11 @@ authors:
12
12
  - Guzman Iglesias
13
13
  - Martin Cabrera
14
14
  - Marcelo Casiraghi
15
+ - Lucas Aragno
15
16
  autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
- date: 2014-12-29 00:00:00.000000000 Z
19
+ date: 2018-04-05 00:00:00.000000000 Z
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: thor
@@ -207,6 +208,9 @@ files:
207
208
  - lib/angus/commands/server_command.rb
208
209
  - lib/angus/definition_reader.rb
209
210
  - lib/angus/exceptions.rb
211
+ - lib/angus/exceptions/invalid_request_format.rb
212
+ - lib/angus/exceptions/invalid_value_error.rb
213
+ - lib/angus/exceptions/missing_parameter_error.rb
210
214
  - lib/angus/generator/templates/Gemfile.erb
211
215
  - lib/angus/generator/templates/README.md
212
216
  - lib/angus/generator/templates/config.ru.erb
@@ -228,6 +232,7 @@ files:
228
232
  - lib/angus/renders/base.rb
229
233
  - lib/angus/renders/html_render.rb
230
234
  - lib/angus/renders/json_render.rb
235
+ - lib/angus/request/json_params.rb
231
236
  - lib/angus/request_handler.rb
232
237
  - lib/angus/resource_definition.rb
233
238
  - lib/angus/response.rb
@@ -305,58 +310,58 @@ required_ruby_version: !ruby/object:Gem::Requirement
305
310
  version: '0'
306
311
  required_rubygems_version: !ruby/object:Gem::Requirement
307
312
  requirements:
308
- - - ">"
313
+ - - ">="
309
314
  - !ruby/object:Gem::Version
310
- version: 1.3.1
315
+ version: '0'
311
316
  requirements: []
312
317
  rubyforge_project:
313
- rubygems_version: 2.2.2
318
+ rubygems_version: 2.5.2
314
319
  signing_key:
315
320
  specification_version: 4
316
321
  summary: A web services summary
317
322
  test_files:
323
+ - spec/spec_helper.rb
318
324
  - spec/angus/middleware/exception_handler_spec.rb
319
325
  - spec/angus/request_handler_spec.rb
320
326
  - spec/angus/rspec/examples/describe_errors_spec.rb
321
- - spec/functional/basic/definitions/messages.yml
322
- - spec/functional/basic/definitions/representations.yml
323
- - spec/functional/basic/definitions/service.yml
324
- - spec/functional/basic/definitions/users/operations.yml
325
- - spec/functional/basic/exceptions/user_errors.rb
326
- - spec/functional/basic/models/user.rb
327
- - spec/functional/basic/resources/users.rb
328
- - spec/functional/basic/services/basic.rb
327
+ - spec/support/matchers/have_in_message_matcher.rb
328
+ - spec/support/matchers/have_error_response_matcher.rb
329
+ - spec/support/matchers/have_success_response_matcher.rb
329
330
  - spec/functional/basic_spec.rb
330
- - spec/functional/empty_resource/definitions/messages.yml
331
- - spec/functional/empty_resource/definitions/representations.yml
332
- - spec/functional/empty_resource/definitions/service.yml
333
- - spec/functional/empty_resource/definitions/users/operations.yml
334
- - spec/functional/empty_resource/resources/users.rb
335
- - spec/functional/empty_resource/services/empty_resource.rb
336
- - spec/functional/empty_resource_spec.rb
331
+ - spec/functional/filters/resources/users.rb
332
+ - spec/functional/filters/models/user.rb
333
+ - spec/functional/filters/exceptions/user_errors.rb
337
334
  - spec/functional/filters/definitions/messages.yml
338
- - spec/functional/filters/definitions/representations.yml
339
335
  - spec/functional/filters/definitions/service.yml
340
336
  - spec/functional/filters/definitions/users/operations.yml
341
- - spec/functional/filters/exceptions/user_errors.rb
342
- - spec/functional/filters/models/user.rb
343
- - spec/functional/filters/resources/users.rb
337
+ - spec/functional/filters/definitions/representations.yml
344
338
  - spec/functional/filters/services/filters.rb
345
- - spec/functional/filters_spec.rb
346
- - spec/functional/no_resources/definitions/messages.yml
347
- - spec/functional/no_resources/definitions/representations.yml
348
- - spec/functional/no_resources/definitions/service.yml
349
- - spec/functional/no_resources/services/no_resources.rb
350
- - spec/functional/no_resources_spec.rb
351
- - spec/functional/type_validation/definitions/admins/operations.yml
339
+ - spec/functional/type_validation/resources/admins.rb
340
+ - spec/functional/type_validation/models/admin.rb
352
341
  - spec/functional/type_validation/definitions/messages.yml
353
- - spec/functional/type_validation/definitions/representations.yml
342
+ - spec/functional/type_validation/definitions/admins/operations.yml
354
343
  - spec/functional/type_validation/definitions/service.yml
355
- - spec/functional/type_validation/models/admin.rb
356
- - spec/functional/type_validation/resources/admins.rb
344
+ - spec/functional/type_validation/definitions/representations.yml
357
345
  - spec/functional/type_validation/services/type_validation.rb
346
+ - spec/functional/empty_resource_spec.rb
347
+ - spec/functional/basic/resources/users.rb
348
+ - spec/functional/basic/models/user.rb
349
+ - spec/functional/basic/exceptions/user_errors.rb
350
+ - spec/functional/basic/definitions/messages.yml
351
+ - spec/functional/basic/definitions/service.yml
352
+ - spec/functional/basic/definitions/users/operations.yml
353
+ - spec/functional/basic/definitions/representations.yml
354
+ - spec/functional/basic/services/basic.rb
358
355
  - spec/functional/type_validation_spec.rb
359
- - spec/spec_helper.rb
360
- - spec/support/matchers/have_error_response_matcher.rb
361
- - spec/support/matchers/have_in_message_matcher.rb
362
- - spec/support/matchers/have_success_response_matcher.rb
356
+ - spec/functional/no_resources_spec.rb
357
+ - spec/functional/no_resources/definitions/messages.yml
358
+ - spec/functional/no_resources/definitions/service.yml
359
+ - spec/functional/no_resources/definitions/representations.yml
360
+ - spec/functional/no_resources/services/no_resources.rb
361
+ - spec/functional/filters_spec.rb
362
+ - spec/functional/empty_resource/resources/users.rb
363
+ - spec/functional/empty_resource/definitions/messages.yml
364
+ - spec/functional/empty_resource/definitions/service.yml
365
+ - spec/functional/empty_resource/definitions/users/operations.yml
366
+ - spec/functional/empty_resource/definitions/representations.yml
367
+ - spec/functional/empty_resource/services/empty_resource.rb