Class: PatientZero::Source

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/patient_zero/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Client

#connection, #get, included, #parse, #post, #put

Constructor Details

#initialize(attributes) ⇒ Source

Returns a new instance of Source.



7
8
9
10
11
12
13
14
15
# File 'lib/patient_zero/source.rb', line 7

def initialize attributes
  @id = attributes.fetch 'id'
  @name = attributes.fetch 'name'
  @invalid = attributes.fetch 'is_invalid'
  @tracked = attributes.fetch 'is_tracked'
  @platform = attributes.fetch 'platform'
  @delete_id = attributes.fetch 'delete_id'
  @token = attributes.fetch 'token'
end

Instance Attribute Details

#delete_idObject

Returns the value of attribute delete_id.



5
6
7
# File 'lib/patient_zero/source.rb', line 5

def delete_id
  @delete_id
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/patient_zero/source.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/patient_zero/source.rb', line 5

def name
  @name
end

#platformObject

Returns the value of attribute platform.



5
6
7
# File 'lib/patient_zero/source.rb', line 5

def platform
  @platform
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/patient_zero/source.rb', line 5

def token
  @token
end

Class Method Details

.all(token = Authorization.token) ⇒ Object



17
18
19
20
21
22
# File 'lib/patient_zero/source.rb', line 17

def self.all token=Authorization.token
  response = get '/mobile/api/v1/sources/', client_token: token
  response['sources'].map do |source_attributes|
    new source_attributes.merge('token' => token)
  end
end

.creation_url(platform, token = Authorization.token, reference_id = nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/patient_zero/source.rb', line 31

def self.creation_url platform, token=Authorization.token, reference_id=nil
  response = connection.get("/mobile/api/v1/sources/#{platform}/authenticate", client_token: token, reference_id: reference_id)
  creation_url = response.headers.fetch 'location'
  raise InvalidPlatformError, creation_url.split('error=').last if creation_url.include? 'error'
  creation_url
end

.find(source_id, token) ⇒ Object



24
25
26
27
28
29
# File 'lib/patient_zero/source.rb', line 24

def self.find source_id, token
  response = get '/mobile/api/v1/sources/show/', id: source_id, client_token: token
  new response['source'].merge('token' => token)
rescue Error => e
  raise NotFoundError, e
end

Instance Method Details

#analytics(start_date: nil, end_date: nil) ⇒ Object



61
62
63
64
# File 'lib/patient_zero/source.rb', line 61

def analytics start_date: nil, end_date: nil
  @analytics ||= {}
  @analytics["#{start_date}#{end_date}"] ||= Analytics.for_platform platform, token: token, source_id: id, start_date: start_date, end_date: end_date
end

#childrenObject



54
55
56
57
58
59
# File 'lib/patient_zero/source.rb', line 54

def children
  response = get '/mobile/api/v1/sources/linked_sources', social_object_uid: id, client_token: token
  response['linked_sources'].map do |source_attributes|
    Source.new(source_attributes.merge 'token' => token) unless source_attributes["id"] == id
  end.compact
end

#parentObject



46
47
48
49
50
51
52
# File 'lib/patient_zero/source.rb', line 46

def parent
  if delete_id == id
    self
  else
    Source.find delete_id, token
  end
end

#platform_idObject



38
39
40
# File 'lib/patient_zero/source.rb', line 38

def platform_id
  id.split('#').last
end

#social_typeObject



42
43
44
# File 'lib/patient_zero/source.rb', line 42

def social_type
  id.scan(/\d+#(\w+)#\d+/).flatten.first
end