Class: Snarl::SNP::Response

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

Constant Summary collapse

ResponseParseRegexp =
/\ASNP\/[\d\.]+\/(\d+)\/(.+?)\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
# File 'lib/snarl/snp/response.rb', line 5

def initialize(s)
  if s.respond_to?(:get) then
    @response = s.get
  else
    @response = s
  end
  parse_response
end

Instance Attribute Details

#codeObject (readonly) Also known as: status

Returns the value of attribute code.



14
15
16
# File 'lib/snarl/snp/response.rb', line 14

def code
  @code
end

#infomationObject (readonly)

Returns the value of attribute infomation.



14
15
16
# File 'lib/snarl/snp/response.rb', line 14

def infomation
  @infomation
end

#messageObject (readonly)

Returns the value of attribute message.



14
15
16
# File 'lib/snarl/snp/response.rb', line 14

def message
  @message
end

#requestObject

Returns the value of attribute request.



15
16
17
# File 'lib/snarl/snp/response.rb', line 15

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/snarl/snp/response.rb', line 14

def response
  @response
end

Instance Method Details

#errorObject



28
# File 'lib/snarl/snp/response.rb', line 28

def error ; Error.klass(self.code) ; end

#inspectObject

p response #=> “SNP/1.1/0/OK/456”



31
# File 'lib/snarl/snp/response.rb', line 31

def inspect ; @response.chomp ; end

#ok?Boolean

Returns:

  • (Boolean)


33
# File 'lib/snarl/snp/response.rb', line 33

def ok? ; code.to_i.zero? ; end

#parse_responseObject

Raises:



18
19
20
21
22
23
24
25
26
# File 'lib/snarl/snp/response.rb', line 18

def parse_response
  if  ResponseParseRegexp =~ @response.chomp then
    @code = $1
    @message, @infomation= $2.split(/\//)
  else
    raise Snarl::SNP::Error::RUBYSNARL_UNKNOWN_RESPONSE.new(self, nil)
  end
  raise error.new(self, nil) unless ok?
end

#request_strObject



34
# File 'lib/snarl/snp/response.rb', line 34

def request_str ; request ? request.to_s : nil ; end

#to_sObject

puts response #=> “0”



30
# File 'lib/snarl/snp/response.rb', line 30

def to_s    ; code ; end