lib/flexmls_api/request.rb in flexmls_api-0.6.5 vs lib/flexmls_api/request.rb in flexmls_api-0.7.0

- old
+ new

@@ -1,11 +1,10 @@ require 'cgi' module FlexmlsApi # HTTP request wrapper. Performs all the api session mumbo jumbo so that the models don't have to. module Request - include PaginateResponse # Perform an HTTP GET request # # * path - Path of an api resource, excluding version and endpoint (domain) information # * options - Resource request options as specified being supported via and api resource # :returns: @@ -87,80 +86,11 @@ end # No luck authenticating... KABOOM! FlexmlsApi.logger.error("Authentication failed or server is sending us expired tokens, nothing we can do here.") raise end - results = response.body.results - paging = response.body.pagination - unless paging.nil? - if request_opts[:_pagination] == "count" - results = paging['TotalRows'] - else - results = paginate_response(results, paging) - end - end - results + response.body end end - # All known response codes listed in the API - module ResponseCodes - NOT_FOUND = 404 - METHOD_NOT_ALLOWED = 405 - INVALID_KEY = 1000 - DISABLED_KEY = 1010 - API_USER_REQUIRED = 1015 - SESSION_TOKEN_EXPIRED = 1020 - SSL_REQUIRED = 1030 - INVALID_JSON = 1035 - INVALID_FIELD = 1040 - MISSING_PARAMETER = 1050 - INVALID_PARAMETER = 1053 - CONFLICTING_DATA = 1055 - NOT_AVAILABLE= 1500 - RATE_LIMIT_EXCEEDED = 1550 - end - - # Errors built from API responses - class InvalidResponse < StandardError; end - class ClientError < StandardError - attr_reader :code, :status - def initialize (options = {}) - # Support the standard initializer for errors - opts = options.is_a?(Hash) ? options : {:message => options.to_s} - @code = opts[:code] - @status = opts[:status] - super(opts[:message]) - end - - end - class NotFound < ClientError; end - class PermissionDenied < ClientError; end - class NotAllowed < ClientError; end - class BadResourceRequest < ClientError; end - - # Nice and handy class wrapper for the api response hash - class ApiResponse - attr_accessor :code, :message, :results, :success, :pagination - def initialize(d) - begin - hash = d["D"] - if hash.nil? || hash.empty? - raise InvalidResponse, "The server response could not be understood" - end - self.message = hash["Message"] - self.code = hash["Code"] - self.results = hash["Results"] - self.success = hash["Success"] - self.pagination = hash["Pagination"] - rescue Exception => e - FlexmlsApi.logger.error "Unable to understand the response! #{d}" - raise - end - end - def success? - @success - end - end - end