validic 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.rspec +0 -1
  4. data/Gemfile +12 -1
  5. data/Gemfile.lock +32 -62
  6. data/README.md +442 -28
  7. data/lib/validic.rb +3 -20
  8. data/lib/validic/app.rb +11 -0
  9. data/lib/validic/biometrics.rb +9 -0
  10. data/lib/validic/client.rb +35 -77
  11. data/lib/validic/diabetes.rb +4 -60
  12. data/lib/validic/error.rb +33 -0
  13. data/lib/validic/extra.rb +10 -0
  14. data/lib/validic/fitness.rb +4 -57
  15. data/lib/validic/nutrition.rb +5 -61
  16. data/lib/validic/organization.rb +6 -13
  17. data/lib/validic/profile.rb +4 -39
  18. data/lib/validic/response.rb +15 -0
  19. data/lib/validic/rest/apps.rb +21 -0
  20. data/lib/validic/rest/biometrics.rb +39 -0
  21. data/lib/validic/rest/diabetes.rb +39 -0
  22. data/lib/validic/rest/fitness.rb +40 -0
  23. data/lib/validic/rest/nutrition.rb +40 -0
  24. data/lib/validic/rest/organizations.rb +12 -0
  25. data/lib/validic/rest/profile.rb +18 -0
  26. data/lib/validic/rest/request.rb +95 -0
  27. data/lib/validic/rest/routine.rb +40 -0
  28. data/lib/validic/rest/sleep.rb +41 -0
  29. data/lib/validic/rest/tobacco_cessation.rb +40 -0
  30. data/lib/validic/rest/users.rb +55 -0
  31. data/lib/validic/rest/utils.rb +28 -0
  32. data/lib/validic/rest/weight.rb +43 -0
  33. data/lib/validic/routine.rb +4 -55
  34. data/lib/validic/sleep.rb +4 -59
  35. data/lib/validic/summary.rb +18 -0
  36. data/lib/validic/tobacco_cessation.rb +4 -53
  37. data/lib/validic/user.rb +4 -99
  38. data/lib/validic/utils.rb +13 -0
  39. data/lib/validic/version.rb +1 -1
  40. data/lib/validic/weight.rb +4 -57
  41. data/spec/fixtures/apps.json +32 -0
  42. data/spec/fixtures/biometrics-expanded.json +56 -0
  43. data/spec/fixtures/biometrics-extras.json +35 -0
  44. data/spec/fixtures/biometrics_record.json +36 -0
  45. data/spec/fixtures/biometrics_records.json +54 -0
  46. data/spec/fixtures/bulk_biometrics_records.json +55 -0
  47. data/spec/fixtures/bulk_diabetes_records.json +37 -0
  48. data/spec/fixtures/bulk_fitnesses.json +35 -0
  49. data/spec/fixtures/bulk_nutritions.json +37 -0
  50. data/spec/fixtures/bulk_sleeps.json +35 -0
  51. data/spec/fixtures/bulk_tobacco_cessations.json +33 -0
  52. data/spec/fixtures/conflict.json +5 -0
  53. data/spec/fixtures/diabetes-expanded.json +38 -0
  54. data/spec/fixtures/diabetes-extras.json +17 -0
  55. data/spec/fixtures/diabetes_record.json +18 -0
  56. data/spec/fixtures/diabetes_records.json +36 -0
  57. data/spec/fixtures/fitness-expanded.json +1521 -0
  58. data/spec/fixtures/fitness-extras.json +15 -0
  59. data/spec/fixtures/fitness.json +16 -0
  60. data/spec/fixtures/fitnesses.json +34 -0
  61. data/spec/fixtures/forbidden.json +5 -0
  62. data/spec/fixtures/internal_server.json +4 -0
  63. data/spec/fixtures/me.json +5 -0
  64. data/spec/fixtures/not_found.json +5 -0
  65. data/spec/fixtures/nutrition-expanded.json +123 -0
  66. data/spec/fixtures/nutrition-extras.json +13 -0
  67. data/spec/fixtures/nutrition.json +18 -0
  68. data/spec/fixtures/nutritions-expanded.json +123 -0
  69. data/spec/fixtures/nutritions.json +36 -0
  70. data/spec/fixtures/organizations.json +27 -0
  71. data/spec/fixtures/profile.json +13 -0
  72. data/spec/fixtures/refresh_token.json +7 -0
  73. data/spec/fixtures/routine-expanded.json +749 -0
  74. data/spec/fixtures/routine-extras.json +14 -0
  75. data/spec/fixtures/routine.json +17 -0
  76. data/spec/fixtures/routines.json +47 -0
  77. data/spec/fixtures/sleep-expanded.json +51 -0
  78. data/spec/fixtures/sleep-extras.json +15 -0
  79. data/spec/fixtures/sleep.json +16 -0
  80. data/spec/fixtures/sleeps.json +48 -0
  81. data/spec/fixtures/synced_apps.json +43 -0
  82. data/spec/fixtures/tobacco_cessation-expanded.json +33 -0
  83. data/spec/fixtures/tobacco_cessation-extras.json +12 -0
  84. data/spec/fixtures/tobacco_cessation.json +14 -0
  85. data/spec/fixtures/tobacco_cessations.json +32 -0
  86. data/spec/fixtures/unauthorized.json +4 -0
  87. data/spec/fixtures/unprocessable_entity.json +5 -0
  88. data/spec/fixtures/updated_user.json +9 -0
  89. data/spec/fixtures/user.json +8 -0
  90. data/spec/fixtures/user_with_profile.json +14 -0
  91. data/spec/fixtures/users.json +23 -0
  92. data/spec/fixtures/weight-expanded.json +66 -0
  93. data/spec/fixtures/weight-extras.json +15 -0
  94. data/spec/fixtures/weight.json +18 -0
  95. data/spec/fixtures/weights.json +48 -0
  96. data/spec/spec_helper.rb +47 -33
  97. data/spec/validic/client_spec.rb +5 -38
  98. data/spec/validic/error_spec.rb +73 -0
  99. data/spec/validic/expanded_spec.rb +87 -0
  100. data/spec/validic/rest/apps_spec.rb +41 -0
  101. data/spec/validic/rest/biometrics_spec.rb +131 -0
  102. data/spec/validic/rest/diabetes_spec.rb +131 -0
  103. data/spec/validic/rest/fitness_spec.rb +131 -0
  104. data/spec/validic/rest/nutrition_spec.rb +144 -0
  105. data/spec/validic/rest/organizations_spec.rb +24 -0
  106. data/spec/validic/rest/profile_spec.rb +44 -0
  107. data/spec/validic/rest/routine_spec.rb +129 -0
  108. data/spec/validic/rest/sleep_spec.rb +148 -0
  109. data/spec/validic/rest/tobacco_cessation_spec.rb +131 -0
  110. data/spec/validic/rest/users_spec.rb +170 -0
  111. data/spec/validic/rest/weight_spec.rb +150 -0
  112. data/spec/validic/summary_spec.rb +3 -0
  113. data/spec/validic_spec.rb +19 -31
  114. data/validic.gemspec +0 -13
  115. metadata +174 -194
  116. data/lib/validic/biometric.rb +0 -97
  117. data/lib/validic/request.rb +0 -46
  118. data/lib/validic/third_party_app.rb +0 -32
  119. data/spec/validic/biometrics_spec.rb +0 -43
  120. data/spec/validic/diabetes_spec.rb +0 -89
  121. data/spec/validic/fitness_spec.rb +0 -85
  122. data/spec/validic/nutrition_spec.rb +0 -91
  123. data/spec/validic/organization_spec.rb +0 -26
  124. data/spec/validic/profile_spec.rb +0 -19
  125. data/spec/validic/routine_spec.rb +0 -82
  126. data/spec/validic/sleep_spec.rb +0 -86
  127. data/spec/validic/third_party_app_spec.rb +0 -32
  128. data/spec/validic/tobacco_cessation_spec.rb +0 -81
  129. data/spec/validic/user_spec.rb +0 -81
  130. data/spec/validic/weight_spec.rb +0 -88
@@ -0,0 +1,40 @@
1
+ require 'validic/tobacco_cessation'
2
+
3
+ module Validic
4
+ module REST
5
+ module TobaccoCessation
6
+
7
+ def get_tobacco_cessation(options = {})
8
+ resp = get_request(:tobacco_cessation, options)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_tobacco_cessations :get_tobacco_cessation
12
+
13
+ def create_tobacco_cessation(options = {})
14
+ user_id = options.delete(:user_id)
15
+ options = { user_id: user_id, tobacco_cessation: options }
16
+ response = post_request(:tobacco_cessation, options)
17
+ Validic::TobaccoCessation.new(response['tobacco_cessation'])
18
+ end
19
+
20
+ def update_tobacco_cessation(options = {})
21
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
22
+ options = { user_id: user_id, _id: _id, tobacco_cessation: options }
23
+ response = put_request(:tobacco_cessation, options)
24
+ Validic::TobaccoCessation.new(response['tobacco_cessation'])
25
+ end
26
+
27
+ def delete_tobacco_cessation(options = {})
28
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
29
+ options = { user_id: user_id, _id: _id }
30
+ delete_request(:tobacco_cessation, options)
31
+ true
32
+ end
33
+
34
+ def latest_tobacco_cessation(options = {})
35
+ resp = latest(:tobacco_cessation, options)
36
+ build_response_attr(resp)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,55 @@
1
+ require 'validic/user'
2
+
3
+ module Validic
4
+ module REST
5
+ module Users
6
+
7
+ def get_users(options = {})
8
+ resp = get_request(:users, options)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_user :get_users
12
+
13
+ def refresh_token(options = {})
14
+ response = get_request(:refresh_token, options)
15
+ Validic::User.new(response['user'])
16
+ end
17
+
18
+ def me(options = {})
19
+ response = get_request(:me, options)
20
+ response['me']['_id']
21
+ end
22
+
23
+ def provision_user(options = {})
24
+ response = post_request(:users, { user: options })
25
+ Validic::User.new(response['user'])
26
+ end
27
+ alias :user_provision :provision_user
28
+
29
+ def update_user(options = {})
30
+ user_id = options.delete(:user_id)
31
+ response = put_request(:users, { user_id: user_id, user: options })
32
+ Validic::User.new(response['user'])
33
+ end
34
+ alias :user_update :update_user
35
+
36
+ def delete_user(options = {})
37
+ delete_request(:users, { user_id: options[:user_id] })
38
+ true
39
+ end
40
+ alias :user_delete :delete_user
41
+
42
+ def suspend_user(options = {})
43
+ put_request(:users, { user_id: options[:user_id], suspend: '1' })
44
+ true
45
+ end
46
+ alias :user_suspend :suspend_user
47
+
48
+ def unsuspend_user(options = {})
49
+ put_request(:users, { user_id: options[:user_id], suspend: '0' })
50
+ true
51
+ end
52
+ alias :user_unsuspend :unsuspend_user
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,28 @@
1
+ require 'validic/response'
2
+ require 'validic/summary'
3
+
4
+ module Validic
5
+ module REST
6
+ module Utils
7
+ private
8
+
9
+ def build_response_attr(resp)
10
+ summary = Validic::Summary.new(resp.delete("summary"))
11
+ Validic::Response.new(summary, response_object(resp))
12
+ end
13
+
14
+ def response_object(resp)
15
+ return resp.values.first if resp.values.first.is_a? Hash
16
+ klass = Validic.const_get(camelize_response_key(resp))
17
+ resp.values.flatten.collect { |obj| klass.new(obj) }
18
+ end
19
+
20
+ def camelize_response_key(resp)
21
+ key = resp.keys.first
22
+ key = key.include?('_') ? key.split('_').map(&:capitalize).join : key.capitalize
23
+ key.chop! if %w(Users Apps).include?(key) #strip last letter off to match ::User or ::App
24
+ key
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,43 @@
1
+ require 'validic/weight'
2
+
3
+ module Validic
4
+ module REST
5
+ module Weight
6
+
7
+ def get_weight(params={})
8
+ resp = get_request(:weight, params)
9
+ build_response_attr(resp)
10
+ end
11
+
12
+ alias :get_weights :get_weight
13
+
14
+ def create_weight(options={})
15
+ user_id = options.delete(:user_id)
16
+ options = { user_id: user_id, weight: options }
17
+ resp = post_request(:weight, options)
18
+
19
+ Validic::Weight.new(resp['weight'])
20
+ end
21
+
22
+ def update_weight(options={})
23
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
24
+ options = { user_id: user_id, _id: _id, weight: options }
25
+ response = put_request(:weight, options)
26
+ Validic::Weight.new(response["weight"])
27
+ end
28
+
29
+ def delete_weight(options = {})
30
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
31
+ options = { user_id: user_id, _id: _id }
32
+ delete_request(:weight, options)
33
+ true
34
+ end
35
+
36
+ def latest_weight(options={})
37
+ resp = latest(:weight, options)
38
+ build_response_attr(resp)
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -1,59 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module Routine
5
-
6
- ##
7
- # Get Routine Activities base on `access_token`
8
- # Default data fetched is from yesterday
9
- #
10
- # @params :organization_id - for organization specific
11
- # @params :user_id - for user specific
12
- #
13
- # @params :start_date - optional
14
- # @params :end_date - optional
15
- # @params :access_token - override for default access_token
16
- # @params :source - optional - data per source (e.g 'fitbit')
17
- # @params :expanded - optional - will show the raw data
18
- #
19
- # @return [Hashie::Mash] with list of Routine
20
- def get_routine(params={})
21
- params = extract_params(params)
22
- get_endpoint(:routine, params)
2
+ class Routine
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
23
6
  end
24
-
25
- alias :get_routines :get_routine
26
-
27
- ##
28
- # Create Routine base on `access_token` and `authentication_token`
29
- #
30
- # @params :access_token - *required if not specified on your initializer / organization access_token
31
- # @params :authentication_token - *required / authentication_token of a specific user
32
- #
33
- # @params :steps
34
- # @params :stairs_climbed
35
- # @params :calories_burned
36
- # @params :calories_consumed
37
- # @params :timestamp
38
- # @params :source
39
- #
40
- # @return success
41
- def create_routine(options={})
42
- options = {
43
- access_token: options[:access_token],
44
- routine: {
45
- steps: options[:steps],
46
- stairs_climbed: options[:stairs_climbed],
47
- calories_burned: options[:calories_burned],
48
- calories_consumed: options[:calories_consumed],
49
- timestamp: options[:timestamp],
50
- source: options[:source]
51
- }
52
- }
53
-
54
- response = post("/#{Validic.api_version}/routine.json", options)
55
- response if response
56
- end
57
-
58
7
  end
59
8
  end
@@ -1,63 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module Sleep
5
-
6
- ##
7
- # Get Sleep Activities base on `access_token`
8
- # Default data fetched is from yesterday
9
- #
10
- # @params :organization_id - for organization specific
11
- # @params :user_id - for user specific
12
- #
13
- # @params :start_date - optional
14
- # @params :end_date - optional
15
- # @params :access_token - override for default access_token
16
- # @params :source - optional - data per source (e.g 'fitbit')
17
- # @params :expanded - optional - will show the raw data
18
- #
19
- # @return [Hashie::Mash] with list of Sleep
20
- def get_sleep(params={})
21
- params = extract_params(params)
22
- get_endpoint(:sleep, params)
2
+ class Sleep
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
23
6
  end
24
-
25
- alias :get_sleeps :get_sleep
26
-
27
- ##
28
- # Create Sleep base on `access_token` and `authentication_token`
29
- #
30
- # @params :access_token - *required if not specified on your initializer / organization access_token
31
- # @params :authentication_token - *required / authentication_token of a specific user
32
- #
33
- # @params :total_sleep
34
- # @params :awake
35
- # @params :deep
36
- # @params :light
37
- # @params :rem
38
- # @params :times_woken
39
- # @params :timestamp
40
- # @params :source
41
- #
42
- # @return success
43
- def create_sleep(options={})
44
- options = {
45
- access_token: options[:access_token],
46
- sleep: {
47
- total_sleep: options[:total_sleep],
48
- awake: options[:awake],
49
- deep: options[:deep],
50
- light: options[:light],
51
- rem: options[:rem],
52
- times_woken: options[:times_woken],
53
- timestamp: options[:timestamp],
54
- source: options[:source]
55
- }
56
- }
57
-
58
- response = post("/#{Validic.api_version}/sleep.json", options)
59
- response if response
60
- end
61
-
62
7
  end
63
8
  end
@@ -0,0 +1,18 @@
1
+ module Validic
2
+ class Summary
3
+ attr_reader :timestamp, :start_date, :end_date, :offset, :message,
4
+ :results, :limit, :previous, :next, :status
5
+ def initialize(sum_hash)
6
+ @timestamp = sum_hash["timestamp"]
7
+ @status = sum_hash["status"]
8
+ @offset = sum_hash["offset"]
9
+ @message = sum_hash["message"]
10
+ @results = sum_hash["results"]
11
+ @start_date = sum_hash["start_date"]
12
+ @end_date = sum_hash["end_date"]
13
+ @limit = sum_hash["limit"]
14
+ @previous = sum_hash["previous"]
15
+ @next = sum_hash["next"]
16
+ end
17
+ end
18
+ end
@@ -1,57 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module TobaccoCessation
5
-
6
- ##
7
- # Get TobaccoCessation Activities base on `access_token`
8
- # Default data fetched is from yesterday
9
- #
10
- # @params :organization_id - for organization specific
11
- # @params :user_id - for user specific
12
- #
13
- # @params :start_date - optional
14
- # @params :end_date - optional
15
- # @params :access_token - override for default access_token
16
- # @params :source - optional - data per source (e.g 'fitbit')
17
- # @params :expanded - optional - will show the raw data
18
- #
19
- # @return [Hashie::Mash] with list of TobaccoCessation
20
- def get_tobacco_cessations(params={})
21
- params = extract_params(params)
22
- get_endpoint(:tobacco_cessation, params)
2
+ class TobaccoCessation
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
23
6
  end
24
-
25
- ##
26
- # Create TobaccoCessation base on `access_token` and `authentication_token`
27
- #
28
- # @params :access_token - *required if not specified on your initializer / organization access_token
29
- # @params :authentication_token - *required / authentication_token of a specific user
30
- #
31
- # @params :cigarettes_allowed
32
- # @params :cigarettes_smoked
33
- # @params :cravings
34
- # @params :last_smoked
35
- # @params :timestamp
36
- # @params :source
37
- #
38
- # @return success
39
- def create_tobacco_cessation(options={})
40
- options = {
41
- access_token: options[:access_token],
42
- tobacco_cessation: {
43
- cigarettes_allowed: options[:cigarettes_allowed],
44
- cigarettes_smoked: options[:cigarettes_smoked],
45
- cravings: options[:cravings],
46
- last_smoked: options[:last_smoked],
47
- timestamp: options[:timestamp],
48
- source: options[:source]
49
- }
50
- }
51
-
52
- response = post("/#{Validic.api_version}/tobacco_cessation.json", options)
53
- response if response
54
- end
55
-
56
7
  end
57
8
  end
@@ -1,103 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module User
5
-
6
- ##
7
- # Get Users base on `access_token` and organization_id
8
- #
9
- # @params :status - optional (active or inactive) default :all
10
- # @params :access_token - optional
11
- # @params :start_date - optional
12
- # @params :end_date - optional
13
- # @params :offset - optional
14
- # @params :limit - optional
15
- #
16
- # @return [Hashie::Mash] with list of Organization
17
- def get_users(params={})
18
- params = extract_params(params)
19
- get_endpoint(:users, params)
20
- end
21
-
22
- ##
23
- # Get User id base on `access_token`
24
- #
25
- # @return id
26
- def me(options={})
27
- options = {
28
- access_token: options[:access_token]
29
- }
30
- response = get("/#{Validic.api_version}/me.json", options)
31
- response if response
32
- end
33
-
34
- ##
35
- # POST User info for provisioning base on `access_token`
36
- #
37
- # @params[:organization_id] -- organization_id -- String
38
- # @params[:access_token] -- organization's access_token -- String
39
- # @params[:uid] -- uid for the new user -- String
40
- # @params[:height] -- info for user -- Integer
41
- # @params[:weight] -- info for user -- String
42
- # @params[:location] -- info for user -- String
43
- # @params[:gender] -- info for user -- String
44
- # @params[:birth_year] -- info for user -- String
45
- #
46
- # @return user object
47
- def user_provision(options={})
48
- organization_id = options[:organization_id]
49
- options = {
50
- access_token: options[:access_token],
51
- user: {
52
- uid: options[:uid],
53
- profile: {
54
- height: options[:height],
55
- gender: options[:gender],
56
- location: options[:location],
57
- weight: options[:weight]
58
- }
59
- }
60
- }
61
- response = post("/#{Validic.api_version}/organizations/#{organization_id}/users.json", options)
62
- response if response
63
- end
64
-
65
- ##
66
- #
67
- # PUT request for suspending a user
68
- #
69
- # @params[:organization_id] -- String
70
- # @params[:access_token] -- String -- organization's access_token
71
- # @params[:user_id] -- String -- user's ID to suspend
72
- # @params[:suspend] -- Boolean -- (1/0)
73
- #
74
- # @return user object
75
- def user_suspend(options={})
76
- organization_id = options[:organization_id]
77
- user_id = options[:user_id]
78
- options = {
79
- suspend: options[:suspend],
80
- access_token: options[:access_token]
81
- }
82
-
83
- response = put("/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}.json", options)
84
- response
85
- end
86
-
87
- ##
88
- #
89
- # DELETE request for deleting a user
90
- #
91
- # @params[:organization_id] -- String
92
- # @params[:access_token] -- String -- organization's access_token
93
- # @params[:uid] -- String -- user's ID to suspend
94
- #
95
- # @return [Hashie::Mash] with response
96
- def user_delete(options={})
97
- organization_id = options[:organization_id]
98
-
99
- response = delete("/#{Validic.api_version}/organizations/#{organization_id}/users.json", options)
100
- response
2
+ class User
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
101
6
  end
102
7
  end
103
8
  end