Sha256: e9b462736ee22d714cc048ee3b61603125679e8c7da773fef2c75baf3192ed7d

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'

describe Trakio do

  subject { Trakio }

  after {
    Trakio.default_instance = nil
  }

  describe '#track' do

    context "when an error is returned by the API" do
      it "raises an exception" do
        stub = stub_request(:post, "https://api.trak.io/v1/track").
          with(:body => {
            token: 'my_api_token',
            data: {
              time: /.+/,
              distinct_id: 'user@example.com',
              event: 'my-event'
            }
          }).to_return(:body => {
            status: 'error',
            code: 401,
            exception: "InvalidToken",
            message: "Missing or invalid API token.",
            details: "You must provide a valid API token, see http://docs.trak.io/authentication.html."
          }.to_json)

        trakio = Trakio.new 'my_api_token'
        expect {
          trakio.track distinct_id: 'user@example.com', event: 'my-event'
        }.to raise_error Trakio::Exceptions::InvalidToken

        expect(stub).to have_been_requested

      end
    end

    context "when no error is returned by the API" do
      it "returns the result" do
        stub = stub_request(:post, "https://api.trak.io/v1/track").
          with(:body => {
            token: 'my_api_token',
            data: {
              time: /.+/,
              distinct_id: 'user@example.com',
              event: 'my-event'
            }
          }).to_return(:body => {
            status: 'success',
            trak_id: '1234567890'
          }.to_json)

        trakio = Trakio.new 'my_api_token'
        resp = trakio.track distinct_id: 'user@example.com', event: 'my-event'

        expect(resp[:status]).to eql 'success'
        expect(resp[:trak_id]).to eql '1234567890'

        expect(stub).to have_been_requested
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trakio-ruby-0.2.2 spec/trakio/exception_spec.rb
trakio-ruby-0.2.1 spec/trakio/exception_spec.rb