Class: Medic::Store

Inherits:
Object
  • Object
show all
Includes:
HKConstants, Types, Units
Defined in:
lib/medic/store.rb

Constant Summary

Constants included from HKConstants

HKConstants::AUTHORIZATION_STATUSES, HKConstants::BIOLOGICAL_SEXES, HKConstants::BLOOD_TYPES, HKConstants::ERROR_CODES, HKConstants::SLEEP_ANALYSES, HKConstants::UPDATE_FREQUENCIES

Constants included from Types

Types::TYPE_IDENTIFIERS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HKConstants

#authorization_status, #error_code, #sleep_analysis, #update_frequency

Methods included from Units

#sample_unit

Methods included from Types

#object_type, #type_identifier

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/medic/store.rb', line 22

def self.available?
  HKHealthStore.isHealthDataAvailable
end

.hk_storeObject



13
14
15
# File 'lib/medic/store.rb', line 13

def self.hk_store
  @@hk_store
end

.sharedObject



7
8
9
10
11
# File 'lib/medic/store.rb', line 7

def self.shared
  Dispatch.once { @@hk_store ||= HKHealthStore.new }
  Dispatch.once { @medic_store ||= new }
  @medic_store
end

.unloadObject



17
18
19
20
# File 'lib/medic/store.rb', line 17

def self.unload
  @@hk_store = nil
  @medic_store = nil
end

Instance Method Details

#authorize(types, block = Proc.new) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/medic/store.rb', line 27

def authorize(types, block=Proc.new)
  share = Array(types[:share] || types[:write]).map{ |sym| object_type(sym) }
  read  = Array(types[:read]).map{ |sym| object_type(sym) }

  @@hk_store.requestAuthorizationToShareTypes(NSSet.setWithArray(share), readTypes: NSSet.setWithArray(read), completion: ->(success, error){
    block.call(success, error)
  })
end

#authorized?(sym) ⇒ Boolean Also known as: authorized_for?, is_authorized?, is_authorized_for?

Returns:

  • (Boolean)


36
37
38
# File 'lib/medic/store.rb', line 36

def authorized?(sym)
  @@hk_store.authorizationStatusForType(object_type(sym)) == HKAuthorizationStatusSharingAuthorized
end

#biological_sexObject



43
44
45
46
47
48
49
50
51
# File 'lib/medic/store.rb', line 43

def biological_sex
  error = Pointer.new(:object)
  sex = @@hk_store.biologicalSexWithError error
  if block_given?
    yield BIOLOGICAL_SEXES.invert[sex.biologicalSex], error[0]
  else
    BIOLOGICAL_SEXES.invert[sex.biologicalSex]
  end
end

#blood_typeObject



53
54
55
56
57
58
59
60
61
# File 'lib/medic/store.rb', line 53

def blood_type
  error = Pointer.new(:object)
  blood = @@hk_store.bloodTypeWithError error
  if block_given?
    yield BLOOD_TYPES.invert[blood.bloodType], error[0]
  else
    BLOOD_TYPES.invert[blood.bloodType]
  end
end

#date_of_birthObject



63
64
65
66
67
68
69
70
71
# File 'lib/medic/store.rb', line 63

def date_of_birth
  error = Pointer.new(:object)
  birthday = @@hk_store.dateOfBirthWithError error
  if block_given?
    yield birthday, error[0]
  else
    birthday
  end
end

#delete(hk_object, block = Proc.new) ⇒ Object Also known as: delete_object



73
74
75
76
77
# File 'lib/medic/store.rb', line 73

def delete(hk_object, block=Proc.new)
  @@hk_store.deleteObject(hk_object, withCompletion: ->(success, error){
    block.call(success, error)
  })
end

#disable_all_background_delivery(block = Proc.new) ⇒ Object



117
118
119
120
121
# File 'lib/medic/store.rb', line 117

def disable_all_background_delivery(block=Proc.new)
  @@hk_store.disableAllBackgroundDeliveryWithCompletion(->(success, error){
    block.call(success, error)
  })
end

#disable_background_delivery(type, block = Proc.new) ⇒ Object Also known as: disable_background_delivery_for



109
110
111
112
113
114
# File 'lib/medic/store.rb', line 109

def disable_background_delivery(type, block=Proc.new)
  return disable_all_background_delivery(block) if type == :all
  @@hk_store.disableBackgroundDeliveryForType(object_type(type), withCompletion: ->(success, error){
    block.call(success, error)
  })
end

#enable_background_delivery(type, frequency, block = Proc.new) ⇒ Object Also known as: enable_background_delivery_for



102
103
104
105
106
# File 'lib/medic/store.rb', line 102

def enable_background_delivery(type, frequency, block=Proc.new)
  @@hk_store.enableBackgroundDeliveryForType(object_type(type), frequency: update_frequency(frequency), withCompletion: ->(success, error){
    block.call(success, error)
  })
end

#execute(query) ⇒ Object Also known as: execute_query

TODO: workout support addSamples:toWorkout:completion:



92
93
94
# File 'lib/medic/store.rb', line 92

def execute(query)
  @@hk_store.executeQuery(query)
end

#save(hk_objects, block = Proc.new) ⇒ Object Also known as: save_object, save_objects



80
81
82
83
84
85
# File 'lib/medic/store.rb', line 80

def save(hk_objects, block=Proc.new)
  objs_array = hk_objects.is_a?(Array) ? hk_objects : [hk_objects]
  @@hk_store.saveObjects(objs_array.map{|obj| prepare_for_save(obj)}, withCompletion: ->(success, error){
    block.call(success, error)
  })
end

#stop(query) ⇒ Object Also known as: stop_query



97
98
99
# File 'lib/medic/store.rb', line 97

def stop(query)
  @@hk_store.stopQuery(query)
end