Class: FyipeTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/fyipeTracker.rb

Instance Method Summary collapse

Constructor Details

#initialize(apiUrl, errorTrackerId, errorTrackerKey, options = []) ⇒ FyipeTracker

FyipeTracker constructor.

Parameters:

  • string

    apiUrl

  • string

    errorTrackerId

  • string

    errorTrackerKey



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fyipeTracker.rb', line 12

def initialize(apiUrl, errorTrackerId, errorTrackerKey, options = [])
    # instance variable intialzation
    @configKeys = ['baseUrl']
    @MAX_ITEMS_ALLOWED_IN_STACK = 100
    @errorTrackerId = errorTrackerId
    setApiUrl(apiUrl) 
    @errorTrackerKey = errorTrackerKey
    @tags = []
    @fingerprint = []
    @options = {
        'maxTimeline': 5,
        'captureCodeSnippet': true
    }
    setUpOptions(options)
    @util = Util.new(@options)
    setEventId()
    @listenerObj = FyipeListener.new(getEventId(), @options)
    @apiTransport = FyipeTransport.new(@apiUrl)
    setUpExceptionHandlerListener()
    
end

Instance Method Details

#addToTimeline(category, content, type) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/fyipeTracker.rb', line 79

def addToTimeline(category, content, type)
    timelineObj =  {}
    timelineObj["category"] = category
    timelineObj["data"] = content
    timelineObj["type"] = type

    @listenerObj.logCustomTimelineEvent(timelineObj)
end

#captureException(exception) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fyipeTracker.rb', line 220

def captureException(exception)

    # construct the error object
    exceptionObj = @util.getExceptionStackTrace(exception)

    # set the a handled tag
    setTag('handled', 'true')

    prepareErrorObject('exception', exceptionObj)

    # send to the server
    return sendErrorEventToServer()
end

#captureMessage(message) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fyipeTracker.rb', line 161

def captureMessage( message)
    # set the a handled tag
    setTag('handled', 'true')
    messageObj = {}
    messageObj["message"] = message 

    prepareErrorObject('message', messageObj);

    # send to the server
    return sendErrorEventToServer()
end

#clear(newEventId) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/fyipeTracker.rb', line 244

def clear(newEventId)
    # clear tags
    @tags = []
    # clear fingerprint
    @fingerprint = []
    # clear timeline
    @listenerObj.clearTimeline(newEventId)
end

#getCurrentEventObject



216
217
218
# File 'lib/fyipeTracker.rb', line 216

def getCurrentEvent()
    return @event
end

#getEventIdObject



75
76
77
# File 'lib/fyipeTracker.rb', line 75

def getEventId()
    return @eventId
end

#getFingerprint(errorMessage) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/fyipeTracker.rb', line 150

def getFingerprint(errorMessage)

    # if no fingerprint exist currently
    if (@fingerprint.length() < 1)
        # set up finger print based on error since none exist
        setFingerPrint(errorMessage)
    end
    
    return @fingerprint
end

#getSDKDetailsObject



253
254
255
256
257
258
259
260
261
# File 'lib/fyipeTracker.rb', line 253

def getSDKDetails()    
    # default sdk details
    sdkDetail = {}
    sdkDetail["name"] = Fyipe::NAME
    sdkDetail["version"] = Fyipe::VERSION

    
    return sdkDetail
end

#getTagsObject



129
130
131
# File 'lib/fyipeTracker.rb', line 129

def getTags()
    return @tags
end

#getTimelineObject



88
89
90
# File 'lib/fyipeTracker.rb', line 88

def getTimeline()
    return @listenerObj.getTimeline()
end

#manageErrorObject(exception) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/fyipeTracker.rb', line 180

def manageErrorObject(exception)

    # construct the error object
    errorObj = @utilObj.getExceptionStackTrace(exception);
    
    # set the a handled tag
    setTag('handled', 'false');
    # prepare to send to server
    prepareErrorObject('error', errorObj);

    # send to the server
    return sendErrorEventToServer();
end

#prepareErrorObject(eventType, errorStackTrace) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fyipeTracker.rb', line 193

def prepareErrorObject(eventType, errorStackTrace) 
    # set a last timeline as the error message
    @listenerObj.logErrorEvent(errorStackTrace["message"], eventType)
    
    # get current timeline
    timeline = getTimeline()
    
    tags = getTags()
    fingerprint = getFingerprint(errorStackTrace["message"]) # default fingerprint will be the message from the error stacktrace
    # get event ID
    # Temporary display the state of the error stack, timeline and device details when an error occur
    # prepare the event so it can be sent to the server
    @event = {}
    @event["type"] = eventType
    @event["timeline"]= timeline
    @event["exception"]= errorStackTrace
    @event["eventId"]= getEventId()
    @event["tags"]= tags
    @event["fingerprint"]= fingerprint
    @event["errorTrackerKey"]= @errorTrackerKey
    @event["sdk"]= getSDKDetails()
end

#sendErrorEventToServerObject



233
234
235
236
237
238
239
240
241
242
# File 'lib/fyipeTracker.rb', line 233

def sendErrorEventToServer()
    response = nil
    # send to API properly
    response = @apiTransport.sendErrorEventToServer(@event)
    # generate a new event Id
    setEventId()
    # clear the timeline after a successful call to the server
    clear(getEventId())
    return response
end

#setApiUrl(apiUrl) ⇒ Object



34
35
36
# File 'lib/fyipeTracker.rb', line 34

def setApiUrl(apiUrl)
    @apiUrl = apiUrl + '/error-tracker/' + @errorTrackerId + '/track';
end

#setEventIdObject



71
72
73
# File 'lib/fyipeTracker.rb', line 71

def setEventId()
    @eventId = @util.v4()
end

#setFingerPrint(key) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fyipeTracker.rb', line 133

def setFingerPrint(key)
    # get data type of the passed key
    keyClassType = key.class.to_s

    # routine check
    if (keyClassType != "String" && keyClassType != "Array")
        raise "Invalid Fingerprint"
    end

    fingerprint = key
    if (keyClassType == "String")
        fingerprint = [key]
    end
    
    @fingerprint = fingerprint
end

#setTag(key, value) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fyipeTracker.rb', line 92

def setTag(key, value)
    if (!((key.class.to_s.eql? "String") || (value.class.to_s.eql? "String")))
        raise "Invalid Tag"
    end
    
    exist = false
    @tags.each do |tag|
        if(tag['key'].to_s.eql? key)
            # set the found flag
            exist = true
            # replace value if it exist
            tag['value'] = value
            break
        end
    end
    if(!exist)
        # push key and value if it doesnt
        tag = {}
        tag['key'] = key
        tag['value'] = value
        @tags.append(tag)
    end
end

#setTags(tags) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fyipeTracker.rb', line 116

def setTags(tags)

    if (!(tags.class.to_s.eql? "Array"))
        raise "Invalid Tags"
    end
    
    tags.each do |tag|
        if(tag[:key] != nil && tag[:value] != nil)
            setTag(tag[:key], tag[:value])
        end
    end
end

#setUpExceptionHandlerListenerObject



173
174
175
176
177
178
179
# File 'lib/fyipeTracker.rb', line 173

def setUpExceptionHandlerListener()
    # start listener
    at_exit do
        manageErrorObject($!) if $!         
    end
    
end

#setUpOptions(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fyipeTracker.rb', line 38

def setUpOptions(options)
    # set up options
    if(options.class.to_s != "Hash")
        return # ignore passed options if it is not an object
    end

    options.each do |key, value|
        # proceed with current key if it is not in the config keys
         if (!(@configKeys.include? key))
            # if key is allowed in options
            if (@options[key] != nil)
                # set max timeline properly after hecking conditions
                if key.to_s == 'maxTimeline'
                    allowedValue = value
                    if value > @MAX_ITEMS_ALLOWED_IN_STACK or value < 1
                        allowedValue = @MAX_ITEMS_ALLOWED_IN_STACK
                    end
                    
                    @options[key] = allowedValue
                elsif key.to_s == 'captureCodeSnippet'
                        defaultVal = true
                        # set boolean value if boolean or set default `true` if anything other than boolean is passed
                        if [true, false].include? value # since there is no Boolean class in Ruby
                            defaultVal = value
                        end
                        @options[key] = defaultVal
                end
            end

         end
    end
end