Class: Dropbox::WebClient::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox/web_client/response_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_text, response_type = :json, error_type = :json) ⇒ ResponseParser

Returns a new instance of ResponseParser.



7
8
9
10
11
12
# File 'lib/dropbox/web_client/response_parser.rb', line 7

def initialize(response_text, response_type = :json, error_type = :json)
  @response_text = response_text
  
  @error_type = error_type
  @response_type = response_type
end

Instance Attribute Details

#response_textObject (readonly)

Returns the value of attribute response_text.



5
6
7
# File 'lib/dropbox/web_client/response_parser.rb', line 5

def response_text
  @response_text
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/dropbox/web_client/response_parser.rb', line 14

def error?
  @response_text.start_with? "err:"
end

#error_dataObject



18
19
20
# File 'lib/dropbox/web_client/response_parser.rb', line 18

def error_data
  JSON.parse(@response_text.split(":", 2)[1]) if error?
end

#response_dataObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dropbox/web_client/response_parser.rb', line 30

def response_data
  if error?
    Exception.new(error_data)
  else
    begin
      self.class.send("parse_#{@response_type.to_s}", @response_text)
    rescue NoMethodError
      raise Exception.new("Unsupported response format")
    end
  end
end