Sha256: 06b67db7bc62175368131c868ee7c4066fd82a2b1419030d97d9779fe091d390

Contents?: true

Size: 844 Bytes

Versions: 1

Compression:

Stored size: 844 Bytes

Contents

module Leadsquared
  class ApiConnection
    attr_reader :connection

    def initialize(service)
      @connection = Leadsquared::Client.new
      @service = service
    end

    protected

    def url_with_service(action)
      @service + action
    end

    def handle_response(response)
      case response.status
      when 200
        return JSON.parse response.body
      when 400
        raise InvalidRequestError.new("Bad Request")
      when 401
        raise InvalidRequestError.new("Unauthorized Request")
      when 404
        raise InvalidRequestError.new("API Not Found")
      when 500
        message = response.body.try(:[],  "ExceptionMessage")
        raise InvalidRequestError.new("Internal Error: #{message}")
      else
        raise InvalidRequestError.new("Unknown Error#{response.body}")
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
leadsquared-0.2.0 lib/leadsquared/api_connection.rb