Class: AmplitudeAnalytics::Response

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

Overview

Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: HttpStatus::UNKNOWN, body: nil) ⇒ Response



32
33
34
35
36
# File 'lib/amplitude/http_client.rb', line 32

def initialize(status: HttpStatus::UNKNOWN, body: nil)
  @status = status
  @code = status.value
  @body = body || {}
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



30
31
32
# File 'lib/amplitude/http_client.rb', line 30

def body
  @body
end

#codeObject

Returns the value of attribute code.



30
31
32
# File 'lib/amplitude/http_client.rb', line 30

def code
  @code
end

#statusObject

Returns the value of attribute status.



30
31
32
# File 'lib/amplitude/http_client.rb', line 30

def status
  @status
end

Class Method Details

.get_status(code) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/amplitude/http_client.rb', line 109

def self.get_status(code)
  case code
  when 200..299
    HttpStatus::SUCCESS
  when 429
    HttpStatus::TOO_MANY_REQUESTS
  when 413
    HttpStatus::PAYLOAD_TOO_LARGE
  when 408
    HttpStatus::TIMEOUT
  when 400..499
    HttpStatus::INVALID_REQUEST
  when 500..Float::INFINITY
    HttpStatus::FAILED
  else
    HttpStatus::UNKNOWN
  end
end

Instance Method Details

#errorObject



65
66
67
# File 'lib/amplitude/http_client.rb', line 65

def error
  @body['error'] if @body.key?('error')
end

#events_with_invalid_fieldsObject



73
74
75
# File 'lib/amplitude/http_client.rb', line 73

def events_with_invalid_fields
  @body['events_with_invalid_fields'] if @body.key?('events_with_invalid_fields')
end

#events_with_invalid_id_lengthsObject



81
82
83
# File 'lib/amplitude/http_client.rb', line 81

def events_with_invalid_id_lengths
  @body['events_with_invalid_id_lengths'] if @body.key?('events_with_invalid_id_lengths')
end

#events_with_missing_fieldsObject



77
78
79
# File 'lib/amplitude/http_client.rb', line 77

def events_with_missing_fields
  @body['events_with_missing_fields'] if @body.key?('events_with_missing_fields')
end

#exceed_daily_quota(event) ⇒ Object



93
94
95
96
97
98
# File 'lib/amplitude/http_client.rb', line 93

def exceed_daily_quota(event)
  return true if @body.key?('exceeded_daily_quota_users') && @body['exceeded_daily_quota_users'].include?(event.user_id)
  return true if @body.key?('exceeded_daily_quota_devices') && @body['exceeded_daily_quota_devices'].include?(event.device_id)

  false
end

#get_status(code) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/amplitude/http_client.rb', line 46

def get_status(code)
  case code
  when 200
    HttpStatus::SUCCESS
  when 400
    HttpStatus::INVALID_REQUEST
  when 408
    HttpStatus::TIMEOUT
  when 413
    HttpStatus::PAYLOAD_TOO_LARGE
  when 429
    HttpStatus::TOO_MANY_REQUESTS
  when 500
    HttpStatus::FAILED
  else
    HttpStatus::UNKNOWN
  end
end

#invalid_or_silenced_indexObject



100
101
102
103
104
105
106
107
# File 'lib/amplitude/http_client.rb', line 100

def invalid_or_silenced_index
  result = Set.new
  result.merge(@body['events_with_missing_fields'].values.flatten) if @body.key?('events_with_missing_fields')
  result.merge(@body['events_with_invalid_fields'].values.flatten) if @body.key?('events_with_invalid_fields')
  result.merge(@body['events_with_invalid_id_lengths'].values.flatten) if @body.key?('events_with_invalid_id_lengths')
  result.merge(@body['silenced_events']) if @body.key?('silenced_events')
  result.to_a
end

#missing_fieldObject



69
70
71
# File 'lib/amplitude/http_client.rb', line 69

def missing_field
  @body['missing_field'] if @body.key?('missing_field')
end

#parse(res) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/amplitude/http_client.rb', line 38

def parse(res)
  res_body = JSON.parse(res.body)
  @code = res_body['code']
  @status = get_status(@code)
  @body = res_body
  self
end

#silenced_eventsObject



85
86
87
# File 'lib/amplitude/http_client.rb', line 85

def silenced_events
  @body['silenced_events'] if @body.key?('silenced_events')
end

#throttled_eventsObject



89
90
91
# File 'lib/amplitude/http_client.rb', line 89

def throttled_events
  @body['throttled_events'] if @body.key?('throttled_events')
end