rocket_pants-core 2.0.0.pre.1

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.
@@ -0,0 +1,4 @@
1
+ module RocketPants
2
+ require 'rocket_pants/error'
3
+ require 'rocket_pants/errors'
4
+ end
@@ -0,0 +1,61 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
3
+ module RocketPants
4
+
5
+ # Represents the standard error type as defined by the API.
6
+ # RocketPants::Error instances will be caught and automatically rendered as JSON
7
+ # by the controller during processing.
8
+ class Error < StandardError
9
+
10
+ # @overload error_name
11
+ # Returns the error name for this error class, defaulting to
12
+ # the class name underscorized minus _error.
13
+ # @return [Symbol] the given errors name.
14
+ # @overload error_name(value)
15
+ # Sets the error name for the current class.
16
+ # @param [#to_sym] the name of this error.
17
+ def self.error_name(value = nil)
18
+ if value.nil?
19
+ @name ||= name.underscore.split("/").last.sub(/_error$/, '').to_sym
20
+ else
21
+ @name = (value.presence && value.to_sym)
22
+ end
23
+ end
24
+
25
+ # @overload http_status
26
+ # Returns the http status code of this error, defaulting to 400 (Bad Request).
27
+ # @overload http_status(value)
28
+ # Sets the http status code for this error to a given symbol / integer.
29
+ # @param value [String, Fixnum] value the new status code.
30
+ def self.http_status(value = nil)
31
+ if value.nil?
32
+ @http_status ||= 400
33
+ else
34
+ @http_status = (value.presence && value)
35
+ end
36
+ end
37
+
38
+ # Gets the name of this error from the class.
39
+ def error_name
40
+ self.class.error_name
41
+ end
42
+
43
+ # Gets the http status of this error from the class.
44
+ def http_status
45
+ self.class.http_status
46
+ end
47
+
48
+ # Setter for optional data about this error, used for translation.
49
+ attr_writer :context
50
+
51
+ # Gets the context for this error, defaulting to nil.
52
+ # @return [Hash] the context for this param.
53
+ def context
54
+ @context ||= {}
55
+ end
56
+
57
+ error_name :unknown
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,90 @@
1
+ require 'active_support/core_ext/object/blank'
2
+ require 'active_support/core_ext/string/inflections'
3
+
4
+ module RocketPants
5
+
6
+ require 'rocket_pants/error'
7
+
8
+ # A simple map of data about errors that the rocket pants system can handle.
9
+ class Errors
10
+
11
+ @@errors = {}
12
+
13
+ # Returns a hash of all known errors, keyed by their error name.
14
+ # @return [Hash{Symbol => RocketPants::Error}] the hash of known errors.
15
+ def self.all
16
+ @@errors.dup
17
+ end
18
+
19
+ # Looks up a specific error from the given name, returning nil if none are found.
20
+ # @param [#to_sym] name the name of the error to look up.
21
+ # @return [Error, nil] the error class if found, otherwise nil.
22
+ def self.[](name)
23
+ @@errors[name.to_sym]
24
+ end
25
+
26
+ # Adds a given Error class in the list of all errors, making it suitable
27
+ # for lookup via [].
28
+ # @see Errors[]
29
+ # @param [Error] error the error to register.
30
+ def self.add(error)
31
+ @@errors[error.error_name] = error
32
+ end
33
+
34
+ # Creates an error class to represent a given error state.
35
+ # @param [Symbol] name the name of the given error
36
+ # @param [Hash] options the options used to create the error class.
37
+ # @option options [Symbol] :class_name the name of the class (under `RocketPants`), defaulting to the classified name.
38
+ # @option options [Symbol] :error_name the name of the error, defaulting to the name parameter.
39
+ # @option options [Symbol] :http_status the status code for the given error, doing nothing if absent.
40
+ # @example Adding a RocketPants::NinjasUnavailable class w/ `:service_unavailable` as the status code:
41
+ # register! :ninjas_unavailable, :http_status => :service_unavailable
42
+ def self.register!(name, options = {})
43
+ klass_name = (options[:class_name] || name.to_s.classify).to_sym
44
+ base_klass = options[:base] || Error
45
+ raise ArgumentError, ":base must be a subclass of RocketPants::Error" unless base_klass <= Error
46
+ klass = Class.new(base_klass)
47
+ klass.error_name(options[:error_name] || name.to_s.underscore)
48
+ klass.http_status(options[:http_status]) if options[:http_status].present?
49
+ (options[:under] || RocketPants).const_set klass_name, klass
50
+ add klass
51
+ klass
52
+ end
53
+
54
+ # The default set of exceptions.
55
+ register! :throttled, http_satus: :service_unavailable
56
+ register! :unauthenticated, http_satus: :unauthorized
57
+ register! :invalid_version, http_satus: :not_found
58
+ register! :not_implemented, http_satus: :service_unavailable
59
+ register! :not_found, http_satus: :not_found
60
+ register! :bad_request, http_satus: :bad_request
61
+ register! :conflict, http_satus: :conflict
62
+ register! :forbidden, http_satus: :forbidden
63
+
64
+ end
65
+
66
+ class InvalidResource < RocketPants::Error
67
+
68
+ http_status :unprocessable_entity
69
+ error_name :invalid_resource
70
+
71
+ # Errors are ActiveModel Errors
72
+ attr_reader :errors
73
+
74
+ def initialize(errors, *args)
75
+ @errors = errors
76
+ super *args
77
+ end
78
+
79
+ def context
80
+ super.tap do |ctx|
81
+ extras = (ctx[:metadata] ||= {})
82
+ extras[:messages] = errors.to_hash if errors
83
+ end
84
+ end
85
+
86
+ Errors.add self
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1 @@
1
+ require 'rocket_pants/core'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rocket_pants-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0.pre.1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Darcy Laycock
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rr
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '2.0'
94
+ description: ! 'RocketPants core provides functionality for RocketPants shared between
95
+ different '
96
+ email:
97
+ - sutto@sutto.net
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - lib/rocket_pants/core.rb
103
+ - lib/rocket_pants/error.rb
104
+ - lib/rocket_pants/errors.rb
105
+ - lib/rocket_pants-core.rb
106
+ homepage: http://github.com/filtersquad
107
+ licenses: []
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.9.3
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: 1.3.6
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.24
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Shared utilities for RocketPants - an opionionated set of API tools for Rails
130
+ test_files: []