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
@@ -1,17 +1,10 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module Organization
5
-
6
- ##
7
- # Get Organization base on `access_token` and organization_id
8
- #
9
- # @params :access_token
10
- # @return [Hashie::Mash] with list of Organization
11
- def get_organization(params={})
12
- params = extract_params(params)
13
- get("/#{Validic.api_version}/organizations/#{Validic.organization_id}.json", params)
2
+ class Organization
3
+ def initialize(attrs = {})
4
+ attrs.each do |k, v|
5
+ instance_variable_set("@#{k}", v)
6
+ self.class.send(:attr_reader, k)
7
+ end
14
8
  end
15
-
16
9
  end
17
10
  end
@@ -1,43 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module Profile
5
-
6
- ##
7
- # Get Profile base on `access_token`
8
- #
9
- # @params :access_token - override for default access_token
10
- # @return [Hashie::Mash] with list of Profile
11
- def get_profile(options={})
12
- options = {
13
- access_token: options[:access_token]
14
- }
15
- response = get("/#{Validic.api_version}/profile.json", options)
16
- response if response
2
+ class Profile
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
17
6
  end
18
-
19
- ##
20
- # Update Profile base on `access_token`
21
- #
22
- # @return success
23
- # def update_profile(options={})
24
- # options = {
25
- # profile: {
26
- # gender: options[:gender],
27
- # location: options[:location],
28
- # birth_year: options[:birth_year],
29
- # height: options[:height],
30
- # weight: options[:weight],
31
- # first_name: options[:first_name],
32
- # last_name: options[:last_name]
33
- # }
34
- # }
35
-
36
- # response = post("/#{Validic.api_version}/profile.json", options)
37
- # response if response
38
- # end
39
-
40
7
  end
41
8
  end
42
-
43
-
@@ -0,0 +1,15 @@
1
+ module Validic
2
+ class Response
3
+ attr_reader :summary, :records, :attributes
4
+ def initialize(summary, response)
5
+ @summary = summary
6
+
7
+ case response
8
+ when Array
9
+ @records = response
10
+ when Hash
11
+ @attributes = response
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require 'validic/app'
2
+
3
+ module Validic
4
+ module REST
5
+ module Apps
6
+
7
+ def get_org_apps
8
+ resp = get_request(:apps)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_apps :get_org_apps
12
+
13
+ def get_user_synced_apps(options = {})
14
+ resp = get_request(:sync_apps,
15
+ authentication_token: options[:authentication_token])
16
+ build_response_attr(resp)
17
+ end
18
+ alias :get_synced_apps :get_user_synced_apps
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ require 'validic/biometrics'
2
+
3
+ module Validic
4
+ module REST
5
+ module Biometrics
6
+
7
+ def get_biometrics(options = {})
8
+ resp = get_request(:biometrics, options)
9
+ build_response_attr(resp)
10
+ end
11
+
12
+ def create_biometrics(options = {})
13
+ user_id = options.delete(:user_id)
14
+ options = { user_id: user_id, biometrics: options }
15
+ response = post_request(:biometrics, options)
16
+ Validic::Biometrics.new(response['biometrics'])
17
+ end
18
+
19
+ def update_biometrics(options = {})
20
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
21
+ options = { user_id: user_id, _id: _id, biometrics: options }
22
+ response = put_request(:biometrics, options)
23
+ Validic::Biometrics.new(response['biometrics'])
24
+ end
25
+
26
+ def delete_biometrics(options = {})
27
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
28
+ options = { user_id: user_id, _id: _id }
29
+ delete_request(:biometrics, options)
30
+ true
31
+ end
32
+
33
+ def latest_biometrics(options = {})
34
+ resp = latest(:biometrics, options)
35
+ build_response_attr(resp)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ require 'validic/diabetes'
2
+
3
+ module Validic
4
+ module REST
5
+ module Diabetes
6
+
7
+ def get_diabetes(options = {})
8
+ resp = get_request(:diabetes, options)
9
+ build_response_attr(resp)
10
+ end
11
+
12
+ def create_diabetes(options = {})
13
+ user_id = options.delete(:user_id)
14
+ options = { user_id: user_id, diabetes: options }
15
+ response = post_request(:diabetes, options)
16
+ Validic::Diabetes.new(response['diabetes'])
17
+ end
18
+
19
+ def update_diabetes(options = {})
20
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
21
+ options = { user_id: user_id, _id: _id, diabetes: options }
22
+ response = put_request(:diabetes, options)
23
+ Validic::Diabetes.new(response['diabetes'])
24
+ end
25
+
26
+ def delete_diabetes(options = {})
27
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
28
+ options = { user_id: user_id, _id: _id }
29
+ delete_request(:diabetes, options)
30
+ true
31
+ end
32
+
33
+ def latest_diabetes(options = {})
34
+ resp = latest(:diabetes, options)
35
+ build_response_attr(resp)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'validic/fitness'
2
+
3
+ module Validic
4
+ module REST
5
+ module Fitness
6
+
7
+ def get_fitness(options = {})
8
+ resp = get_request(:fitness, options)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_fitnesses :get_fitness
12
+
13
+ def create_fitness(options = {})
14
+ user_id = options.delete(:user_id)
15
+ options = { user_id: user_id, fitness: options }
16
+ response = post_request(:fitness, options)
17
+ Validic::Fitness.new(response['fitness'])
18
+ end
19
+
20
+ def update_fitness(options = {})
21
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
22
+ options = { user_id: user_id, _id: _id, fitness: options }
23
+ response = put_request(:fitness, options)
24
+ Validic::Fitness.new(response['fitness'])
25
+ end
26
+
27
+ def delete_fitness(options = {})
28
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
29
+ options = { user_id: user_id, _id: _id }
30
+ delete_request(:fitness, options)
31
+ true
32
+ end
33
+
34
+ def latest_fitness(options = {})
35
+ resp = latest(:fitness, options)
36
+ build_response_attr(resp)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'validic/nutrition'
2
+
3
+ module Validic
4
+ module REST
5
+ module Nutrition
6
+
7
+ def get_nutrition(options = {})
8
+ resp = get_request(:nutrition, options)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_nutritions :get_nutrition
12
+
13
+ def create_nutrition(options = {})
14
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
15
+ options = { user_id: user_id, nutrition: options }
16
+ response = post_request(:nutrition, options)
17
+ Validic::Nutrition.new(response['nutrition'])
18
+ end
19
+
20
+ def update_nutrition(options = {})
21
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
22
+ options = { user_id: user_id, _id: _id, nutrition: options }
23
+ response = put_request(:nutrition, options)
24
+ Validic::Nutrition.new(response['nutrition'])
25
+ end
26
+
27
+ def delete_nutrition(options = {})
28
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
29
+ options = { user_id: user_id, _id: _id }
30
+ delete_request(:nutrition, options)
31
+ true
32
+ end
33
+
34
+ def latest_nutrition(options = {})
35
+ resp = latest(:nutrition, options)
36
+ build_response_attr(resp)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,12 @@
1
+ require 'validic/organization'
2
+
3
+ module Validic
4
+ module REST
5
+ module Organizations
6
+ def get_organization(params = {})
7
+ resp = get_request(:organizations, params)
8
+ build_response_attr(resp)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ require 'validic/profile'
2
+
3
+ module Validic
4
+ module REST
5
+ module Profile
6
+ def get_profile(options = {})
7
+ resp = get_request(:profile, options)
8
+ Validic::Profile.new(resp)
9
+ end
10
+
11
+ def create_profile(options = {})
12
+ token = options.delete(:authentication_token)
13
+ resp = post_request(:profile, authentication_token: token, profile: options)
14
+ Validic::Profile.new(resp['profile'])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,95 @@
1
+ require 'multi_json'
2
+ require 'validic/error'
3
+
4
+ module Validic
5
+ module REST
6
+ module Request
7
+ def latest(type, options = {})
8
+ organization_id = options[:organization_id] || Validic.organization_id
9
+ user_id = options.delete(:user_id)
10
+ if user_id
11
+ path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/latest.json"
12
+ else
13
+ path = "/#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}/latest.json"
14
+ end
15
+ get(path, options)
16
+ end
17
+
18
+ private
19
+
20
+ def get_request(type, options = {})
21
+ path = construct_path(type, options)
22
+ get(path, options)
23
+ end
24
+
25
+ def post_request(type, options = {})
26
+ path = construct_path(type, options)
27
+ post(path, options)
28
+ end
29
+
30
+ def put_request(type, options = {})
31
+ path = construct_path(type, options)
32
+ put(path, options)
33
+ end
34
+
35
+ def delete_request(type, options = {})
36
+ path = construct_path(type, options)
37
+ delete(path, options)
38
+ end
39
+
40
+ def construct_path(type, options)
41
+ organization_id = options.delete(:organization_id) || Validic.organization_id
42
+ user_id = options.delete(:user_id)
43
+ activity_id = options.delete(:_id)
44
+ if activity_id
45
+ path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/#{activity_id}.json"
46
+ elsif user_id && type == :users
47
+ path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}.json"
48
+ elsif user_id
49
+ path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}.json"
50
+ elsif type == :me
51
+ path = "/#{Validic.api_version}/me.json"
52
+ elsif type == :profile
53
+ path = "/#{Validic.api_version}/profile.json"
54
+ elsif type == :organizations
55
+ path = "/#{Validic.api_version}/organizations/#{organization_id}.json"
56
+ else
57
+ path = "/#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}.json"
58
+ end
59
+ path
60
+ end
61
+
62
+ def get(path, options)
63
+ request(:get, path, options)
64
+ end
65
+
66
+ def post(path, options)
67
+ request(:post, path, options)
68
+ end
69
+
70
+ def put(path, options)
71
+ request(:put, path, options)
72
+ end
73
+
74
+ def delete(path, options)
75
+ request(:delete, path, options)
76
+ end
77
+
78
+ def request(method, path, options)
79
+ options[:access_token] = options[:access_token].nil? ? Validic.access_token : options[:access_token]
80
+ response = connection.send(method) do |request|
81
+ case method
82
+ when :get
83
+ request.url(path, options)
84
+ when :post, :put, :delete
85
+ request.path = path
86
+ request.body = MultiJson.encode(options) unless options.empty?
87
+ end
88
+ end
89
+ error = Validic::Error::ERRORS[response.status]
90
+ raise error.from_response(response.body) if error
91
+ response.body
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,40 @@
1
+ require 'validic/routine'
2
+
3
+ module Validic
4
+ module REST
5
+ module Routine
6
+
7
+ def get_routine(params = {})
8
+ resp = get_request(:routine, params)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_routines :get_routine
12
+
13
+ def create_routine(options = {})
14
+ user_id = options.delete(:user_id)
15
+ options = { user_id: user_id, routine: options }
16
+ response = post_request(:routine, options)
17
+ Validic::Routine.new(response['routine'])
18
+ end
19
+
20
+ def update_routine(options = {})
21
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
22
+ options = { user_id: user_id, _id: _id, routine: options }
23
+ response = put_request(:routine, options)
24
+ Validic::Routine.new(response['routine'])
25
+ end
26
+
27
+ def delete_routine(options = {})
28
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
29
+ options = { user_id: user_id, _id: _id }
30
+ delete_request(:routine, options)
31
+ true
32
+ end
33
+
34
+ def latest_routine(options = {})
35
+ resp = latest(:routine, options)
36
+ build_response_attr(resp)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,41 @@
1
+ require 'validic/sleep'
2
+
3
+ module Validic
4
+ module REST
5
+ module Sleep
6
+
7
+ def get_sleep(options = {})
8
+ resp = get_request(:sleep, options)
9
+ build_response_attr(resp)
10
+ end
11
+ alias :get_sleeps :get_sleep
12
+
13
+ def create_sleep(options = {})
14
+ user_id = options.delete(:user_id)
15
+ options = { user_id: user_id, sleep: options }
16
+ response = post_request(:sleep, options)
17
+ Validic::Sleep.new(response['sleep'])
18
+ end
19
+
20
+ def update_sleep(options = {})
21
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
22
+ options = { user_id: user_id, _id: _id, sleep: options }
23
+ response = put_request(:sleep, options)
24
+ Validic::Sleep.new(response['sleep'])
25
+ end
26
+
27
+ def latest_sleep(options = {})
28
+ resp = latest(:sleep, options)
29
+ build_response_attr(resp)
30
+ end
31
+
32
+ def delete_sleep(options = {})
33
+ user_id, _id = options.delete(:user_id), options.delete(:_id)
34
+ options = { user_id: user_id, _id: _id }
35
+ delete_request(:sleep, options)
36
+ true
37
+ end
38
+
39
+ end
40
+ end
41
+ end