Sha256: c7f703cf5775daecf0a1128a138697621be5642d56955a477c54ca5b5bd34d57

Contents?: true

Size: 846 Bytes

Versions: 4

Compression:

Stored size: 846 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

4 entries across 4 versions & 1 rubygems

Version Path
leadsquared-0.5.0 lib/leadsquared/api_connection.rb
leadsquared-0.4.0 lib/leadsquared/api_connection.rb
leadsquared-0.3.1 lib/leadsquared/api_connection.rb
leadsquared-0.3.0 lib/leadsquared/api_connection.rb