Class: Fastbeans::Response
- Inherits:
-
Object
- Object
- Fastbeans::Response
- Defined in:
- lib/fastbeans/response.rb
Instance Method Summary collapse
- #camelize(term, uppercase_first_letter = true) ⇒ Object
- #error? ⇒ Boolean
-
#initialize(call_data, raw_response) ⇒ Response
constructor
A new instance of Response.
- #payload ⇒ Object
- #raise_exception ⇒ Object
- #signature ⇒ Object
- #signed_with?(orig_signature) ⇒ Boolean
- #underscore(camel_cased_word) ⇒ Object
Constructor Details
#initialize(call_data, raw_response) ⇒ Response
Returns a new instance of Response.
4 5 6 7 |
# File 'lib/fastbeans/response.rb', line 4 def initialize(call_data, raw_response) @call_data = call_data @raw_response = raw_response end |
Instance Method Details
#camelize(term, uppercase_first_letter = true) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fastbeans/response.rb', line 42 def camelize(term, uppercase_first_letter = true) string = term.to_s acronym_regex = /(?=a)b/ if uppercase_first_letter string = string.sub(/^[a-z\d]*/) { $&.capitalize } else string = string.sub(/^(?:#{acronym_regex}(?=\b|[A-Z_])|\w)/) { $&.downcase } end string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::') end |
#error? ⇒ Boolean
9 10 11 |
# File 'lib/fastbeans/response.rb', line 9 def error? @raw_response.is_a?(Hash) and @raw_response.has_key?("fastbeans-error") end |
#payload ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/fastbeans/response.rb', line 25 def payload unless error? @raw_response[1] else raise_exception end end |
#raise_exception ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/fastbeans/response.rb', line 33 def raise_exception name = camelize(underscore(@raw_response["fastbeans-error"])) error = @raw_response["error-information"] msg = "%s\nCall: %s" % [error["message"], error["call"]] backtrace = error["backtrace"].split(/\n/).concat(caller).flatten.compact raise Fastbeans.exception(name), msg, backtrace end |
#signature ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/fastbeans/response.rb', line 13 def signature unless error? @raw_response[0] else nil end end |
#signed_with?(orig_signature) ⇒ Boolean
21 22 23 |
# File 'lib/fastbeans/response.rb', line 21 def signed_with?(orig_signature) signature == orig_signature end |
#underscore(camel_cased_word) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fastbeans/response.rb', line 53 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup acronym_regex = /(?=a)b/ word.gsub!(/::/, '/') word.gsub!(/(?:([A-Za-z\d])|^)(#{acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.gsub!(/\s+/, "_") word.downcase! word end |