Class: Fastbeans::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/fastbeans/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(call_data, raw_response) ⇒ 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



40
41
42
43
44
45
46
47
48
49
# File 'lib/fastbeans/response.rb', line 40

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

#payloadObject



25
26
27
28
29
30
31
# File 'lib/fastbeans/response.rb', line 25

def payload
  unless error?
    @raw_response[1]
  else
    raise to_exception
  end
end

#signatureObject



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

#to_exceptionObject



33
34
35
36
37
38
# File 'lib/fastbeans/response.rb', line 33

def to_exception
  name = camelize(underscore(@raw_response["fastbeans-error"]))
  error = @raw_response["error-information"]

  Fastbeans.exception(name).new("%s. Call: %s" % [error["message"], error["call"]], error["backtrace"])
end

#underscore(camel_cased_word) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastbeans/response.rb', line 51

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