class Dropbox::WebClient::ResponseParser

Attributes

response_text[R]

Public Class Methods

new(response_text, response_type = :json, error_type = :json) click to toggle source
# 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

Private Class Methods

parse_html(text) click to toggle source
# File lib/dropbox/web_client/response_parser.rb, line 52
def self.parse_html(text)
  doc = Nokogiri::HTML(text)
  result = {}

  members = doc.css("#sf-members .bs-row")
  if members.size > 0
    result[:members] = members.map do |member|
      {
        :email => member.css("a[href^=mailto]").text,
        :name => (member.css(".sf-tooltip-name").children[0].text.strip rescue nil),
        :access => (member.css(".sf-can-edit-text").text rescue nil),
        :joined => member.css("> .sf-name > em").text == "(pending)" ? "Still waiting" : "Joined"
      }
    end
  end
  
  # Parse ns_id
  result[:ns_id] = doc.css("[data-ns-id]").first.attr("data-ns-id") rescue nil

  result
end
parse_json(text) click to toggle source
# File lib/dropbox/web_client/response_parser.rb, line 44
def self.parse_json(text)
  JSON.parse(text)
end
parse_share_options(text) click to toggle source

Parses a ‘share_options` response

# File lib/dropbox/web_client/response_parser.rb, line 79
def self.parse_share_options(text)
  json = parse_json(text)
  format, html, el = json["actions"].first
  parse_html(html)
end
parse_text(text) click to toggle source
# File lib/dropbox/web_client/response_parser.rb, line 48
def self.parse_text(text)
  text
end

Public Instance Methods

error?() click to toggle source
# File lib/dropbox/web_client/response_parser.rb, line 14
def error?
  @response_text.start_with? "err:"
end
error_data() click to toggle source
# File lib/dropbox/web_client/response_parser.rb, line 18
def error_data
  JSON.parse(@response_text.split(":", 2)[1]) if error?
end
response_data() click to toggle source
# 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