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,37 +1,20 @@
1
- require "faraday"
2
- require "faraday_middleware"
3
- require "validic/client"
4
- require "validic/version"
5
-
6
- directory = File.expand_path(File.dirname(__FILE__))
1
+ require 'validic/client'
7
2
 
8
3
  module Validic
9
-
4
+ BASE_URL = "https://api.validic.com/v1"
10
5
  class << self
11
6
  attr_accessor :api_url,
12
7
  :api_version,
13
8
  :access_token,
14
- :organization_id,
15
- :user_id
9
+ :organization_id
16
10
 
17
11
  ##
18
12
  # Configure default
19
13
  #
20
14
  # @yield [Validic]
21
15
  def configure
22
- load_defaults
23
16
  yield self
24
17
  true
25
18
  end
26
-
27
- private
28
-
29
- def load_defaults
30
- self.api_url ||= 'https://api.validic.com'
31
- self.api_version ||= 'v1'
32
- self.access_token ||= 'ENTERPRISE_KEY'
33
- self.organization_id ||= '51aca5a06dedda916400002b'
34
- end
35
19
  end
36
-
37
20
  end
@@ -0,0 +1,11 @@
1
+ module Validic
2
+ class App
3
+ def initialize(attrs={})
4
+ #create attr_reader for all key value pairs
5
+ attrs.each do |k, v|
6
+ instance_variable_set("@#{k}", v)
7
+ self.class.send(:attr_reader, k)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'validic/utils'
2
+ module Validic
3
+ class Biometrics
4
+ include Validic::Utils
5
+ def initialize(attrs = {})
6
+ attributes_builder(attrs, self)
7
+ end
8
+ end
9
+ end
@@ -1,36 +1,38 @@
1
- # encoding: utf-8
2
- require 'forwardable'
3
- require 'validic/request'
4
- require 'validic/organization'
5
- require 'validic/user'
6
- require 'validic/profile'
7
- require 'validic/fitness'
8
- require 'validic/weight'
9
- require 'validic/nutrition'
10
- require 'validic/sleep'
11
- require 'validic/diabetes'
12
- require 'validic/biometric'
13
- require 'validic/routine'
14
- require 'validic/tobacco_cessation'
15
- require 'validic/third_party_app'
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ require 'validic/rest/apps'
5
+ require 'validic/rest/biometrics'
6
+ require 'validic/rest/diabetes'
7
+ require 'validic/rest/fitness'
8
+ require 'validic/rest/nutrition'
9
+ require 'validic/rest/organizations'
10
+ require 'validic/rest/profile'
11
+ require 'validic/rest/request'
12
+ require 'validic/rest/routine'
13
+ require 'validic/rest/sleep'
14
+ require 'validic/rest/tobacco_cessation'
15
+ require 'validic/rest/users'
16
+ require 'validic/rest/utils'
17
+ require 'validic/rest/weight'
18
+ require 'validic/version'
16
19
 
17
20
  module Validic
18
21
  class Client
19
- extend Forwardable
20
-
21
- include Request
22
- include Organization
23
- include User
24
- include Profile
25
- include Fitness
26
- include Weight
27
- include Nutrition
28
- include Sleep
29
- include Diabetes
30
- include Biometric
31
- include Routine
32
- include TobaccoCessation
33
- include ThirdPartyApp
22
+ include REST::Apps
23
+ include REST::Biometrics
24
+ include REST::Diabetes
25
+ include REST::Fitness
26
+ include REST::Nutrition
27
+ include REST::Organizations
28
+ include REST::Profile
29
+ include REST::Request
30
+ include REST::Routine
31
+ include REST::Sleep
32
+ include REST::TobaccoCessation
33
+ include REST::Users
34
+ include REST::Utils
35
+ include REST::Weight
34
36
 
35
37
  attr_accessor :api_url,
36
38
  :api_version,
@@ -42,11 +44,10 @@ module Validic
42
44
  #
43
45
  # @params options[Hash]
44
46
  def initialize(options={})
45
- @api_url = options[:api_url].nil? ? Validic.api_url : options[:api_url]
47
+ @api_url = options[:api_url].nil? ? 'https://api.validic.com' : options[:api_url]
46
48
  @api_version = options[:api_version].nil? ? 'v1' : options[:api_version]
47
49
  @access_token = options[:access_token].nil? ? Validic.access_token : options[:access_token]
48
50
  @organization_id = options[:organization_id].nil? ? Validic.organization_id : options[:organization_id]
49
-
50
51
  reload_config
51
52
  end
52
53
 
@@ -55,56 +56,14 @@ module Validic
55
56
  #
56
57
  # @return [Faraday::Connection]
57
58
  def connection
58
- params = {}
59
- @connection = Faraday.new(url: @api_url, params: params, headers: default_headers, ssl: {verify: true}) do |faraday|
60
- faraday.use FaradayMiddleware::Mashify
59
+ Faraday.new(url: @api_url, headers: default_headers, ssl: { verify: true }) do |faraday|
60
+ # faraday.use FaradayMiddleware::Mashify
61
61
  faraday.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
62
62
  faraday.use FaradayMiddleware::FollowRedirects
63
63
  faraday.adapter Faraday.default_adapter
64
64
  end
65
65
  end
66
66
 
67
- ##
68
- # Pull the latest endpoint
69
- def latest(type, org_id=nil, user_id=nil, params={})
70
- organization_id = org_id.nil? ? Validic.organization_id : org_id
71
- user_id = org_id.nil? ? Validic.user_id : org_id
72
-
73
- url = "/#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}/latest.json"
74
-
75
- if user_id
76
- url = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/latest.json"
77
- end
78
-
79
- get(url, params)
80
- end
81
-
82
- ##
83
- # Generic Pull of Validic Objects
84
- def get_endpoint(type, params={})
85
-
86
- url = "/#{Validic.api_version}/organizations/#{Validic.organization_id}/#{type.to_s}.json"
87
-
88
- if Validic.user_id && type != :users
89
- url = "/#{Validic.api_version}/organizations/#{Validic.organization_id}/users/#{Validic.user_id}/#{type.to_s}.json"
90
- elsif Validic.user_id && type == :users
91
- url = "/#{Validic.api_version}/organizations/#{Validic.organization_id}/users/#{Validic.user_id}.json"
92
- end
93
-
94
- get(url, params)
95
- end
96
-
97
- ##
98
- # Generic POST to Validic
99
- def post_to_validic(type, params={})
100
- url = "/#{Validic.api_version}/organizations/#{Validic.organization_id}/#{type.to_s}.json"
101
-
102
- if Validic.user_id
103
- url = "/#{Validic.api_version}/organizations/#{Validic.organization_id}/users/#{Validic.user_id}/#{type.to_s}.json"
104
- end
105
- post(url, params)
106
- end
107
-
108
67
  private
109
68
 
110
69
  def default_headers
@@ -121,6 +80,5 @@ module Validic
121
80
  Validic.access_token = access_token
122
81
  Validic.organization_id = organization_id
123
82
  end
124
-
125
83
  end
126
84
  end
@@ -1,64 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module Diabetes
5
-
6
- ##
7
- # Get User Diabetes 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 Diabetes
20
- def get_diabetes(params={})
21
- params = extract_params(params)
22
- get_endpoint(:diabetes, params)
2
+ class Diabetes
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
23
6
  end
24
-
25
- ##
26
- # Create Diabetes 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 :c_peptide
32
- # @params :fasting_plasma_glucose_test
33
- # @params :hba1c
34
- # @params :insulin
35
- # @params :oral_glucose_tolerance_test
36
- # @params :random_plasma_glucose_test
37
- # @params :triglyceride
38
- # @params :timestamp
39
- # @params :source
40
- #
41
- # @return success
42
- def create_diabetes(options={})
43
- options = {
44
- authentication_token: options[:authentication_token],
45
- access_token: options[:access_token],
46
- diabetes: {
47
- c_peptide: options[:c_peptide],
48
- fasting_plasma_glucose_test: options[:fasting_plasma_glucose_test],
49
- hba1c: options[:hba1c],
50
- insulin: options[:insulin],
51
- oral_glucose_tolerance_test: options[:oral_glucose_tolerance_test],
52
- random_plasma_glucose_test: options[:random_plasma_glucose_test],
53
- triglyceride: options[:triglyceride],
54
- timestamp: options[:timestamp],
55
- source: options[:source]
56
- }
57
- }
58
-
59
- response = post("/#{Validic.api_version}/diabetes.json", options)
60
- response if response
61
- end
62
-
63
7
  end
64
8
  end
@@ -0,0 +1,33 @@
1
+ module Validic
2
+ class Error < StandardError
3
+ ClientError = Class.new(self)
4
+ ServerError = Class.new(self)
5
+
6
+ Unauthorized = Class.new(ClientError)
7
+ NotFound = Class.new(ClientError)
8
+ Forbidden = Class.new(ClientError)
9
+ UnprocessableEntity = Class.new(ClientError)
10
+ Conflict = Class.new(ClientError)
11
+ InternalServerError = Class.new(ServerError)
12
+
13
+ ERRORS = {
14
+ 401 => Validic::Error::Unauthorized,
15
+ 403 => Validic::Error::Forbidden,
16
+ 404 => Validic::Error::NotFound,
17
+ 409 => Validic::Error::Conflict,
18
+ 422 => Validic::Error::UnprocessableEntity,
19
+ 500 => Validic::Error::InternalServerError
20
+ }
21
+
22
+ def self.from_response(body)
23
+ code, errors = [body['code'], body['errors']]
24
+ message = "#{code}: #{errors}"
25
+ new(message)
26
+ end
27
+
28
+ def initialize(message = '', code = nil)
29
+ super(message)
30
+ @code = code
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,10 @@
1
+ module Validic
2
+ class Extra
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
8
+ end
9
+ end
10
+ end
@@ -1,61 +1,8 @@
1
- # encoding: utf-8
2
-
3
1
  module Validic
4
- module Fitness
5
-
6
- ##
7
- # Get Fitness 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 Fitness
20
- def get_fitness(params={})
21
- params = extract_params(params)
22
- get_endpoint(:fitness, params)
2
+ class Fitness
3
+ include Validic::Utils
4
+ def initialize(attrs = {})
5
+ attributes_builder(attrs, self)
23
6
  end
24
-
25
- alias :get_fitnesses :get_fitness
26
-
27
- ##
28
- # Create Fitness 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 :timestamp
34
- # @params :primary_type
35
- # @params :intensity
36
- # @params :start_time
37
- # @params :total_distance
38
- # @params :duration
39
- # @params :source
40
- #
41
- # @return success
42
- def create_fitness(options={})
43
- options = {
44
- access_token: options[:access_token],
45
- fitness: {
46
- timestamp: options[:timestamp],
47
- primary_type: options[:primary_type],
48
- intensity: options[:intensity],
49
- start_time: options[:start_time],
50
- total_distance: options[:total_distance],
51
- duration: options[:duration],
52
- source: options[:source]
53
- }
54
- }
55
-
56
- response = post("/#{Validic.api_version}/fitness.json", options)
57
- response if response
58
- end
59
-
60
7
  end
61
8
  end
@@ -1,65 +1,9 @@
1
- # encoding: utf-8
2
-
1
+ require 'validic/utils'
3
2
  module Validic
4
- module Nutrition
5
-
6
- ##
7
- # Get Nutrition 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 Nutrition
20
- def get_nutritions(params={})
21
- params = extract_params(params)
22
- get_endpoint(:nutrition, params)
3
+ class Nutrition
4
+ include Validic::Utils
5
+ def initialize(attrs = {})
6
+ attributes_builder(attrs, self)
23
7
  end
24
-
25
- ##
26
- # Create Nutrition 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 :calories
32
- # @params :carbohydrates
33
- # @params :fat
34
- # @params :fiber
35
- # @params :protein
36
- # @params :sodium
37
- # @params :water
38
- # @params :timestamp
39
- # @params :meal
40
- # @params :source
41
- #
42
- # @return success
43
- def create_nutrition(options={})
44
- options = {
45
- access_token: options[:access_token],
46
- nutrition: {
47
- calories: options[:calories],
48
- carbohydrates: options[:carbohydrates],
49
- fat: options[:fat],
50
- fiber: options[:fiber],
51
- protein: options[:protein],
52
- sodium: options[:sodium],
53
- water: options[:water],
54
- timestamp: options[:timestamp],
55
- meal: options[:meal],
56
- source: options[:source]
57
- }
58
- }
59
-
60
- response = post("/#{Validic.api_version}/nutrition.json", options)
61
- response if response
62
- end
63
-
64
8
  end
65
9
  end